[TOC]

The resources

Docker official website docker official documentation rookie tutorial

Docker, as a lightweight and high-performance sandbox container, is used very frequently and has very powerful functions.

Powerful functions need multifarious commands to support, although there are many docker commands, too many to remember.

A good memory is not as good as a bad pen. This paper summarizes the commonly used commands of Docker, and gives explanations and examples for each command, which can be taken at will

A mirror warehouse is used to store images. It can be divided into remote mirror warehouses and local mirror warehouses.

You can use the pull command to download the image of the remote warehouse to the local PC, and use the push command to push the image of the local PC to the remote PC

Images in the local repository can be used to create containers, and one image can create multiple containers

Containers can also be packaged as images with the commit command and committed to a local repository

The command to operate the remote warehouse

Login: Logs in to the remote repository

You can use the login command to login to the remote repository. After logging in to the remote repository, you can pull the image of the repository

The login grammar

docker login [OPTIONS] [SERVER]
Copy the code

SERVER: specifies the address of the remote repository. The default address is hub.docker.com/

Common values of OPTIONS

-u string: indicates the user name. -p string: indicates the password

Login: login to the remote warehouse as user helianxiaowu. The password is 123456

docker login -u helianxiaowu -p 123456 192.16810.10./docker-lib
Copy the code

If you do not specify a user to log in to the remote repository, you will be prompted for a user name or password

docker login 192.16810.10./docker-lib
Copy the code

You will be prompted for a user name or password without specifying the user to log in to the default remote repository

docker login
Copy the code

Search: searches for images from the remote repository

The search command searches for images from remote repositories

  • Column meanings are as follows
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
Name of the mirror Image description Mirror heat, similar to Github’s starts Is it official? Automatic build or not

Search syntax

docker search [OPTIONS] TERM
Copy the code

OPTIONS: Optional parameter TERM: indicates the keyword of the mirror

Common values of OPTIONS

-f filter: indicates that mirrors are filtered according to the filtering conditions. For details about the filtering conditions, see the following no-trunc: indicates that complete image descriptions are displayed. — Limit int: Indicates the maximum number of mirrors to be searched. The default number is 25. — Format string: Indicates the maximum number of mirrors to be searched. Specifies the format for the image display, as described below

  • -fParameter Indicates the image that is filtered by conditions. The syntax is as follows

docker search -f KEY=VALUE TERM

  • The possible values of KEY are as follows

Stars int: filter by heat, for example, if stars=10, the image whose heat is greater than 10 is automated Boolean Is-is Automated = False Filters images that are not automatically constructed. Is-official Boolean: Filters images that are not automatically constructed. Is-official =false Filters images that are not automatically constructed

  • --formatParameter is used to specify the format of the image to be displayed.tableDisplay in a table. Supported\tformat

docker search –format “[table] {{COLUMN}}[{{COLUMN}}…] ” TERM

  • The optional values for COLUMN are as follows:

Description: indicates the Description of the image.StarCount: indicates the popularity of the image.IsOfficial: indicates whether an image is officially released

Search Searches for a centos image

docker search centos
Copy the code

Search for centos images, only 5 are displayed

docker search --limit 5 centos
Copy the code

Search for a centos image whose heat is greater than 100 and is not automatically built

docker search -f stars=100 -f is-automated=true centos
Copy the code

Search unofficial centos images. The search results show only the name and popularity, separated by the TAB key

docker search -f is-official=false --format "table{{.Name}}\t{{.StarCount}}" centos
Copy the code

Push: Pushes the local image to the remote repository

Push can push images from a local repository to a remote repository, but you need to log in to the remote repository first

Push the usage

docker push [OPTIONS] NAME[:TAG]
Copy the code

OPTIONS: Optional parameters NAME: image NAME TAG: image version, which can be omitted. The default value is latest

Common values of OPTIONS

–disable-content-trust: the remote repository does not verify signatures during push. The default value is true

Push pushes version 1.1.0 of a my-image image to a remote repository

docker push my-image:1.1. 0
Copy the code

Push the my-image image to the remote repository. If the version is not specified, the latest version is default

docker push my-image
Copy the code

Pull: Pulls or updates an image from a remote repository

