Docker common commands

  • Check the version
docker -v
  • View the detailed version
docker version
  • View Docker basic information
docker info

Image related commands

Mirror repository address: https://hub.docker.com

  • View the locally installed image
docker images
  • Search for a specified image
Docker search <image-name> # docker search centos
  • Pull the mirror
Docker pull <image-name> # for example pull centos (will pull the latest version of the image, Docker pull Ubuntu :16.04 Docker pull Ubuntu :16.04 Docker pull Ubuntu :16.04
  • Remove the mirror
Docker rmi <image-name> # docker rmi <image-name> #
  • Delete all images
Docker rmi $# (docker images - q) to see all mirror mirror id docker images - q
  • Update image
Ubuntu: 15.10bash: docker run -it: 15.10bash: apt-get update Enter the exit command to exit the container # commit a container copy with docker commit docker commit -m="has update" -a=" Alex "e218edb10161 Alex /ubuntu:v2 # -m # e218edb10161 # alex/ubuntu:v2 # specify the name of the target image to be created # view new image docker images # use new image Alex/Ubuntu: V2 to launch a container docker run-it Alex/Ubuntu: V2 bash
  • Build the mirror
  1. Vim ~ / glory/codes/book/demo/Dockerfile filling the following content, build a centos 8 system
If you write a RUN FROM centos for 3 times, you will create 3 new layers on the docker, which will cause the image to be bloated too much. This way will only create 1 layer below the mirror RUN/bin/echo 'root: 123456' | chpasswd. \ useradd alex; \ /bin/echo 'alex:123456' | chpasswd; \ /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local EXPOSE 22 EXPOSE 80 CMD /usr/sbin/sshd -D
  1. Start building the image. Be careful not to build the image~/glory/codes/book/demoDedicated files in the directory, because all files in the directory will be packaged and sent to the Docker engine, and too many files will slow the build process
# -t is the name of the target image to be created: # ~/glory/codes/book/demo is the directory where the Dockerfile is located: docker build-t alex/ centOS :8.0 ~/glory/codes/book/demo # View the image information already built docker images # Use the new image to create a new container docker run-it Alex/CentOS :8.0 bash
  • Set the mirror tag
$docker images REPOSITORY TAG IMAGE ID CREATED SIZE Alex/CentOS 8.0 594AB4747ED4 14 minutes ago 210MB $docker images REPOSITORY tag IMAGE ID CREATED SIZE $docker images REPOSITORY tag 594AB4747ED4 alex1/centos1:8.1.1 Alex1 / Centos1 8.1154ab4747ed4 14 minutes ago 8.115MB Alex1 / Centos1 8.1154ab4747ed4 14 minutes ago 8.1MB

Container-related commands

  • View the system version information for the container
Linux version 4.19.121- LinuxKit (root@18b3f92ade35) (GCC version 9.2.0 (Alpine 9.2.0)) #1 SMP Thu Jan 21 15:36:34 UTC 2021
  • Look at all containers
Docker container ls-a # / docker container ls-a # / docker container ls-a # / docker container ls-a # / docker container ls-a # / docker container ls-a #
  • Look at all containers that are already running
docker ps
  • Look at the container port map
docker port <container-name or container-id>
  • View Docker low-level information (e.g., view the IP address of the specified container)
Docker inspect <container-name or container-id> docker inspect <container-name or container-id> The container - id > | grep IPAddress # like see the containers with id 66204 be9fe65 container the IP address of the corresponding docker inspect 66204 be9fe65 | grep IPAddress # Such as to discover the IP address of the corresponding container name is alex docker inspect alex | grep IPAddress
  • Create the container and restore the image to the container, and start the container
Docker run [-i][-t][-v][--name][-d][-p] # -i = "terminal #" -t = "terminal # Runs as a daemon (does not enter the container by default, To access the container, you need to use the docker exec command) # -p for ** Specify ** mapping port # -p (capital P) for ** Random ** mapping port # /bin/bash and bash equivalent docker run-it <image-name> /bin/bash # For example, create a new container and enter Ubuntu :16.04 with docker run-it Ubuntu :16.04 bash # or docker run-it Ubuntu :16.04 bash # /bin/bash # or docker run-it Ubuntu :16.04 # create a new container with a centOS image And change the new container name to alx-container docker run-it --name # run docker run-d --name as daemon # / docker run-di/docker run-di/docker containerCentOS # / docker run-di/docker containerCentOS # / docker run-di/docker run-di/docker containerCentOS # Docker run-d-p 127.0.0.1:5001:5000 centos:8.0 bash # If you need to bind to the container, you can access port 5000 by accessing 127.0.0.1:5001 Docker run -d-p 127.0.0.1:5001:5000/udp centos:8.0 python app.py # docker run -d-p 127.0.0.1:5001:5000/udp centos:8.0 python app.py # Install the hyperf image and start the container # If the selinux-enabled option is enabled when Docker starts, access to host resources within the container will be limited. When start the container so can increase - ring -u root option docker run - name hyperf \ - v/workspace/skeleton: / data/project \ -p 9501:9501 - it \ --privileged -u root \ --entrypoint /bin/sh /hyperf /hyperf:7.4-alpine-v3.11-swoole # If you need to open more than one port, Can refer to docker run - name api_dfo_hyperf_ws \ - v/Users/pudongping/glory/codes/dfo/api_dfo_hyperf: / api_dfo_hyperf \ -p 9502:9502 \ -p 9503:9503 \ -p 9504:9504 \ -p 9505:9505 -it \ --entrypoint /bin/sh \ alex/alex_api_dfo:v1.0
  • Start the container
