Quick view of common Docker commands

Posted by devarishi on Sun, 19 Dec 2021 01:54:31 +0100

Docker installation

  1. Uninstall the old version. The older docker version is docker or docker engine. If you have installed these programs, uninstall them and their related dependencies.

    sudo yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-engine
    
  2. Set the yum source. Install the yum utils package (the yum config manager program is provided) and set a stable Yum source to facilitate downloading Docker Engine.

    # Install Yum utils
    sudo yum install -y yum-utils
    # Set the yum source to alicloud for easy download of Docker Engine
    sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
  3. Install the latest version of Docker Engin and container. Manually y multiple times during

    sudo yum install docker-ce docker-ce-cli containerd.io
    
  4. Verify installation

    docker -v
    docker version
    
  5. Configure mirroring acceleration. Docker pulls images from Docker Hub, which is slow because it is obtained from abroad. You can obtain images from China by configuring domestic image sources to improve the pull speed.

    vi /etc/docker/daemon.json
    
    # Enter the following in the file
    {
      "registry-mirrors": ["http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn"]
    }
    
    # Reload the configuration file for a service
    sudo systemctl daemon-reload
    # Restart docker
    sudo systemctl restart docker
    

Docker start stop

# Start / stop docker
systemctl start/stop docker

# Restart docker
systemctl restart docker

# Set boot / disable boot
systemctl enable/disable docker

# View docker status
systemctl status docker

# View the running status of docker content manager
docker stats

# View docker profile
docker info

# View docker help documentation
docker --help

Mirror operation

# Search image
docker search Image name

# Pull image
docker pull Image name

# View the existing image on this machine
docker images

# Delete a single mirror
docker rmi image ID

# You can also delete multiple mirrors
docker rmi image ID image ID image ID

# Delete all images. docker images -q can query the ID s of all images. You can delete all images by combining them
docker rmi `docker images -q`

Container operation

# View containers, [- a]: view all containers, and only the running containers
docker ps -a

# Stop container
docker stop Container name|container ID

# Start container
docker start Container name|container ID

# Delete single or multiple containers
docker rm Container name|container ID Container name|container ID

# View container IP
docker inspect Container name|container ID

Creating and launching containers

# The following command means to create a container through the specified image, run the container and enter the ` / bin/bash of the container`
docker run -it --name Container name mirror name:label /bin/bash

# Creating containers in a daemon mode
docker run -di --name Container name mirror name:label

# Login daemon container mode
docker exec -it Container name|container ID /bin/bash

# Exit current container
exit
  • i: Indicates the running container;
  • t: Indicates that the container will enter its command line after it is started. After adding these two parameters, the container creation can log in. That is, assign a pseudo terminal;
  • -Name: name the created container;
  • v: Indicates the directory mapping relationship (the former is the host directory, and the latter is the directory mapped to the host). You can use multiple -v to map multiple directories or files. Note: it is best to do directory mapping, modify it on the host, and then share it on the container;
  • d: If you add the - D parameter after run, a daemon container will be created to run in the background (in this way, the container will not be logged in automatically after the container is created. If you only add the - i -t parameter, it will be automatically entered into the container after the container is created);
  • p: Indicates port mapping. The former is the host port and the latter is the mapped port in the container. You can use multiple - P for multiple port mapping.
  • P: Randomly use the mapping between the available ports of the host and the exposed ports in the container.

View container log

docker logs container id

File copy

# Copy files to container
docker cp The name of the file or directory container that needs to be copied:Container directory

# Copy the file out of the container
docker cp Container name:Container directory the file or directory that needs to be copied

Directory mount

docker run -di -v /mydata/docker_centos/data:/usr/local/data --name centos7-01 centos:7

# Multi directory mount
docker run -di -v /Host Directory:/Container directory -v /Host Directory 2:/Container directory 2 image name

MySQL

# Pull image
docker pull mysql

# Create container
docker run -di --name mysql8 -p 3306:3306 -v /mydata/docker_mysql/conf:/etc/mysql/conf.d -v /mydata/docker_mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 mysql

# Enter container
docker exec -it mysql8 /bin/bash

# Open the client with MySQL command
mysql -uroot -p123 --default-character-set=utf8
  • p: Represents port mapping. The format is host mapping port: container running port.
  • e: Represents adding the environment variable MYSQL_ROOT_PASSWORD is the login password of root user.

Redis

# Pull image
docker pull redis

# Create container
docker run -di --name redis -p 6379:6379 redis

When connecting Redis in the container, you only need to connect the IP of the host + the specified mapping port.

MongoDB

# Pull image
docker pull mongo

# Create container
docker run -di --name mongo -p 27017:27017 mongo

When connecting MongoDB in the container, you only need to connect the IP of the host + the specified mapping port.

RabbitMQ

# Pull image
docker pull rabbitmq

# Create container
docker run -di --name rabbitmq -p 4369:4369 -p 5671:5671 -p 5672:5672 -p 15671:15671 -p 15672:15672 -p 25672:25672 rabbitmq

Enter the container and turn on the management function.

# Enter container
docker exec -it rabbitmq /bin/bash

# Enable RabbitMQ management function
rabbitmq-plugins enable rabbitmq_management

visit: http://ip:15672/ You can open the RabbitMQ management page and log in with the guest login account and password (all guests).

Nginx

# Pull image
docker pull nginx

# Create Nginx container
docker run -di --name nginx -p 80:80 nginx

Copy the configuration file in the container to the specified directory (please create the directory in advance).

# Create directory
mkdir -p /mydata/docker_nginx

# Copy the configuration file in the container to the specified directory
docker cp nginx:/etc/nginx /mydata/docker_nginx/

Rename host / mydata/docker_nginx/nginx is / mydata/docker_nginx/conf

mv /mydata/docker_nginx/nginx/ /mydata/docker_nginx/conf

Terminate and delete the container because the mount directory needs to be set, and the directory mount operation can only be set when the container is created.

docker stop nginx
docker rm nginx

Create the Nginx container and connect the / etc/nginx directory in the container to / mydata / docker of the host_ Mount in Nginx / conf directory.

docker run -di --name nginx -p 80:80 -v /mydata/docker_nginx/conf:/etc/nginx nginx

Access host: http://ip:80/ You can open Welcome to nginx! Page!

Topics: Linux Docker Container