Consolidation Docker common commands


Docker includes three basic concepts: Image, Container and Repository

Docker hello world

  • Output Hello world
docker run ubuntu:15.10 /bin/echo "Hello world"
  • Interactive dialogue ability
docker run -i -t ubuntu:15.10 /bin/bash
  • Start container (background mode)
docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
  • View the standard output in the container
docker logs <container ID/NAMES>
  • Stop container
docker stop <container ID/NAMES>

Docker container usage

  • Get image
docker pull ubuntu
  • Start container
docker run -it ubuntu /bin/bash
  • View running containers
docker ps
  • View all containers
docker ps -a
  • Start a stopped container
docker start <container ID/NAMES>
  • Background operation
docker run -itd --name ubuntu-test ubuntu /bin/bash
  • Stop container
docker stop <container ID/NAMES>
  • Restart stopped container
docker restart <container ID/NAMES>
  • Two ways to enter the container
docker attach <container ID/NAMES>                # Exit and the container stops
docker exec -it <container ID/NAMES> /bin/bash    # Exit, the container will not stop
  • Export container
docker export <container ID/NAMES> > ubuntu.tar
  • Import container
cat docker/ubuntu.tar | docker import - test/ubuntu:v1
  • Import by specifying a URL or a directory
docker import example/imagerepo
  • Delete container
docker rm -f <container ID/NAMES>

When deleting a container, the container must be in the stopped state, otherwise an error will be reported

  • Clean up all containers in the terminated state
docker container prune

Run a web application

  • Run WEB application
docker pull training/webapp  # Load image
docker run -d -P training/webapp python

-d: Let the container run in the background

-P: Randomly map the network ports used inside the container to the hosts we use

  • Use the - p parameter to set different ports
docker run -d -p 45154:5000 training/webapp python

Docker internal port 5000 is mapped to host port 45154

Docker opens 5000 ports (the default Python Flask port) to map to host port 32769

  • View container port mapping
docker port <container ID/NAMES>
  • View WEB application log
docker logs -f <container ID/NAMES>
  • View the process of WEB application container
docker top <container ID/NAMES>
  • Check WEB application (underlying information)
docker inspect <container ID/NAMES>

A JSON file will be returned, recording the configuration and status information of the Docker container

  • Query last created container
docker ps -l

Docker image usage

When running the container, if the image used does not exist locally, docker will automatically download it from the docker image warehouse. By default, it is downloaded from the Docker Hub public image source

Manage mirroring

  • Lists the mirrors on the local host
docker images
  • Get a new image
docker pull ubuntu:13.10
  • Find mirror
docker search httpd
  • Use this image
docker run httpd
  • delete mirror
docker rmi httpd

create mirror

When the image we downloaded from the docker image warehouse cannot meet our needs, we can change the image in the following two ways:

1. Update the image from the container that has been created and commit the image

2. Use the Dockerfile directive to create a new image

Update mirror

  • Before updating the image, we need to use the image to create a container
docker run -t -i ubuntu:15.10 /bin/bash
  • Submit container copy
docker commit -m="has update" -a="runoob" <container ID/NAMES> runoob/ubuntu:v2
  • Start a container with the new image runoob/ubuntu
docker run -t -i runoob/ubuntu:v2 /bin/bash

Build mirror

  • Create a Dockerfile file that contains a set of instructions to tell Docker how to build our image
FROM    centos:6.7
MAINTAINER      Fisher ""

RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd runoob
RUN     /bin/echo 'runoob:123456' |chpasswd
RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
CMD     /usr/sbin/sshd -D
  • Use the Dockerfile file and use the docker build command to build an image
docker build -t runoob/centos:6.7 .
  • Create a container with a new image
docker run -t -i runoob/centos:6.7 /bin/bash
  • Add a new label for the mirror
docker tag <image ID> runoob/centos:dev

Docker container connection

  • Network port mapping