The pull command can pull an image from a remote repository and update it if it already exists in the local repository

Pull the grammar

docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Copy the code

TAG: latest DIGEST: indicates the DIGEST of an image. Each image has its own NAME, ID, and DIGEST information. Each DIGEST uniquely represents an image, as shown in the following figure

Common values of OPTIONS

–disable-content-trust: indicates that the remote repository does not verify the signature during push. The default value is true. -q: indicates the quiet mode, and no detailed information is displayed during push

Pull a centos image from a remote repository. If the version is not specified, the latest version is used by default

docker pull centos
Copy the code

Pull the centos image 5.11 from the remote repository in quiet mode

docker pull -q centos:5.11
Copy the code

Pull all centos images from the remote repository in quiet mode

docker pull -a -q centos
Copy the code

Command for operating a local mirror

Images: Displays all images

The images command displays all images that exist locally

  • Column meanings are as follows
REPOSITORY TAG IMAGE ID CREATED SIZE
Warehouse path The mirror version Mirror id Creation time Image size

Images of grammar

docker images [OPTIONS] [REPOSITORY[:TAG]]
Copy the code

OPTIONS: Optional parameters REPOSITORY: image path TAG: image version

Common values of OPTIONS

-a: displays all images, including intermediate images. (Intermediate images are hidden by default.) -f filter: filters images based on the conditions described in the following. By default, only the first 12 digits of the image ID are displayed. The no-trunc parameter displays the full image ID. — Digests: indicates the summary information about the image

  • -fParameter Indicates that the mirror to be displayed is filtered based on conditions. The syntax is as follows

docker images -f KEY=VALUE [REPOSITORY[:TAG]]

  • The possible values of KEY are as follows

Dangling Boolean. For example, dangling=true Specifies that only the dangling image is displayed. For example, label=version indicates that an image with the version label is displayed. Label =version=1.0 indicates that an image with version=1.0 is displayed. Before image: Before =centos:5.8 Displays images created before centos:5.8. Since image: Since =centos:5.8 Indicates the image created after centos:5.8 exists. Reference =cent*:5* : displays images whose names start with cent and version numbers start with 5

  • --formatThe following syntax is used to specify the image display format.tableDisplay in a table. Supported\tformat

docker images –format “[table] {{COLUMN}}[{{COLUMN}}…] ” [REPOSITORY[:TAG]]

  • The optional values for COLUMN are as follows:

Tag: shows the popularity of the image.Digest: shows whether the image is officially published.CreatedSince: shows whether the image is automatically built. Size: displays whether the mirror automatically builds a column

Images Displays all local images

docker images 
Copy the code

Displays all local mirrors, showing only the ID column and not truncating

docker images -q --no-trunc
Copy the code

The centos image information is displayed

docker images centos
Copy the code

Displays all mirrors that contain the cent keyword in the column

docker images | grep cent
Copy the code

Displays all local images, and displays the summary column

docker images --digests
Copy the code

Displays all latest versions of mirrors created after Cengos: Latest mirrors

docker images -f since=centos:latest -f reference=*:latest
Copy the code

All image information is displayed in the image ID column, summary column, and creation time column, separated by the TAB key

docker images --format "table {{.ID}}\t{{.Digest}}\t{{.CreatedAt}}"
Copy the code

The images created before centos:5.11 Images are displayed. Only the image repository path, version number, and creation time are displayed. The columns are separated by the TAB key

docker images -f before=centos:5.11 --format "table {{.Repository}}\t{{.Tag}}\t{{.CreatedAt}}"
Copy the code

Rmi: Deletes a local mirror

The rmi command can delete one or more local images. Usually, rm is used to delete the command. However, in the doker command, rm is used to delete the container, so rmi is used to delete the image, where I is the first letter of image

Rmi grammar

docker rmi [OPTIONS] IMAGE [IMAGE...]
Copy the code

OPTIONS: Optional parameters IMAGE: IMAGE ID or warehouse path name

Common values of OPTIONS

-f: forcibly deletes the mirror. If the mirror has a running container, the mirror cannot be deleted directly and must be forcibly deleted. –no-prune: does not delete the process mirror of the mirror

Rmi Delete a centos mirror

