To learn Docker, it is necessary to master the Docker command. If the foundation is not solid, it cannot be further improved. Docker has a lot of commands, but not a lot of them. This article describes common commands based on actual situations.

Before you begin, you must clear, docker mirror is a static concept, and the container is a dynamic concept, some similar program, the binary executable file, is just a file, whether running or not running, it is there (unless removed), but only run, is the real “program”, can play its role. Similarly, only when you run the image and turn it into a container can you really run the “image” for the corresponding purpose.

Docker permission management

By default, most docker commands are only allowed to be used by root users. In order to make the docker command available to ordinary users. Permissions need to be configured. Refer to the previous article for details.

Docker program

Enter docker and press Tab once, you can see that there are many commands that start with docker, as follows:

$ docker
docker                  docker-containerd-ctr   docker-init             docker-runc             
docker-compose          docker-containerd-shim  docker-machine          docker-swarm            
docker-containerd       dockerd                 docker-proxy   
Copy the code

Docker, dockerd, docker-compose, docker-compose, docker-compose, etc. Dockerd server program provides all docker services, query its running status as follows:

$ps - ef | grep dockerd root 1893 1 0 on July 13? 00:57:05 /usr/bin/dockerd -H fd://Copy the code

This process is started and stopped by /etc/init.d/docker. If you stop this process, many docker commands will become unusable.

For most applications, “docker command” refers to using the Docker command, which is also the operation of the Docker client. Docker client can connect to the Docker server of other hosts, which may be used in docker in Docker occasions, and will be introduced in subsequent articles.

Docker service related

Stop the docker:

D /docker stop or service docker stopCopy the code

Start the docker:

/etc/init.d/docker start or service docker startCopy the code

Restart docker:

D /docker restart or service docker restartCopy the code

Docker client related

Pull the mirror

