Docker common command summary

Posted by flatpooks on Sat, 25 Dec 2021 03:49:37 +0100

Docker image command

List mirrors

$ docker images
$ docker image ls -a

Run Docker image (guard mode)

$ docker run -d {Image name}

Delete the specified Docker image

$ docker  image rm {Image name}

Delete Docker virtual image

$ docker image prune

Docker container command

List running containers

$ docker ps -a

List all containers (including stopped containers)

$ docker ps -l

Enter the running Docker container

$ docker exec -it {container ID} /bin/bash

Stop Docker container

$ docker stop {container ID}

Delete the specified Docker container

$ docker rm -f {container ID}

Delete stopped Docker container

$ docker container prune

View Docker container history run log

$ docker logs {Container name}

Monitor Docker container running log in real time

$ docker logs -f {Container name}

Docker data volume command

Create Docker data volume

$ docker volume create {Data volume name}

List all Docker data volumes

$ docker volume ls

Delete the specified Docker data volume

$ docker volume rm {Data volume name}

Delete unassociated (retired) Docker data volumes

$ docker volume prune
$ docker volume rm $(docker volume ls -qf dangling=true)

Docker file operation command

Copy files from the host to the Docker container

$ sudo docker cp {File path in host} {container ID}:{File storage path in container}

Copy files from Docker container to host

$ sudo docker cp {container ID}:{File path in container} {File storage path in host}

Start a container from the image

The docker run command will first create a writable container from a specific image, and then start it through the start command. The stopped container can be restarted and keep the original changes. The run command has many startup parameters. Here are some general instructions

When using docker run to create a container, the standard operations of Docker running in the background include:

  • Check whether the specified image exists locally. If it does not exist, download it from the public warehouse
  • Create and start a container using a mirror
  • Allocate a file system and mount a read-write layer outside the read-only image layer
  • Bridge a virtual interface from the bridge interface configured by the host to the container
  • Configure an ip address from the address pool to the container
  • Execute user specified applications
  • The container is terminated after execution
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Here is an example:

$ sudo docker run --name nginx_test \
> -v /tmp/docker:/usr/share/nginx/html:ro \
> -p 80:80 -d \
> nginx:1.7.6

start/stop/restart container (start/stop/restart)

A container can be run by creating a new one through run, or it can restart the container that has been stopped. However, start can no longer specify the instructions to run when the container is started, because docker can only have one foreground process.

When the container stop s (or Ctrl+D), it will exit after saving the status of the current container. The next start will keep the changes made when it was closed last time. Moreover, the interface for entering the attach is the same every time, which is the same as the time of the first run start or commit submission.

CONTAINER_ID=$(docker start <containner_id>)
$ sudo docker stop $CONTAINER_ID
$ sudo docker restart $CONTAINER_ID

Connect to a running container (attach)

The container to be attached must be running. You can connect the same container to share the screen at the same time (similar to the attachment of the screen command).

The official document says that you can use CTRL-C to detach after attach, but in fact, after my test, if the container is currently running bash, CTRL-C is naturally the input of the current line and does not exit; If the container is currently running a process in the foreground, such as the output of nginx access Log, CTRL-C will not only cause the container to exit, but also stop. This is not what we want. Detach means to leave the container terminal, but the container is still running. Fortunately, attach can bring -- sig proxy = false to ensure that CTRL-D or CTRL-C will not close the container.

$ sudo docker attach --sig-proxy=false $CONTAINER_ID

View the underlying information of image or container (inspect)

The objects of inspect can be image, running container and stopped container.

View the internal IP address of the container

$ sudo docker inspect --format='{{.NetworkSettings.IPAddress}}' $CONTAINER_ID

View the processes running in the container (top)

When the container is running, there may not be a / bin/bash terminal to interactively execute the top command and view the running processes in the container. Moreover, there may not be a top command. This is docker top < container_ id/container_ Name > is very useful. In fact, using ps -ef|grep docker on the host can also see a similar set of process information. It is right to regard the process in the container as the child process that starts docker on the host.

$ sudo docker top <container_id/container_name>

Docker build command

The build command can create images from Dockerfile and context:

$ sudo docker build [OPTIONS] PATH | URL | -

The files in the above PATH or URL are called context. The process of building image will first transfer these files to the server of docker.

# cat Dockerfile 
 FROM seanlook/nginx:bash_vim
 EXPOSE 80
 ENTRYPOINT /usr/sbin/nginx -c /etc/nginx/nginx.conf && /bin/bash

# docker build -t seanlook/nginx:bash_vim_Df .

tag the image

Tag has two main functions: one is to give an easy to understand name to the image, and the other is to re specify the image warehouse through docker tag, so that it can be automatically submitted to the warehouse during push.

The same image_ All tags of ID are merged into a new tag

$ sudo docker tag 195eb90b5349 seanlook/ubuntu:rm_test

Create a new tag and keep the old record

$ sudo docker tag Registry/Repos:Tag New_Registry/New_Repos:New_Tag

Solidify a container into a new image (commit)

When we are making our own image, we will install some tools and modify the configuration in the container. If we do not commit and save it, the container will stop and start again, and these changes will disappear.

$ sudo docker commit <container> [repo:tag]

The following repo:tag is optional

Only running containers can be submitted, that is, containers visible through docker ps

Push an image or repository to the registry (push)

Corresponding to the pull above, it can be pushed to the Public, Private and Private servers of the Docker Hub, but not to the Top Level Repository.

$ sudo docker push seanlook/mongo
$ sudo docker push registry.tp-link.net:5000/mongo:2014-10-27

registry.tp-link.net can also be written as IP, 172.29 88.222.

When the repository does not exist, the one push ed on the command line will be created as a private library for us, but the one created through the browser is a public library by default.

Other commands

docker also has some commands that are not commonly used, such as login, cp, logs, export, import, load, kill, etc. they are relatively simple. Please refer to the official website.

events, history, and logs commands

These three commands are used to view the system log information of Docker. The events command will print out real-time system events; The history command will print the historical version information of the specified image, that is, the command record of constructing each layer of the image; The logs command prints out the running logs of the processes in the container.

docker events [options]: get real-time events from the server.

docker history [options] image: view the creation history of the specified image.

docker logs [options] container

Options:

     --details    Show more information
   -f, --follow     Trace log output. The last line is the log with the current timestamp
     --since string  Displays logs from a specific time or period
     --tail string  How many lines of logs are displayed from the end of the log? The default is all
   -t, --timestamps   presentation time stamp 

Next: Docker uses portal to build a visual management interface →