docker rmi tomcat
Copy the code

Deleting centos:5.11 Mirroring

docker rmi centos:5.11
Copy the code

Example Delete the mirror whose ID is 621ceEF7494A

docker rmi 621ceef7494a
Copy the code

Delete tomcat, centos, and Redis mirrors at the same time

docker rmi tomcat centos redis
Copy the code

Forcibly delete the Tomcat image, even if a Tomcat container is running at this time, the image will be deleted

docker rmi -f tomcat
Copy the code

Tag: Tag the image and put it in the repository

The tag command creates a new version of the image based on an image and puts it in the local repository. At this point, the image has two versions in the repository, and different containers can be created based on these two images

The tag syntax

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Copy the code

SOURCE_IMAGE: original image TARGET_IMAGE: new image TAG: version number of the image

A common way to write tag is to create a my-redis1.0 image based on redis:latest image and put the new image into the Redis-lib repository

docker tag redis:latest redis-lib/my-redis:1.0
Copy the code

Create my-Redis :test-100m image based on 621CEEF7494A image and store the new image in redis-lib

docker tag 621ceef7494a redis-lib/my-redis:test-100m
Copy the code

History: View the creation history of an image

Using the history command, you can view the creation history of a mirror, that is, the submission record of the mirror

The history of grammar

docker history [OPTIONS] IMAGE
Copy the code

OPTIONS: Optional parameters IMAGE: mirror

Common values of OPTIONS

-h Boolean: Print date and size in a readable format. The default value is true. -q: display only image id NO-trunc: The output result is not captured

  • --formatNumber is used to specify the display format of the image. The syntax is as follows.tableDisplay in a table. Supported\tformat

docker history –format “[table] {{COLUMN}}[{{COLUMN}}…] ” IMAGE

  • The optional values for COLUMN are as follows:

CreatedAt: timestamp of a mirror creation.CreatedBy: command used to create a mirror.Size: Size of a mirror

History Displays the creation history of a centos image

docker history centos
Copy the code

Display centos image creation history, time and size conversion to human readable format

docker history -H=true centos
Copy the code

Displays the creation history of a centos image. Only the ID, timestamp, and command are displayed in the three columns, separated by the TAB key

docker history --format "table {{.ID}}\t{{.CreatedAt}}\t{{.CreatedBy}}" centos
Copy the code

Save: Package the image into a file

The save command can package one image or multiple images into a file. You need to distinguish the save command from the export command

The save command packages the image and contains all the information about the image

The exprot command is used to package a container, but only saves the snapshot of the container at that time. Historical records and metadata information are lost. For details, see exprot command

Save the grammar

docker save [OPTIONS] IMAGE [IMAGE...]
Copy the code

OPTIONS: Optional parameters IMAGE: mirror

Common values of OPTIONS

-o string: specifies the target file. It has the same effect as the native Linux command >

Save The centos image as my-images.tar

docker save centos > /home/my-images.tar
Copy the code

Package the centos and Redis images into my-images.tar

docker save centos redis > /home/my-images.tar
Copy the code

Package the centos and Redis images into my-images.tar

docker save -o /home/my-images.tar centos redis  
Copy the code

Load: loads the image from the specified file

The load command can load an image from a file saved by the save command

The load of grammar

docker load [OPTIONS]
Copy the code

OPTIONS: This parameter is optional

Common values of OPTIONS

-i string: specifies the file path. -q: indicates the output in quiet mode

Load is usually written to load an image from the my-images.tar file

docker load < /home/my-images.tar
Copy the code

Load the image from the my-images.tar file

docker load -i /home/my-images.tar
Copy the code

Load the image from the my-images.tar file in quiet mode

docker load -i /home/my-images.tar -q
Copy the code

Commands to manipulate containers

Run: Creates a container and runs it

The run command creates a container and runs it. If the image that created the container does not exist, it will be downloaded from the remote image repository

Running the container can also send a command to the container

Run the grammar

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Copy the code

OPTIONS: Optional parameters IMAGE: IMAGE COMMAND: COMMAND to be run ARG: COMMAND parameters

Common OPTIONS Values The OPTIONS of the run command have many optional values. Only the most frequently used OPTIONS are listed here. Use docker run –help to view all the available parameters of the run command

