Home >Operation and Maintenance >Docker >How to use docker after installing it

How to use docker after installing it

PHPz
PHPzOriginal
2023-04-18 09:48:312863browse

In recent years, Docker has been widely used in software development and deployment. It provides a containerized technical solution and brings great convenience to developers and operation and maintenance personnel. After installing Docker, we need to know how to use it to build, run and manage containers. The following details how to use Docker after installing it.

1. Start Docker

After installing Docker, we need to start the Docker service. In the Linux system, you can use the following command to check whether it has been started:

systemctl status docker

If it is displayed active (running), it means that Docker has been started, otherwise it can be started by the following command:

sudo systemctl start docker

2. After starting Docker using Docker

, we can use Docker to build, run and Manage containers. Docker provides many command line tools. Here are some commonly used ones:

  1. docker pull

docker pull is used to obtain images from the Docker warehouse. Its command format is as follows:

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Among them, OPTIONS is optional and can be used to specify which warehouse to obtain the image from and which authentication method to use; NAME is the image name, which can include the private warehouse address, and TAG is the label of the image, which is optional ;DIGEST is the hash value of the image and can also be used to identify the image. The sample command is as follows:

docker pull ubuntu:18.04
  1. docker run

docker run is used to create and start the container. The command format is as follows:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Among them, OPTIONS is optional, and the following are commonly used:

  • -d runs the container in the background and returns the container ID;
  • -it runs the container in interactive mode, usually used with -t and -i;
  • -p maps the container internal port to the host port;
  • --name specifies a name for the container ;
  • --restart Set the restart strategy of the container.

IMAGE is the image name, COMMAND is the command to be run when starting the container, and ARG is the parameter of the command. The example command is as follows:

docker run -it --rm --name ubuntu_test ubuntu:18.04 /bin/bash
  1. docker ps

docker ps is used to list currently running containers. The command format is as follows:

docker ps [OPTIONS]

Among them, OPTIONS is optional, and the following are commonly used:

  • -a Lists all containers, including stopped containers;
  • -q List only the container IDs.

The sample command is as follows:

docker ps -a
  1. docker logs

docker logs is used to view the logs of the container. The command format is as follows:

docker logs [OPTIONS] CONTAINER

Among them, OPTIONS is optional, and the following are commonly used:

  • -f real-time output log;
  • -t display Timestamp;
  • --tail displays the last few logs.

CONTAINER is the container name or ID. The example command is as follows:

docker logs -f ubuntu_test
  1. docker stop

docker stop is used to stop a running container. The command format is as follows:

docker stop [OPTIONS] CONTAINER [CONTAINER...]

Among them, OPTIONS is optional, and the following are commonly used:

  • -t Set the timeout.

The sample commands are as follows:

docker stop ubuntu_test

3. Summary

Docker provides a wealth of command line tools, making it very simple to build, run and manage containers. . This article introduces common commands of Docker and hopes to provide a reference for readers. When using Docker, you need to pay attention to security and performance issues. For example, when making an image, you should try to avoid installing too many software packages in it. At the same time, you should specify the resource limit of the container when starting the container to prevent the container from occupying too many resources and causing system performance degradation.

The above is the detailed content of How to use docker after installing it. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn