docker common commands

Keywords: Linux Docker Container

What is docker

Docker's idea comes from the container. What problem does the container solve? In a large ship, the goods can be placed neatly. And all kinds of goods are standardized by containers, and containers will not affect each other. Then I don't need a ship for fruit and a ship for chemicals. As long as the goods are well sealed in the container, I can take them all away in a big ship.

Some people say that docker is a container, while others say that docker is not a container. In my opinion, docker is more like a container for loading containers;, A large ship is a container, and then each container is a small container; Let's look at the docker icon;

The most important is the concept of image layering; At the bottom is the operating system. The penultimate layer is the basic image. In the middle is the service installed on the basic image, and at the top is the container layer; At first glance, it looks like containers are nested;

After introducing docker, let's see what commands docker has!

Start and stop docker

# start-up       
 systemctl start docker
 
# Daemon restart  
 sudo systemctl daemon-reload
 
# Restart docker service   
systemctl restart  docker

# Restart docker service  
sudo service docker restart

# Close docker   
service docker stop   

# Close docker  
systemctl stop docker

Help command

docker version # View version information
docker info    # Displays the system information of docker, including the number of images and containers
docker command --help # Help command

-aq represents a set of - all [all] and - quiet [display id ONLY]

Mirror command

[root@VM_0_5_centos ~]# docker images   # View all mirrors
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    feb5d9fea6a5   4 weeks ago   13.3kB
# explain
  REPOSITORY  Mirrored warehouse source
  TAG         Mirrored label
  IMAGE ID    image id
  CREATED     Creation time of the image
  SIZE        Mirror size

docker images -q # Show ID ONLY

Search image docker search

docker search  mysql   # Search mysql image
docker search  mysql --filter=stars=3000  search mysql More than 3000 image collections

Download Image docker pull

Download images can also be queried through the website: https://hub.docker.com/_/redis

[root@VM_0_5_centos ~]# docker pull redis   # Download the redis image. The latest version is downloaded by default
Using default tag: latest
latest: Pulling from library/redis  
7d63c13d9b9b: Pull complete    # Layered Download: if you have downloaded the same file before, you can share it. Divide a file into several files, similar to rar volume compression
a2c3b174c5ad: Pull complete 
283a10257b0f: Pull complete 
7a08c63a873a: Pull complete 
0531663a7f55: Pull complete 
9bf50efb265c: Pull complete 
Digest: sha256:a89cb097693dd354de598d279c304a1c73ee550fbfff6d9ee515568e0c749cfe  # autograph
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest  # The real address docker pull redis is the same as docker pull docker.io/library/redis:latest

docker pull redis:5.0  # Download the specified version of redis

Delete image docker rmi

docker rmi -f $(docker images -aq) Delete all mirrors

docker rmi -f 02fee89f17ad  # Deletes the image with the specified id

View mirror construction history

docker history image id

Container command

explain

A container can only be created with an image; So download a centos system first

docker pull centos

Create a new container and start

# Create a container based on the image and start the container into the container 
docker run  --name contos_3 -p 9090:80 -it  centos /bin/bash

# Parameter description
--name cname  The name of the container, if not configured, docker Will automatically give you a name
-d  Run in background mode
-it  Run interactively
-p (a lowercase letter p)Specifies the port of the container
   -p ip:Host port:Container port
   -p Host port:Container port
   -p  Container port
   Container port
-P (Capitalize P)Random mapping port  
/bin/bash After entering the container, open a new terminal

Delete after use -- rm

When we finish the test, there is no problem. At this time, we need to delete the container. It will be troublesome to delete it manually every time, so docker gives a parameter - rm, which can delete the container when stopping the container. The usage is as follows

# The official usage of tomcat, which is generally used on Tomcat
docker run -d --rm centos 

# When you stop docker by running the following command, the container will be deleted
docker rm -f container id/Container name

Common pit

When running in the background through the following commands, it is found that centos is not lucky. This is because to make the container run in the background, there must be a foreground process. If docker finds that there is no application, it will automatically stop; Like nginx, when the container starts and finds that it does not provide services, it will stop immediately;

# This command stops the container
dockers run  -d  centos  

# The following command does not stop the container because there is a foreground process
dockers run  -d -it centos  

How to connect overnight hosts and containers (flowchart)
When creating a new container, there is a very important parameter - p 8080:80. This parameter is used by the overnight host and container. That is, when we access localhost:8080 externally, we actually access port 80 of the container. The complete command for creating a new container is as follows

docker run -p 9090:80  centos 

Open multiple ports

A server like tomcat has three open ports, namely 8080, 8005 and 8009. What should we do if we want to open these ports? In fact, it is very simple. You only need to configure multiple - PS

docker run -d -p 8080:8080 -p 8005:8005 tomcat

Exit container

eixt   # Note: This is to stop the container directly

ctrl + P + Q  Shortcut keys can return to the main system, but do not exit the container

Start or stop the container

docker start container id     # To start a container, you can only start a container that has already been created

docker restart container id/Container name  # Restart container

docker stop container id/Container name   # Stop container

docker kill Allow id/Container name   # Force stop container




View all containers

docker ps -a # View the containers that have been run- a means - all (all)

View running containers

docker ps # View all running containers

Enter a running container

# Method 1: open a new terminal after entering the container
docker exec -it container id/Container name /bin/bash


# Mode 2: use the executing terminal after entering the container
docker attach container id/Container name

Delete container

It should be noted that,

docker rm container id/Container name  # Delete the specified container. You cannot delete a running container

docker rm -f container id/Container name  # Deleting a specified container can also delete a running container

docker rm  $(docker ps -aq)  # Delete all containers. You cannot delete a running container

docker rm -f $(docker ps -aq)  # Delete all containers, including running containers

view log

docker logs -f -t --tail 10  container id

# explain
    -f Tracking log output will always display the latest log content; and tail -f Is the same;
    -t Print timestamp
    --tail 10  Only 10 lines are displayed

Viewing processes in a container

docker top container id/Container name

View metadata for container

docker inspect container id/Container name

Copy the files in the container to the host

docker cp container id/Container name:In container host path

View container status command

docker stats

The display contents are as follows

CONTAINER ID   NAME       CPU %     MEM USAGE / LIMIT     MEM %     NET I/O          BLOCK I/O   PIDS
c75c62eff5f6   tomcat_1   0.16%     126.3MiB / 1.796GiB   6.87%     28.4kB / 233kB   20MB / 0B   29

# explain
  CONTAINER ID container id
  NAME         Container name
  CPU %        CPU Utilization rate
  MEM USAGE    How much memory is used
  LIMIT        Total memory
  MEM %        Memory usage, showing memory usage as a percentage
  NET I/O      network I/O data
  BLOCK I/O    disk I/O data
  PIDS         process id

Commit image commit the current container generates a new image

docker commit -a="author" -m="explain"  -p container id/Container name mirror new name:Mirror version

# For example, make the current tomcat container to generate an image
docker commit -a="yexindong" -m="First made image" tomcat_1 mytomcat:1.0

# Parameter description
    -a :Submitted image author;
    -c :use Dockerfile Command to create an image;
    -m :Explanatory text at the time of submission;
    -p :stay commit Pause the container when.

Mount - v

After running the following command, the local directory will be mounted on the container directory, the corresponding directory will be automatically created on the container and host computer, and the files created or modified under the directory will be automatically synchronized;

docker run -d -v Host Directory:Container directory tomcat 

If you need to mount multiple directories, you only need to configure multiple - v

docker run -d -v Host Directory:Container directory -v Host Directory:Container directory tomcat 

Configure container environment variable - e

e is the abbreviation of environment,
The following is how to set the initial password for mysql: 123456

docker run -d -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

The first way: you can view the environment variables that have been configured in the container in the following ways

docker exec "Container name or container id" env

The second method: check through inspect and find that the Env item is the environment variable- A 10 means to print the last 10 lines

docker inspect container id/Container name | grep Env -A 10

give the result as follows

"Env": [
    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],

Image saving and loading

If you need to send your own image to others and don't want to share it through the warehouse, you can use the function of image saving,

Mirror save

First, we save the image to the local disk as a file

docker save image id/Image name -o /Local directory/File name
# Like this
docker save ad8a585d32e3 -o /root/dockerFile/xx.tar
# After running, you can see that the file has been saved successfully
[root@VM_0_5_centos dockerFile]# ll
total 356440
-rw------- 1 root root 364991488 Oct 28 18:45 xx.tar

Load saved image

Use the load command to load and see the following contents, indicating that the image has been loaded successfully

[root@VM_0_5_centos dockerFile]# docker load -i /root/dockerFile/xx.tar 
2fefb3e7f055: Loading layer [==================================================>]   47.6MB/47.6MB
8b589f61fd94: Loading layer [==================================================>]  58.82MB/58.82MB
753a6b1d88c8: Loading layer [==================================================>]  19.99MB/19.99MB
Loaded image ID: sha256:ad8a585d32e34b301aab4b278704a908f6eca53884fd531a79859dd6054a22ae

Next, let's see that the image has been loaded, but it hasn't been named yet

[root@VM_0_5_centos dockerFile]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
<none>       <none>    ad8a585d32e3   2 hours ago   356MB

Next, use the tag command to give the image a name and version number

[root@VM_0_5_centos dockerFile]# docker tag ad8a585d32e3 xindong_tomcat_2:1.0

# Looking at the image, it already has a name
[root@VM_0_5_centos dockerFile]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED       SIZE
xindong_tomcat_2   1.0       ad8a585d32e3   2 hours ago   356MB

Execute commands within the container on the host

docker exec -it container id/Container name command to execute

# For example, I want to view the network card in the container
docker exec -it tomcat_01  ip addr

Network command network

View all local docker networks

docker network ls

Create a network - driver is the network mode, - subnet is the subnet mask, followed by / 16 indicates that more than 60000 IPS can be generated, and - gateway is the gateway address

docker network create Network name --driver bridge --subnet 192.168.0.1/16 --gateway 192.168.0.2 mynet

Join container to current network

docker network connect Network name container name

Disconnect the network of the container (the container must be running to disconnect)

docker network disconnect Network name container name

View network details

docker network inspect network id/Network name

Delete network

docker network rm network id/Network name

Delete all unused networks

docker network prune --f

Posted by sdm on Sun, 31 Oct 2021 18:07:11 -0700