-t: allocates a pseudo-terminal to the container. -d: Runs the container in background mode and returns the container ID. -P list: specifies the port mapping, in the format of host port: container port -p: -m bytes: specifies the memory space available to the container. The unit is optional. B, K, m, or g -v list Run the following command to mount the disk path of the host to a specific path of the container: -w: specifies the working directory of the container, the root directory by default. –rm: automatically deletes the container when it stops running. –hostname string: specifies the hostname of the container

Run Creates a centos container and runs it

docker run centos
Copy the code

Create a centos container and run it in interactive mode

docker run -it centos
Copy the code

Create a centos container and run it in background mode

docker run -d centos
Copy the code

Create a centos container, rename it my-centos, run it in interactive mode, and run the bash command inside the container

docker run -it --name my-centos centos /bin/bash
Copy the code

Create a spring-boot container and run it in interactive mode. Rename the container to my-boot and map port 80 of the host to port 8080 of the container. Then access port +80 of the host to access the sping-boot project in the container

docker run -it --name my-boot -p 80:8080 spring-boot
Copy the code

Create a spring-boot container and run it in interactive mode. Rename the container to my-boot and bind the directory of the host /logs/my-boot/ to the directory of the container. At this time, the logs of the my-boot project can be viewed in the directory of the host /logs/my-boot

docker run -it --name my-boot -v /logs/my-boot/:/logs/ spring-boot
Copy the code

Create a spring-boot container and run it in interactive mode. Rename the container to my-boot. Map port 80 of the host to port 8080 of the container. Bind the host /logs/my-boot/ path to the container /logs directory; Allocate up to 500 MB of memory to the container. Specify the spring-boot configuration file as test

docker run -it --name my-boot -p 80:8080 -v /logs/my-boot/:/logs/ -m 500M spring-boot --spring.profiles.active=test
Copy the code

Start: Starts the container

The start command can start one or more stopped containers

Start the grammar

docker start [OPTIONS] CONTAINER [CONTAINER...]
Copy the code

OPTIONS: Optional parameters CONTAINER: indicates the CONTAINER

Common values of OPTIONS

-A: appends the container’s standard output or standard error to a terminal. -I: appends a standard input terminal to a container

Start Start the Tomcat container that has been stopped

docker start tomcat
Copy the code

Start tomcat and centos containers that have been stopped

docker start tomcat centos
Copy the code

Start the stopped my-spring-boot container and output logs

docker start -a my-spring-boot
Copy the code

Start the stopped centos container and attach an input terminal

docker start -i centos
Copy the code

Restart: Restarts the container

The restart can restart one or more containers. The container is started if it is not started and restarted if it is running

Restart the grammar

docker restart [OPTIONS] CONTAINER [CONTAINER...]
Copy the code

OPTIONS: Optional parameters CONTAINER: indicates the CONTAINER

Common values of OPTIONS

-t int: wait several seconds before restarting. The default value is 10 seconds

Restart Restart the centos container

docker restart centos
Copy the code

Restart the centos and Tomcat containers after 20 seconds

docker restart -t 20 centos tomcat
Copy the code

Stop: Stops the container

The stop command can stop one or more running containers

The kill command can also be used to stop containers

The difference is that the stop command allows the container some time to perform additional operations, such as releasing links, closing requests, and so on, before stopping

The kill command forces the container to be killed

Stop the grammar

docker stop [OPTIONS] CONTAINER [CONTAINER...]
Copy the code

OPTIONS: Optional parameters CONTAINER: indicates the CONTAINER

Common values of OPTIONS

-t int: Kills the vm if the vm is not stopped after n seconds. The default value is 10 seconds

Stop Stops the Tomcat container

docker stop tomcat
Copy the code

Stop the Tomcat and centos containers

docker stop tomcat centos
Copy the code

Stop the Tomcat container or kill it if it is not stopped within 5 seconds

docker stop -t 5 tomcat
Copy the code

Restart: Restarts the container

The restart command can restart one or more containers, whether they are running or stopped

Restart the grammar

docker restart [OPTIONS] CONTAINER [CONTAINER...]
Copy the code

OPTIONS: Optional parameters CONTAINER: indicates the CONTAINER

Common values of OPTIONS

-t int: If the restarted container is running, kill the container and restart it after n seconds. The default value is 10 seconds

Restart Restarts the Tomcat container

docker restart tomcat
Copy the code

Restart the Tomcat and centos containers

docker restart tomcat centos
Copy the code

Restart the tomcat container that is running, or if it does not stop within 5 seconds, kill it and restart it

docker restart -t 5 tomcat
Copy the code

Kill: Kills the container

The kill command can kill one or more running containers

Kill the grammar

docker kill [OPTIONS] CONTAINER [CONTAINER...]
Copy the code

OPTIONS: Optional parameters CONTAINER: indicates the CONTAINER

Common values of OPTIONS

-s string: sends a signal to the container. The signal id is the same as that of the native Linux command kill. The default value is 9

  • -sParameter Signal Number Common value

1: KILL and reload. The value can also be HUP. 9: forcibly KILL or KILL

Kill Kills the Tomcat container

docker kill tomcat
Copy the code

Forcibly kill the Tomcat container

docker kill -s 9 tomcat
Copy the code

Forcibly kill the Tomcat container

docker kill -s KILL tomcat
Copy the code

Kill the Tomcat and centos containers

docker kill tomcat centos
Copy the code

Rm: Delete the container

The rm command can delete one or more containers

If the container is running, use the -f parameter to forcibly delete it

The rm grammar

docker rm [OPTIONS] CONTAINER [CONTAINER...]
Copy the code

OPTIONS: Optional parameters CONTAINER: indicates the CONTAINER

Common values of OPTIONS

-f: deletes the container forcibly even when the container is running. -l: deletes the network relationship between containers, but not the container itself. -v: deletes the container and its mounted volumes

Rm Delete a centos container

docker rm centos
Copy the code

Forcibly delete centos containers, even if the containers are running

docker rm -f centos
Copy the code

Delete the centos container and the volumes to which it is mounted

docker rm -f centos
Copy the code

Delete all containers that have been stopped

docker rm $(docker ps -a -q)
Copy the code

Remove connection from my-nginx to my-db with connection name db

docker rm -l db 
Copy the code

Pause: Suspends the container

The pause command pauses one or more running containers

Pause the grammar

docker pause CONTAINER [CONTAINER...]
Copy the code

The CONTAINER: the CONTAINER

Pause Pauses a centos container that is running

docker pause centos
Copy the code

Pause running centos and Tomcat containers

docker pause centos tomcat
Copy the code

Unpause: Cancels the pause container

The unpause command can unpause one or more paused containers

Pause the grammar

docker unpause CONTAINER [CONTAINER...]
Copy the code

The CONTAINER: the CONTAINER

Unpause Unpause a centos container

docker unpause centos
Copy the code

Cancel suspending centos and Tomcat containers

docker unpause centos tomcat
Copy the code

Create: Creates a container

The create command creates a container, but does not run it. You can start it with the start command if necessary

Almost identical to the run command, both create a container and pull images from the remote repository if they don’t exist

The run command runs the container after it is created

The create command simply creates the container and does not run the create syntax

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Copy the code

OPTIONS: Optional parameters IMAGE: IMAGE COMMAND: COMMAND to be run ARG: COMMAND parameters

Common values of OPTIONS The create command has the same optional parameters as the run command

Since there are many optional parameters, only the most frequently used optional values are listed here. Use docker create –help to view all the parameters available to the create command

-t: allocates a pseudo-terminal to the container. -d: Runs the container in background mode and returns the container ID. -P list: specifies the port mapping, in the format of host port: container port -p: -m bytes: limited the memory capacity of the container. -v list: mounted the disk path of the host to a specific path of the container. -w: specifies the working directory of the container, the root directory by default. –rm: automatically deletes the container when it stops running. –hostname string: specifies the hostname of the container

Create Creates a centos container

docker create centos
Copy the code

Create a centos container and start in interactive mode

docker create -it centos
Copy the code

Create a centos container and start it in background mode

docker create -d centos
Copy the code

Create a centos container, rename it my-centos, start in interactive mode, and run the bash command in the container

docker create -it --name my-centos centos /bin/bash
Copy the code

Create a spring-boot container, rename the container to my-boot, and map port 80 of the host to port 8080 of the container. The interactive mode is started when the sping-boot project is accessed by accessing port +80 of the host

docker create -it --name my-boot -p 80:8080 spring-boot
Copy the code

Create a spring-boot container, rename the container to my-boot, bind the host /logs/my-boot/ directory to the container /logs directory, start the interactive mode, at this time the my-boot project logs can be viewed in the host /logs/my-boot directory

docker create -it --name my-boot -v /logs/my-boot/:/logs/ spring-boot
Copy the code

Create a spring-boot container and rename the container to my-boot. Map port 80 of the host to port 8080 of the container. Bind the host /logs/my-boot/ path to the container /logs directory; Allocate up to 500 MB of memory to the container. Specify the spring-boot configuration file as test. Start runs in interactive mode

docker create -it --name my-boot -p 80:8080 -v /logs/my-boot/:/logs/ -m 500M spring-boot --spring.profiles.active=test
Copy the code

Exec: Executes commands in the container

The exce command can execute a command in a running container

The exec grammar

 docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Copy the code

OPTIONS: Optional parameters CONTAINER: CONTAINER COMMAND: COMMAND to be executed ARG: COMMAND parameters

Common values of OPTIONS

-d: the command is run in the background. -I: keeps the standard input, which is usually used with -t. -T: allocates a pseudo-terminal, which is usually used with -i

Run the PWD command in the centos container

docker exec centos pwd
Copy the code

Assign an input terminal to a centos container

docker exec -it centos /bin/bash
Copy the code

Run the ls command in the bin directory of the centos image

docker exec -w /bin centos ls
Copy the code

Ps: View the container list

The ps command lists all containers and displays basic information about containers. By default, only running containers are displayed without any arguments

  • Column meanings are as follows
CONTAINER ID IMAGE COMMAND CREATED PORTS NAMES
The container id Corresponding mirror image The command to run when the container starts Creation time Bound port Container name

Ps grammar

docker ps [OPTIONS]
Copy the code

OPTIONS: This parameter is optional

Common values of OPTIONS

-f filter: filters the containers according to the conditions described below. -n int: displays the last n containers, including all the statuses. -l: displays the latest created containers, including all the statuses. –no-trunc: the content is not truncated. By default, truncated container information is displayed

  • -fParameter Indicates the image that is filtered by conditions. The syntax is as follows

docker ps -f KEY=VALUE

  • The possible values of KEY are as follows

Id: filter based on the container ID name: View the container containing the specified field in the container name exited: filter based on the error code of the container exit status: The value can be created, paused, exited, dead, running, Restarting, or removing. Before: Only containers created before a container are displayed. Since: Volume: filters containers bound to a directory only for running containers publish: filters by host ports only for running containers expose: filters by host ports only for running containers

Ps View a running container

docker ps
Copy the code

View all containers

docker ps -a
Copy the code

View all containers and display the container size

docker ps -a -s
Copy the code

View all containers without truncating the display

docker ps -a --no-trunc
Copy the code

View containers whose container names contain Cent

docker ps -f name=cent
Copy the code

View the created container in state

docker ps -f status=created
Copy the code

View containers created before centos

docker ps -f before=centos
Copy the code

View a running container bound to host port 80

docker ps -f publish=80
Copy the code

Inspect: retrieves metadata of a container or image

The inspect command retrieves metadata information for one or more containers or images

Metadata information can be interpreted as the details of the container or image, which is much more detailed than the ps command displays. For example, port mappings and mount directories are displayed in JSON format

Inspect grammar

docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
Copy the code

OPTIONS: Optional parameters CONTAINER: CONTAINER IMAGE: IMAGE

Common values of OPTIONS

-f string: formats the output. Inspect displays details of the entire file by default. The -f argument can specify that only certain attributes are displayed. SizeRw and SizeRootFs are displayed in the command output. Type string: Specifies the type inspect. Container indicates a container, image indicates an image, and the default value is container. For example, if I have a Tomcat image and a container named tomcat, I can use the –type parameter to specify whether INSPECT is a Tomcat container or a Tomcat image