Docker start <container-name or container-id> # docker start redis-alex # docker start redis-alex # docker start redis-alex # docker start redis-alex # docker start redis-alex # docker start redis-alex # docker start redis-alex # C8C0C770AC5B Container Docker Start C8C0C770AC5B
  • Go directly to the already created container (without starting the container)
Docker start-i 66204be9fe65 docker start-i 66204be9fe65 docker start-i 66204be9fe65 docker start-i <container-name or container-id> # docker start-i 66204be9fe65 # docker start-i 66204be9fe65 # docker start-i <container-name or container-id> # Alex's container Docker Start-i Alex
  • Restart the container
Docker restart <container-name/container-id> # docker restart <container-name/container-id> # docker restart <container-name/container-id> # docker restart <container-name/container-id> # docker restart <container-name/container-id> # Container Restart C8C0C770AC5B Docker Restart C8C0C770AC5B
  • Enter the already running container
Docker exec-it redis-Alex bash # docker exec-it redis-Alex bash # docker exec-it redis-Alex bash # docker exec-it redis-Alex bash # C8c0c770ac5b bash # Execute the shell command or execute the shell script docker exec-it after entering the container with the id c8c0c770ac5b <container-name or container-id> /bin/sh -c "while true; do echo hello world; sleep 1; Done "# For example, if you go to a container with id c8c0c770ac5b, After entering the container, execute 'bash /portal_api_dfo_hyperf/server.sh restart' script docker exec-it c8c0c770ac5b /bin/ sh-c "bash /portal_api_dfo_hyperf/server.sh restart"
  • Stop the container
Docker stop > < the container - the name or the container - id # for example to stop the container name for redis - alex container docker stop alex # redis - such as stop container with id C8c0c770ac5b docker stop c8c0c770ac5b
  • Out of the container
exit
  • Remove the container
Docker rm <container-name or container-id> # docker rm <container-name or container-id> # docker rm <container-name or container-id> # Clear all docker container prune that is in the terminated state
  • Change the Container Name
Docker rename <container-name or container-id> < new-container-container-name > # redis-alex redis-tt
  • View the standard output of the container
Docker logs c8c0c770ac5b # docker logs c8c0c770ac5b # docker logs c8c0c770ac5b # docker logs c8c0c770ac5b # docker logs <container-name or container-id> # Parameters to output the standard output Docker logs-f c8c0c770ac5b inside the container as with tail-f

A copy of a file or directory between the container and the host

  • Copy files from the host to the container
Docker cp <local-directory-or-file> <container-name>:<container-directory-or-file> # /home/alex/test.txt Centos1 :/test.txt docker cp /home/alex/test.txt centos1:/test.txt
  • Copy from the container to the host
Docker cp <container-name>:<container-directory-or-file> <local-directory> # For example, copy the /test directory from centos1 to /home/alex Docker cp centos1:/test /home/alex

Directory mount (this is what you need to do when you create the container)

Docker run-di-v <local-directory>:<container-directory> <image-name> # And put the alex directory on the local d-disk, /usr/local/demo docker run-di-v d:\ Alex :/usr/local/demo centos And local/home/alex/alex, mounted to the container in the directory/usr/local/demo docker run - di - v/home/alex/alex: / usr/local/demo centos

Export and import containers

  • Export container snapshot
Tar docker export <container-id> > <your-backup-name.tar docker export < Alex.tar docker export <container-id> > <your-backup-name.tar docker export 7691a814370e > alex.tar
  • Importing the container snapshot
cat <your-backup-name.tar> | docker import - <image-author-name>/<your-new-image-name>:<your-new-image-version> # For example, import the container snapshot file alex.tar into the alx-demo image and define the author of the alx-demo image as Alex. The version number for v1.0 cat alex. Tar | docker import - alex/alex - demo: v1.0 # in addition, By specifying a url or a directory to import docker import example/image - repo: http://example.com/example-image.tgz v1.0

Original link address