[docker series] docker learning II, common commands of docker

Posted by ofi on Sat, 30 Oct 2021 02:59:15 +0200

[docker series] docker learning II, common commands of docker

Basic help commands

# View the basic version information of docker
docker version

# View the system information of docker, such as the number of images and containers
docker info

# View help for a command
docker xx command --help

We can see the official help document: https://docs.docker.com/reference/

Mirror command

docker images view images

Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]

View images on this machine

# docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
ubuntu            latest    1318b700e415   5 days ago     72.8MB
hello-world       latest    d1165f221234   4 months ago   13.3kB
keyword explain
REPOSITORY Warehouse source
TAG Mirror label
IMAGE ID Mirror ID
CREATED Creation time
SIZE Mirror size

Optional parameters:

Options:
  -a, --all             Show all mirrors
  -q, --quiet           Show only mirrors ID

docker search search image

Search redis as an example

# docker search redis
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
redis                            Redis is an open source key-value store that...   9734      [OK]
sameersbn/redis                                                                  83                   [OK]
grokzen/redis-cluster            Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2      78
rediscommander/redis-commander   Alpine image for redis-commander - Redis man...   63                   [OK]

Add parameters

Filter images with STARS greater than 2000

# docker search redis -f STARS=2000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
redis     Redis is an open source key-value store that...   9734      [OK]

We can also search for images directly on the dockerhub page

The search results are consistent with the results of the command search

docker pull Download Image

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Download the redis image as an example

# docker pull redis
Using default tag: latest			# Default pull latest version
latest: Pulling from library/redis	# redis Library
33847f680f63: Pull complete			# Layered Download
26a746039521: Pull complete
18d87da94363: Pull complete
5e118a708802: Pull complete
ecf0dbe7c357: Pull complete
46f280ba52da: Pull complete
Digest: sha256:cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59									# autograph
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest		# redis real download address

Therefore, the above download operation
docker pull redis is consistent with docker pull docker.io/library/redis:latest

You can view the version supported by redis on dockerhub

We download a 6 version of redis

# docker pull redis:6
6: Pulling from library/redis
Digest: sha256:cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59
Status: Downloaded newer image for redis:6
docker.io/library/redis:6

You can see that when you download version 6 redis, there is no layered download, which indicates that they share the layered download seen above

View the image of the installation just now

docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
redis             6         aa4d65e670d6   8 days ago     105MB
redis             latest    aa4d65e670d6   8 days ago     105MB
hello-world       latest    d1165f221234   4 months ago   13.3kB

docker rmi delete image

  • Delete a single mirror
docker rmi -f container ID
  • Delete multiple mirrors
docker rmi -f container ID container ID container ID 
  • Delete all mirrors
docker rmi -f $(docker images -q)

Container command

Containers are created based on images. Let's download a ubuntu image

docker pull ubuntu

docker run creates and starts a container

docker run [parameter] image name [instruction] [parameter]

Description of common parameters:

--name="xxx"	# Name of the running container
-d				# Run in background mode
-it				# Run interactively
-p				# Specify the port number of the container, for example -p 6379:6380
				Common operations are -p Host port:Container port
-P				# Randomly specify the port number

Start the ubuntu in the container. We can see the host switching through the host name

root@iZuf66y3tuzn4wp3h02t7pZ:~# docker run -it ubuntu /bin/bash
root@87fb04e2a6e7:/#

Exit container

  • Type the exit command and the container exits
  • Use the shortcut keys Ctrl + P + Q to return to the host, and the container will not exit

docker ps viewing containers

docker ps [OPTIONS]

# docker ps -a
CONTAINER ID   IMAGE          COMMAND           CREATED          STATUS                      PORTS                     NAMES
73f951b70438   ubuntu         "/bin/bash"       2 minutes ago    Up 2 minutes                                          vigorous_buck
87fb04e2a6e7   ubuntu         "/bin/bash"       7 minutes ago    Exited (0) 3 minutes ago                              flamboyant_tu

Optional parameters:

			# View running containers
-a 			# View containers that have been run
-n=xx		# View the first xx containers that have been run
-s			# View the size of the container run
-q			# View container ID

docker rm delete container

docker rm container ID		# Delete non running containers
docker rm -f container ID	# Force deletion of running containers
docker rm -f $(docker ps -aq)		# Delete all containers
docker ps -aq | xargs docker rm 	# Delete all containers

Start, restart, stop, kill start, restart, stop, forcibly stop the container

docker start container ID
docker restart container ID
docker stop container ID
docker kill container ID

Common other commands

docker run -d background start container

# Start a ubuntu in the background
docker run -d ubuntu

# View running containers
docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES

No containers found running

The reasons are as follows:

  • When docker starts the service in the background, there needs to be a foreground process. Otherwise, if docker finds no application, it will stop the service

We take the initiative to add a foreground process to see the effect

# Temporarily add a foreground process
docker run -d ubuntu /bin/bash -c "while true;do echo xiaozhupeiqi;sleep 2;done"

# View running containers
docker ps
CONTAINER ID   IMAGE     COMMAND
10ba0e687434   ubuntu    "/bin/bash -c 'while..." 

It can be seen that the docker ps command can view the running container, OK

docker logs view logs

docker logs [parameter] container ID

Options:
  -f		# Consistent with output
  -n		# Output recent lines
  -t		# Print timestamp

View the log of the above container

# docker logs -tf -n 5 10ba0e687434
2021-08-01T08:02:51.380512218Z xiaozhupeiqi
2021-08-01T08:02:53.381606198Z xiaozhupeiqi
2021-08-01T08:02:55.382780869Z xiaozhupeiqi
2021-08-01T08:02:57.383890580Z xiaozhupeiqi
2021-08-01T08:02:59.384977473Z xiaozhupeiqi
2021-08-01T08:03:01.386430484Z xiaozhupeiqi

docker top view the process information in the container

docker top container ID

# docker top 10ba0e68743
UID                 PID                 PPID  		CMD
root                11101               11073  		bin/bash -c while true;do echo xiaozhupeiqi;sleep 2;done
root                11504               11101

docker inspect view image metadata

docker inspect container ID

A large amount of information is omitted from the output information

[
 	{
        "Id": "10ba0e6874341b2e2f002c22613a71223ca981dc36df0d1ea4ed3bb5a7a6c58e",
        "Created": "2021-08-01T07:57:52.725305443Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo xiaozhupeiqi;sleep 2;done"
        ],
        "State": {
            ...
        },
       ...
        "GraphDriver": {
      ...
        },
        "Mounts": [],
        "Config": {
            "Hostname": "10ba0e687434",
            ...
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash",
                "-c",
                "while true;do echo xiaozhupeiqi;sleep 2;done"
            ],
            "Image": "ubuntu",
            ...
        },
        "NetworkSettings": {
            ...
                }
    }
]

docker exec enters the currently running container

docker exec [parameter] container ID instruction [parameter of instruction]

# docker exec -it 10ba0e687434 /bin/bash
root@10ba0e687434:/# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 08:04 ?        00:00:00 /bin/bash -c while true;do echo xiaozhupeiqi;sleep 2;done
root       922     0  0 08:34 pts/0    00:00:00 /bin/bash
root       963     0  0 08:35 pts/1    00:00:00 /bin/bash
root       972     1  0 08:35 ?        00:00:00 sleep 2
root       973   963  0 08:35 pts/1    00:00:00 ps -ef

docker attach enters the executing program in the container

docker attach container ID

docker attach 10ba0e687434

The difference between docker exec and docker attach

  • docker exec

After entering the container, a new terminal will be opened and can operate normally

  • docker attach

Entering the terminal where the container is executing will not start a new terminal

docker cp copies the files in the container to the host

Docker CP container ID: container file path host path

# Enter the container and create a file xiaomotong.go in the / home directory of the container
 docker exec -it 10ba0e687434 /bin/bash
 cd /home/
 touch xiaomotong.go
# Ctrl + P + Q exit container

# Copy the files in the container to the host
docker cp 10ba0e687434:/home/xiaomotong.go ./

docker stats view service memory status in docker

# docker stats
CONTAINER ID   NAME          CPU %     MEM USAGE / LIMIT    MEM %     NET I/O           BLOCK I/O     PIDS
2772a4050157   nginx1        0.00%     3.02MiB / 1.946GiB   0.15%     25.8kB / 33.1kB   0B / 8.19kB   3
10ba0e687434   loving_bohr   0.00%     4.07MiB / 1.946GiB   0.20%     810B / 0B         1.95MB / 0B   5

summary

There are still a lot of docker commands. You can try to sort out the list of common commands according to the following figure for easy query. The following pictures are from the network and are only for learning

reference material:

docker docs

Welcome to like, follow and collect

My friends, your support and encouragement are the driving force for me to insist on sharing and improve quality

Well, that's all for this time

Technology is open, and our mentality should be open. Embrace change, live in the sun and strive to move forward.

I'm Nezha, the Little Devil boy. Welcome to praise and pay attention to the collection. See you next time~