Inspect displays metadata information about the Tomcat container

docker inspect tomcat
Copy the code

View metadata information about the Tomcat image

docker inspect --type=image tomcat
Copy the code

View the IP address of the Tomcat container

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' tomcat
Copy the code

View the IP address of the Tomcat container

docker inspect tomcat | grep IPAddress
Copy the code

View port mappings of the Tomcat container

docker inspect -f "{{.HostConfig.PortBindings}}" tomcat
Copy the code

View the mount directory of the Tomcat container

docker inspect -f "{{.HostConfig.Binds}}" tomcat
Copy the code

Stats: Monitors resource usage of the container

The stats command can monitor the resource usage of a container, such as CPU usage, memory usage, and so on. Refresh every second until usedctrl+cexit

  • Column meanings are as follows
CONTAINER ID NAME CPU % MEM USAGE/LIMIT MEM % NET I/O BLOCK I/O PIDS
The container id Container name CPU usage percentage Use content size/maximum available memory Percentage of memory used Network IO Disk I/o The number of threads or processes in the container

Stats grammar

docker stats [OPTIONS] [CONTAINER...]
Copy the code

OPTIONS: Optional parameters CONTAINER: indicates the CONTAINER

Common values of OPTIONS

-a filter: displays the resource usage of all containers. By default, only the running resource is displayed. –format string: formats the output result. Do not truncate the display information. By default, some fields are displayed with only brief information, such as the container ID

Stats monitors resource usage of all running containers

docker stats
Copy the code

Monitor resource usage for all containers, including containers that are not started

docker stats -a
Copy the code

Only resource usage of centos containers is monitored

docker stats centos
Copy the code

The resource usage of the centos container is monitored. The displayed result is not refreshed

docker stats --no-stream centos
Copy the code

Top: Displays information about processes running in the container

Docker exec CONTAINER ps can also view the process information of the CONTAINER.

The difference is that the former looks at the id of the process on which the container is running. The latter looks at the process ID inside the container

Top grammar

docker top CONTAINER [ps OPTIONS]
Copy the code

CONTAINER: CONTAINER OPTIONS: optional parameters of the ps command

Top Common syntax Query the ID of the host process of the centos image

docker top centos
Copy the code

Rename: renames a container

Rename can rename a container. If the –name parameter is not specified during run, rename can rename the container

Rename the grammar

docker rename CONTAINER NEW_NAME
Copy the code

Rename Rename a centos container to my-centos

docker rename centos my-centos
Copy the code

Attach: Attach to a container

Attach can be attached to a container. The container must be a running container. An error will be reported if the container is not running

Exiting a container using commands such as CTRL + C or exit will cause the container to stop running. Therefore, it is not recommended to use this command in the production environment. Production environments can use the exec command to enter containers

Attach the grammar

docker attach [OPTIONS] CONTAINER
Copy the code

OPTIONS: Optional parameters CONTAINER: indicates the CONTAINER

Common values of OPTIONS

–sig-proxy=false Boolean: Default true, false prevents container from stopping when it encounters CTRL + C exit signal

Attach to the running centos image

docker attach centos
Copy the code

Update: Updates the configuration of one or more containers

Update allows you to update the container configuration

The update syntax

docker update [OPTIONS] CONTAINER [CONTAINER...]
Copy the code

OPTIONS: Optional parameters CONTAINER: indicates the CONTAINER

Common values of OPTIONS

-m bytes: The unit can be b, K, m, or G. –memory-swap bytes: — CPU Demecial: indicates the CPU resources, for example, 1.5 indicates that 1.5 CPU resources can be used on the host. –cpuset-cpus string: The container can use the host CPU kernel number. 0-3 indicates that all four cores can be used. 1,3 indicates that only cores 1 and 3 can be used. No: does not restart. On-failure: restarts when the container exits abnormally; On-failure :3: restarts 3 times on abnormal exit; Alaways: Always reboot; Unless -stopped: the container is always restarted when the container exits, but does not take into account containers that were stopped when the Docker daemon started — ids-limit int: limits the number of processes or threads in the container. Default -1 means no limit

Update Update the memory of a centos image to 2 gb