Docker official website (dockerhub or other websites have many already done image files, using the Docker pull command can be directly pulled to the local use (without creating their own) :

docker pull image-name:tag  
Copy the code

Such as:

Docker Pull NVIDIA/CUDA :8.0 # Nvida official mirror docker pull registry.cn-hangzhou.aliyuncs.com/latelee/wordpress # I hosted on ali cloud a mirror image of the warehouseCopy the code

Note that the “latest” tag is used by default, and you do not need to enter “latest”. To specify a version, a label is required. By default, the official dockerHub image is downloaded from hub.docker.com/. If you download mirrors from other websites…

Create a mirror image

If you want to create an image by yourself, you need to write your own Dockerfile, and then use the docker build command as follows:

docker build -t myimage .
Copy the code

The creation of mirrors will be covered in a future article.

Upload the image

For images that have been made (or pulled from other repositories), you can upload them to your own repository using a Docker push. The first step is to log into the repository, which is hub.docker.com by default. Use the docker login command to enter your account and password. If you log into Aliyun warehouse, the command is as follows:

Sudo docker login [email protected] registry.cn-hangzhou.aliyuncs.com #Copy the code

Uploading an image requires two steps: first, label the image, use docker tag command, and then use Docker push to upload the image to the repository. The default is hub.docker.com. Like uploading BusyBox to hub.docker.com.

docker tag busybox latelee/busyboxdocker push latelee/busybox
Copy the code

About the use of Ali Cloud warehouse, the follow-up article. For docker images that do not involve confidentiality, it is recommended to use Dockerfile+ automatic build to ensure that you do not need to create their own. Note that the mirror name must be lowercase.

Run the image (into a container)

The following commands are used to run an image:

Docker run-itd Image nameCopy the code

The above command is simple, and its variations are as follows:

  • Specify the container name
Docker run --name Container name -ITD Image nameCopy the code
  • The directory needs to be mounted
Docker run --name Container name -v /home:/home -itd Image nameCopy the code

-v indicates mounting. The former directory is the host directory, and the latter directory is the directory in the container. Notice that if the mounted container directory already contains content, it will be overwritten. After the mount, the host is written to the mount directory but cannot be seen in the container. The container writes files to the mount directory, visible to the host. During the container running, if you delete the directory mounted to the host and create a directory with the same name, the mount fails and you need to restart the container to take effect. Question: Is there a shortcut for mounting a directory with the same path?

  • Port to be mapped
Docker run --name Container name -p 8080:80 -itd Image nameCopy the code

-p The former port indicates the host port, and the latter port indicates the container internal port. Note The host port cannot be occupied repeatedly.

Exit or start automatically:

Docker run-itd --name Container name --restart=always Image nameCopy the code

Updatable for containers already running:

Docker update --restart=always Container name or container IDCopy the code

Note: If there is a problem with the image startup itself, using always may cause repeated restarts.

Stop the container using the command:

Docker stop Container ID/ container nameCopy the code

Delete container command:

Docker RM Container ID/ container nameCopy the code

Note that the container to be removed must be stopped, and the -f option is required to forcibly remove the container.

In addition, docker start can also be used to start the image, but the author actually uses less.

Into the container

Run an already running image (used when the container starts in the background and wants to enter the container interface) :

Docker exec-it container ID/ container name bashDocker exec-it container ID/ container name shCopy the code

Note that the above “bash” or “sh” command is the first command executed by the run container. The previous examples are not noted because each Docker image has a default start execution command, which is described later. When executing Docker Exec, you can use the commands to execute, such as:

Docker exec -it myUbuntu uname -aLinux 6f15602f0338 4.13.0-38-generic #43~16.04.1-Ubuntu SMP Wed Mar 14 17:48:43 UTC 2018 x86_64 x86_64 x86_64 GNU/LinuxCopy the code

The result of executing these commands is the same as that of entering the container and then executing the same command, but it can reduce the step of entering the container, which is very useful in practice.

Remove the mirror

To delete a mirror, run the following command:

Docker RmI image nameCopy the code

Note that docker RM is delete container, while Docker RMI is delete image, the two operation objects are different.

Save the container as a new image

When changes are made to the container, it may be necessary to save the current state as the new image.

Docker commit < container ID> < new image name >[: tag] For example, docker commit cb9a3D81a6A0 busybox:devCopy the code

Note: It is best not to exit the container completely. Note: Docker is built in layers. You can use Docker history < image name > to view the build information of each layer. If the previous volume is large, the volume will not decrease even if a large number of files are deleted this time.

Export import image

Once an image has been created on one host, it can be migrated to another host using the export and import function (again, using only the pull form of the repository, but relying on the mirror repository).

Save the image to a local directory:

docker save -o file.tar image:tag
Copy the code

Example:

Docker save -o Ubuntu16.04_20170703.tar Ubuntu :16.04Copy the code

Note that the package is in tar format, not tar.bz2 format. If you want to explore the package, you can decompress it. The resulting content contains many layers that are not covered here. To import on another host, use the following command format:

docker load -i file.tar
Copy the code

The following is an example:

The docker load - I ubuntu16.04 _1017. TarCopy the code

After the import, use Docker images to view the image information.

Note that the export command needs to specify the image name and label. If the image ID is used, the imported image name and label will be < None >.

Other common commands

Stop all containers:

docker stop $(docker ps -aq)
Copy the code

Delete all containers (not deleted if they are running) :

docker rm $(docker ps -aq)
Copy the code

Delete all mirrors that include latelee keyword:

Docker rmi $(docker images | grep latelee | awk '{print $3}') / / awk's role is to get three columns (the third column is the mirror ID)Copy the code

Delete all mirrors marked none:

docker rmi $(docker images | grep none | awk '{print $3 }')
Copy the code

Delete all mirrors (carefully) :

Docker rmi $(docker images | awk '{print $3}') / / additive - f mandatory deleteCopy the code

Look at the log:

Docker logs < container name/container ID>Copy the code

To continuously observe logs, add the -f option.

Docker logs -f < container name/container ID>Copy the code

View the last 100 lines of the log:

Docker logs -f --tail 100 docker logs -f --tail 100Copy the code

Turn on experimental features with environment variables:

export DOCKER_CLI_EXPERIMENTAL=enabled
Copy the code

All commands in this article have been tested, but are only valid at the time of Posting or after a certain period of time. But the essence of the command is the same.

practice

switchrootpermissions

Scenario: Download the centos image and run it as a common user by default. You cannot switch to root because you do not know the password.

Sudo docker exec it -u root < container name or ID> bashCopy the code

In this case, the container is the root permission. Run the passwd root command to set the password. In this case, run the su root command to switch the permission of the container.