docker run -d -P training/webapp python
docker run -d -p 5000:5000 training/webapp python

-P: Is the high port that the internal port of the container is randomly mapped to the host

-p: Is the container's internal port bound to the specified host port

  • Specifies the network address to which the container is bound
docker run -d -p training/webapp python
  • The default is to bind tcp ports. If you want to bind udp ports, you can add / udp after the ports
docker run -d -p training/webapp python

Container interconnection

docker has a connection system that allows multiple containers to be connected together and share connection information

docker connection will create a parent-child relationship, where the parent container can see the information of the child container

  • Container naming

When creating a container, docker will automatically name it

In addition, we can also use the -- name ID to name the container

docker run -d -P --name runoob training/webapp python
  • New network
docker network create -d bridge test-net    # Create a new Docker network
  • View docker network
docker network ls
  • Connection container

Run a container and connect to the new test net network:

docker run -itd --name test1 --network test-net ubuntu /bin/bash

Open a new terminal, run a container and join the test net network:

docker run -itd --name test2 --network test-net ubuntu /bin/bash

Enter the following command in the test1 container

docker exec -it test1 /bin/bash
ping test2

Enter the following command in the test2 container

docker exec -it test2 /bin/bash
ping test1

Configure DNS

  • Check whether the DNS of the container is in effect
docker run -it --rm  ubuntu  cat etc/resolv.conf
  • Manually specify the configuration of the container
docker run -it --rm -h host_ubuntu  --dns= ubuntu

Docker warehouse management

  • Log in to docker hub
docker login
  • Exit docker hub
docker logout
  • Retrieve image
docker search ubuntu
  • Pull image
docker pull ubuntu
  • Push image
docker push username/ubuntu:18.04

Docker Dockerfile

A text file used to build an image. The text content contains instructions and instructions required to build an image

  • Dockerfile file
FROM nginx
RUN echo 'This is a locally built nginx image' > /usr/share/nginx/html/index.html
  • Start building mirror
docker build -t nginx:v3 .
  • Context path

Context path means that docker sometimes wants to use local files (such as copying) when building an image. After knowing this path, the docker build command will package all the contents under the path

Common instructions

  • FROM

Customized images are all FROM based images

  • RUN

Used to execute the following command line commands

  • COPY

Copy instruction, copy files or directories from the context directory to the specified path in the container

  • ADD

It will be automatically copied and decompressed to < target path >, and the tar compressed file cannot be copied without decompressing

Compared with ADD, COPY is recommended

  • CMD

Similar to the RUN instruction, it is used to RUN programs

CMD runs when docker run s

RUN runs when docker build s


It is similar to the CMD instruction, but it will not be overwritten by the instruction specified by the command line parameter of docker run

  • ENV

Setting environment variables

  • ARG

The environment variable set by ARG is only valid in Dockerfile


Define anonymous data volumes to avoid data loss and container size


Just declare the port to facilitate configuration mapping


assign work directory

  • USER

It is used to specify the user and user group to execute subsequent commands. Here, only the user to switch the execution of subsequent commands (the user and user group must already exist in advance)


It is used to specify a program or instruction to monitor the running status of docker container service


Used to delay the execution of build commands


Add some metadata to the image in the form of key value pairs

Docker Compose

A tool for defining and running multi container Docker applications that creates and starts all services from the YML file configuration

Docker Machine

Swarm cluster management

Project record

  • Dockerfile
FROM python:3.9.6
ADD ./src /usr/src/app/code
WORKDIR /usr/src/app/code
RUN pip install -r requirements.txt
CMD ["python", "/usr/src/app/code/"]
  • Build mirror
docker build -t my_image .
  • Start container
docker run -it -p 49153:5000 --name mic my_image


docker run -it -p 49153:5000 --name mic my_image /bin/bash

You can also enter the container

docker exec -it mic /bin/bash

Tags: Docker

Posted on Tue, 14 Sep 2021 18:03:35 -0400 by katlis