docker update --memory-swap -1 -m 2g centos
Copy the code

Update the container restart policy

docker update --restart on-failure:3 centos
Copy the code

The maximum number of threads to update the Tomcat container is 2000

docker update --pids-limit 2000 tomcat
Copy the code

Logs: View the logs of the container

Logs grammar

docker logs [OPTIONS] CONTAINER
Copy the code

OPTIONS: Optional parameters CONTAINER: indicates the CONTAINER

Common values of OPTIONS

-f: logs are traced in real time. — Until string: logs generated before a certain point in time are viewed in the format of 2021-06-03T19:30:20Z. Or use a relative time of 10m, indicating 10 minutes ago –since string: View logs after a certain point in time, format: 2021-06-03T19:30:20Z. -n int: Displays the last few lines of the log. All lines are displayed by default. -t: displays the timestamp in the log

View the last 10 lines of the Tomcat log

docker logs -n 10 tomcat
Copy the code

View the last 10 lines of Tomcat logs and monitor the log output in real time

docker logs -n 10 -f  tomcat
Copy the code

View logs generated within the last 10 minutes

docker logs --since 10m tomcat
Copy the code

Check the logs on June 3rd between 9:00 and 10:00

docker logs --since 2021-06-03T9:00:00  --until 2021-06-03T10:00:00 tomcat
Copy the code

Wait: Blocks the container until it exits and prints its exit code

The wait command blocks one or more containers until the containers exit and prints out their exit code

Wait the grammar

docker wait CONTAINER [CONTAINER...]
Copy the code

The CONTAINER: the CONTAINER

Wait is commonly written to block the centos container until it exits and prints the exit status code

docker wait centos
Copy the code

In this case, open a new terminal, stop the centos container, switch to wait terminal, you can see a status code

Port: lists port mappings

The port of grammar

docker port CONTAINER [PRIVATE_PORT[/PROTO]]
Copy the code

CONTAINER: CONTAINER PRIVATE_PORT: CONTAINER port PROTO: protocol used by the port

View the port mapping of the my-boot container

docker port my-boot
Copy the code

View the host port mapped from port 8080 of the my-Boot container

docker port my-boot 8080
Copy the code

View the host port mapped to the My-boot container using TCP port 8080

docker port my-boot 8080/tcp
Copy the code

Export: Package the container into a file

The export command, which packages containers into a file, can be confused with the save command

The difference between Export and Save is that export packages containers, while Save packages images

Export packages the snapshot of the container at that time. The history and metadata information of the container will be lost. Also, when an export file is imported as an image, the image name and version number can be respecified

The export of grammar

docker export [OPTIONS] CONTAINER
Copy the code

OPTIONS: Optional parameters CONTAINER: indicates the CONTAINER

Common values of OPTIONS

-o string: Specifies the package file

Export Package the my-boot container to the my-boot.tar file

docker export -o /tmp/my-boot.tar my-boot
Copy the code

Import: Import an image from a local or remote file to a local repository

Import can import images from a local or remote file to a local repository

If it is imported from a file, the file must be exported by the export command

The import of grammar

docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Copy the code

OPTIONS: Optional parameters File: file address URL: URL address – : imported from the STANDARD input terminal. Usually used with the cat command in Linux, REPOSITORY: local image REPOSITORY address TAG: image version number

Common values of OPTIONS

-m string: adds the description. -c list: applies the dokerfile command to the created container

Import Create an image from the my-boot.tar file

cat /tmp/my-boot.tar | docker import -
Copy the code

Create an image from the my-boot.tar file

docker import /tmp/my-boot.tar
Copy the code

Create an image from the my-boot.tar file and specify the name of the image as my-boot-test and version 1.0

docker import /tmp/my-boot.tar my-boot-test:1.0
Copy the code

Create an image from the my-boot.tar file. Note that the information is test, and specify the image name as my-boot-test and version 1.0

docker import --message 'test' /tmp/my-boot.tar my-boot-test:1.0
Copy the code

Create an image from the my-boot.tar file on the remote server

docker import http:/ / 192.168.100.1:8080 / images/my - boot. The tar
Copy the code

Content from the public account Helian Xiao Wu, reprint please indicate the source