Docker command university and examples of commonly used writing

Docker command university and examples of commonly used writing

The content comes from the official account

Helian Xiaowu
, Please indicate the source for reprinting [TOC]

Reference

docker official website
docker official document
rookie tutorial

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

Powerful functions need complicated commands to support, although there are many docker commands, I can't remember many.

A good memory is not as good as a bad pen. This article summarizes the commands commonly used by docker, and gives descriptions and examples for each command. You can take it whenever you want.

The mirror warehouse is used to store mirror images, which can be divided into remote mirror warehouse and local mirror warehouse.

The image of the remote warehouse can be downloaded to the local through the pull command, and the image of the local warehouse can be pushed to the remote through the push command

The image in the local warehouse can be used to create containers, and one image can create multiple containers

The container can also be packaged into a mirror through the commit command and submitted to the local warehouse

Commands for operating remote warehouses

login: log in to the remote warehouse

The login command can log in to the remote warehouse. After logging in to the remote warehouse, you can pull the mirror image of the warehouse.

login syntax

docker login [OPTIONS] [SERVER] Copy code

OPTIONS
: Optional parameters
SERVER
: The address of the remote warehouse, the default is the official docker warehouse, which is hub.docker.com/

Common values of OPTIONS

-u
string: username
-p
string: password

Common way to write login
Use helianxiaowu user to log in to the remote warehouse, the password is 123456

Login -p -u helianxiaowu Docker 123456 192.168 .10 .10/lib Docker- duplicated code

Do not specify a user to log in to the remote warehouse, then you will be prompted to enter a user name or password

Login Docker 192.168 .10 .10/lib Docker- duplicated code

If you do not specify a user to log in to the default remote warehouse, you will also be prompted to enter a user name or password

docker logincopy code

search: Search for mirrors from remote warehouses

The search command can search for mirrors from remote warehouses

  • The column meaning is as follows
NAMEDESCRIPTIONSTARSOFFICIALAUTOMATED
Mirror nameMirror descriptionMirror heat, similar to github's startsIs it an official releaseWhether to build automatically

search syntax

docker search [OPTIONS] TERM duplicated code

OPTIONS
: Optional parameters
TERM
Keywords for mirroring

Common values of OPTIONS

-f
filter: Filter images based on conditions, see below for filter conditions
no-trunc
: Display the complete mirror description. By default, if the description of the searched image is too long, it will be hidden. The no-trunc parameter will make the image information displayed completely
--limit
int: Limit the number of searched mirrors, the maximum cannot exceed 100, and the default is 25
--format
string: Specify the format of the mirror display, see below for the format

  • -f
    The parameter represents the mirror image searched out according to the filter condition, the syntax is as follows

docker search

-f KEY=VALUE
TERM

  • The optional values of KEY are as follows

stars
int: Filter according to heat, such as: stars=10 means to filter mirror images with heat greater than 10
is-automated
boolean: Filter according to whether to automatically build, such as: is-automated=false means to filter non-automatically built images
is-official
boolean: Filter according to whether it is officially released, such as: is-official=false means to filter unofficially released images

  • --format
    The parameter is used to specify the display format of the searched mirror, the syntax is as follows.
    table
    Indicates the use of a table to display, support
    \t
    format

docker search

--format "[table] {{COLUMN}}[{{COLUMN}}...]"
TERM

  • The optional values of COLUMN are as follows:

.Name
: Display the name column of the mirror
.Description
: Display the description column of the mirror
.StarCount
Display the heat column of the mirror
.IsOfficial
: Shows whether the mirror is an official release column
.IsAutomated
: Shows whether the mirror is automatically constructed a column

Search commonly used wording
Search centos mirror

docker search centoscopy code

Search centos mirror, only show 5

Search --limit Docker . 5 CentOS duplicated code

The search popularity is greater than 100 and is not an automatically built centos image

Search Stars = -f Docker 100 -f = IS-Automated to true CentOS duplicated code

Search unofficially released centos mirrors, the search results only show the name and popularity, and the columns are separated by the TAB key

Search -f IS-Official Docker = to false --format "Table {{.}} the Name/T {{.}} starcount" CentOS copy the code

push: push the local image to the remote warehouse

Push can push the image in the local warehouse to the remote warehouse, but you need to log in to the remote warehouse first

push usage

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

OPTIONS
: Optional parameters
NAME
: Mirror name
TAG
Mirror version number, can be omitted, the default is latest

Common values of OPTIONS

--disable-content-trust
: The remote warehouse does not verify the signature when pushing, the default is true

Push commonly used wording Push
version 1.1.0 of the my-image mirror to the remote warehouse

My Image Push-Docker: 1.1 .0 copy the code

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

docker push my-image duplicated code

pull: pull or update the image from a remote warehouse

The pull command can pull a mirror from a remote warehouse, if the mirror already exists in the local warehouse, it will be updated

pull syntax

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

OPTIONS
: Optional parameters
NAME
: Mirror name
TAG
Mirror version number, can be omitted, the default is latest
DIGEST
: The summary of the mirror, each mirror has a corresponding name, id, and summary information, and each summary information can uniquely represent a mirror, as shown in the figure below

Common values of OPTIONS

-a
: Pull all the version numbers of the image
--disable-content-trust
: The remote warehouse does not verify the signature when pushing, the default is true
-q
: Quiet mode, no detailed information is displayed during the push process

Common way of writing
pull to pull centos image from remote warehouse, default to latest version when version is not specified

docker pull centos copy code

Use quiet mode to pull the centos image with version number 5.11 from the remote warehouse

CentOS pull -q Docker: 5.11 copy the code

Use quiet mode to pull centos images of all version numbers from remote warehouses

docker pull -a -q centos copy the code

Commands for operating local mirrors

images: display all images

The images command can display all the images that exist locally

  • The column meaning is as follows
REPOSITORYTAGIMAGE IDCREATEDSIZE
Warehouse pathMirror versionMirror idCreation timeMirror size

images syntax

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

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

Common values of OPTIONS

-a
: Show all images, including intermediate images (intermediate images are hidden by default)
-f
filter: Filter images based on conditions, see below for filter conditions
-q
: Only display the mirror id
no-trunc
: Display the complete image id. By default, only the first 12 digits of the mirror id will be displayed, and the no-trunc parameter will display the mirror id completely
--digests
: Display summary information of mirroring
--format
string: Specify the format of the mirror display, see below for the format

  • -f
    The parameter indicates that the mirror image to be displayed is filtered according to the condition, the syntax is as follows

docker images

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

  • The optional values of KEY are as follows

dangling
boolean: Filter hanging mirrors, such as: dangling=true means only display hanging mirrors
label
string: Filter by label, for example: label=version means to display the image with version label, label=version=1.0 means to display the image with version=1.0
before
image: Display the mirror created before a certain mirror, such as: before=centos:5.8 means to display the mirror created before the centos:5.8 mirror
since
image: Display the mirror created after a certain existence, such as: since=centos:5.8 means display the mirror after the existence of centos:5.8
reference
string: Fuzzy matching, such as: reference=cent*:5*, to display the image whose name starts with cent and the version number starts with 5.

  • --format
    The parameter is used to specify the mirror display format, the syntax is as follows.
    table
    Indicates the use of a table to display, support
    \t
    format

docker images

--format "[table] {{COLUMN}}[{{COLUMN}}...]"
[REPOSITORY[:TAG]]

  • The optional values of COLUMN are as follows:

.ID
: Display the name column of the mirror
.Repository
: Display the description column of the mirror
.Tag
Display the heat column of the mirror
.Digest
: Shows whether the mirror is an official release column
.CreatedSince
: Shows whether the mirror is automatically constructed a column
.CreatedAt
: Shows whether the mirror is automatically constructed a column
.Size
: Shows whether the mirror is automatically constructed a column

Images commonly written to
show all local mirrors

docker imagescopy code

Display all local mirrors, only display the id column without truncation

docker images -q --no-trunc duplicated code

Display centos mirror information

docker images centoscopy code

Show all images that contain the cent keyword in the column

docker images | grep cent copy the code

Display all local mirrors, and display the summary column

docker images --digests copy the code

Show all images of the latest version created after the cengos:latest image

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

Display all mirror information, only display 3 columns of mirror id, summary, and creation time, separated by TAB key

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

Display the mirror created before the centos:5.11 mirror, only display 3 columns of mirror warehouse path, version number, and creation time, separated by TAB key

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

rmi: delete local mirror

The rmi command can delete one or more local mirrors. Normally, rm should be used to indicate the delete command, but in the doker command, rm means to delete the container, so rmi means to delete the mirror.

i
Is the first letter of image

rmi syntax

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

OPTIONS
: Optional parameters
IMAGE
Mirror id or warehouse path name

Common values of OPTIONS

-f
: Forced deletion. If the corresponding container of the image is running, it is not allowed to delete the image directly. Forced deletion
--no-prune
: Do not delete the process image of the image, it is deleted by default

rmi commonly used wording
delete centos mirror

docker rmi tomcatcopy code

Delete centos:5.11 mirror

rmi CentOS Docker: 5.11 copy the code

Delete the mirror with id 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: mark the mirror image and put it into the warehouse

The tag command can be based on a mirror, create a new version of the mirror and put it into the local warehouse. At this time, the mirror has two versions in the warehouse. You can create different containers based on these two mirrors.

tag syntax

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

SOURCE_IMAGE
: Original image
TARGET_IMAGE
: New mirror
TAG
: The version number of the mirror

The common way to write tags is to
create a my-redis1.0 mirror based on the redis:latest mirror, and put the new mirror into the redis-lib warehouse

Tag Redis Docker: Latest Redis-lib/My-Redis: 1.0 copy the code

Create a my-redis:test-100m image based on the 621ceef7494a image, and put the new image into the redis-lib warehouse

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

history: View the creation history of the mirror

The history command is used to view the creation history of a certain mirror, that is, the submission record of the mirror

history syntax

docker history [OPTIONS] IMAGE duplicated code

OPTIONS
: Optional parameters
IMAGE
: Mirror

Common values of OPTIONS

-H
boolean: print the date and size in a readable format, the default is true
-q
: Only display the mirror id
no-trunc
: The output result will not be intercepted. Under normal circumstances, the result will be intercepted if a column is too long
--format
string: Specify the format of the mirror display, see below for the format

  • --format
    The number is used to specify the format of the mirror display, the syntax is as follows.
    table
    Indicates the use of a table to display, support
    \t
    format

docker history

--format "[table] {{COLUMN}}[{{COLUMN}}...]"
IMAGE

  • The optional values of COLUMN are as follows:

.ID
: ID of the image
.CreatedSince
: The duration of the image creation
.CreatedAt
: Timestamp of image creation
.CreatedBy
: Commands used for image creation
.Size
: The size of the image
.Comment
: Mirror comments

History is commonly written to
show the creation history of centos mirroring

docker history centos copy the code

Display the creation history, time and size of the centos mirror into a human-readable format

History -H = Docker to true CentOS duplicated code

Display the creation history of centos mirror, only display ID, creation timestamp and creation command 3 columns, separated by TAB key

History --format Docker "Table .ID {} {}/T {{.}} createdAt/T {{.}} CreatedBy" CentOS copy the code

save: package the image into a file

The save command can pack one image or multiple images into a file, and special attention is needed to distinguish it from the export command

The save command packs the image, which contains all the information of the image

The exprot command packs the container, but saves the snapshot of the container at the time, and the historical record and metadata information will be lost. See the exprot command introduction for details.

save syntax

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

OPTIONS
: Optional parameters
IMAGE
: Mirror

Common values of OPTIONS

-o
string: Specify the target file, and the linux native command
>
Have the same effect

Save commonly used wording
Pack the centos image into my-images.tar

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

Pack the centos image and redis image to my-images.tar

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

Pack the centos image and redis image to my-images.tar

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

load: load the image from the specified file

The load command can load the image from the specified file, which needs to be the file saved by the save command

Load syntax

docker load [OPTIONS] Copy code

OPTIONS
: Optional parameters

Common values of OPTIONS

-i
string: specify the path of the file
-q
: Quiet mode output

Load commonly used wording
Load the image from the my-images.tar file

docker load </home/my-images.tar duplicated code

Load the image from the my-images.tar file

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

Use quiet mode to load images from the my-images.tar file

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

Commands for manipulating containers

run: create a container and run

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

While running the container, you can also send a command to the container

run syntax

docker run [OPTIONS] IMAGE [COMMAND ] [ARG ...] duplicated code

OPTIONS
: Optional parameters
IMAGE
: Mirror
COMMAND
: Commands to be run
ARG
: The parameters of the command

Commonly used values
of OPTIONS As there are many optional values for OPTIONS in the run command, only the most frequently used optional values are listed here. use

docker run --help
You can view all available parameters of the run command

-i
: Run in interactive mode, usually used with -t
-t
: Assign a pseudo terminal to the container, usually used with -i
-d
: Run the container in background mode and return the container id
-p
list: Specify port mapping, the format is
Host port: container port

-P
: Randomly assign port mapping
--name
string: Assign a name to the container
-m
bytes: limit the memory size that the container can use, the unit can be b, k, m, g
-v
list: Mount the disk path of the host to a path of the container
--volumes-from
list: Bind a path of another container to a path of this container
-w
: Specify the working directory of the container, the default is the root directory
--rm
: Automatically delete when the container stops running
--hostname
string: Specify the host name of the container

Run commonly used writing
Create a centos container and run

docker run centoscopy 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 to my-centos, and run it in interactive mode, and run bash commands in the container

docker run -it --name my-centos centos /bin/bash duplicated 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. At this time, you can access the sping-boot in the container by accessing the host ip+80 port. project

My-RUN Expediting IT --name Docker Boot -p 80 : 8080 Spring Boot- duplicated code

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

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 path of host/logs/my-boot/to/logs of the container Directory; allocate a maximum of 500M of memory to the container; specify the spring-boot configuration file as test

My-RUN Expediting IT --name Docker Boot -p 80 : 8080 -v/logs/My-Boot/:/logs/-m = --spring.profiles.active Boot-500M Spring Test duplicated code

start: start the container

The start command can start one or more stopped containers

start syntax

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

OPTIONS
: Optional parameters
CONTAINER
:container

Common values of OPTIONS

-a
: Append the standard output or standard error of the container to the terminal
-i
: Attach a standard input terminal to the container

Start commonly used wording
Start the stopped tomcat container

docker start tomcat copy the code

Start stopped tomcat and centos containers

docker start tomcat centos copy the code

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

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: restart the container

Restart can restart one or more containers. If the container is not started, it will start, if it is running, it will restart

restart syntax

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

OPTIONS
: Optional parameters
CONTAINER
:container

Common values of OPTIONS

-t
int: wait a few seconds before restarting, the default is 10 seconds

Restart commonly used wording
restart centos container

docker restart centos copy the code

Restart centos and tomcat containers after 20 seconds

restart -t Docker 20 CentOS Tomcat Copy the code

stop: stop the container

The stop command can stop one or more running containers

The kill command can also be used to stop the container

The difference is that the stop command allows the container to have a certain amount of time to perform additional operations before stopping, such as releasing the link, closing the request, etc.

The kill command will forcefully kill the container directly

stop syntax

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

OPTIONS
: Optional parameters
CONTAINER
:container

Common values of OPTIONS

-t
int: If it doesn't stop after waiting for n seconds, kill it directly, the default is 10 seconds

Stop commonly used wording
Stop the tomcat container

docker stop tomcatcopy code

Stop tomcat and centos container

docker stop tomcat centos copy the code

Stop the tomcat container, if it has not stopped within 5 seconds, kill it directly

STOP -t Docker 5 Tomcat Copy the code

restart: restart the container

The restart command can restart one or more containers, regardless of whether the container is running or stopped

restart syntax

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

OPTIONS
: Optional parameters
CONTAINER
:container

Common values of OPTIONS

-t
int: If the restarted container is running, wait for n seconds and have not stopped, then kill it and restart it, the default is 10 seconds

Restart commonly used wording
restart tomcat container

docker restart tomcat copy the code

Restart tomcat and centos container

docker restart tomcat centos copy the code

Restart the running tomcat container, if it has not stopped within 5 seconds, kill it and restart it

restart -t Docker 5 Tomcat Copy the code

kill: kill the container

The kill command can kill one or more running containers

kill syntax

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

OPTIONS
: Optional parameters
CONTAINER
:container

Common values of OPTIONS

-s
string: Send a signal to the container, the signal number is the same as the signal number of the linux native command kill, the default value is 9, some commonly used values are listed below

  • -s
    Common value of parameter signal number

1
: Kill and reload, also available
HUP
Means
9
: Forced kill, also available
KILL
Indicates that the default value
15
: Normal stop, also available
TERM
Means

Common way of writing
kill kill tomcat container

docker kill tomcatcopy code

Force to kill the tomcat container

the kill -s Docker 9 Tomcat Copy the code

Force to kill the tomcat container

docker kill -s KILL tomcat copy the code

Kill tomcat and centos container

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, you need to force deletion through the -f parameter

rm syntax

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

OPTIONS
: Optional parameters
CONTAINER
:container

Common values of OPTIONS

-f
: Forced deletion, even if the container is running, it can be deleted
-l
: Delete the network relationship between the containers, not the container itself
-v
: Delete the container and its mounted volume

rm commonly used wording
delete centos container

docker rm centoscopy code

Forcibly delete the centos container, even if the container is running, it will be deleted

docker rm -f centos copy the code

Delete the centos container and delete the volume it mounts

docker rm -f centos copy the code

Delete all stopped containers

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

Remove the connection of the container my-nginx to the container my-db, the connection name is db

docker rm -l db copy the code

pause: Pause the container

The pause command can pause one or more running containers

pause syntax

docker pause CONTAINER [CONTAINER...] Copy code

CONTAINER
:container


Pause commonly used wording Pause the running centos container

docker pause centos copy the code

Pause the running centos and tomcat containers

docker pause centos tomcat copy the code

unpause: unpause the container

The unpause command can unpause one or more paused containers

pause syntax

docker unpause CONTAINER [CONTAINER...] Copy code

CONTAINER
:container

Unpause commonly used wording to
cancel the suspended centos container

docker unpause centos copy the code

Unpause centos and tomcat containers

docker unpause centos tomcat copy the code

create: Create a container

The create command can create a container, but does not run it, you can use the start command to start it when needed

The usage is almost the same as the run command. It will create a container. If the image that the container depends on does not exist, it will be pulled from the remote warehouse.

The run command will run the container after creating the container

The create command just creates the container, does not run the
create syntax

docker create [OPTIONS] IMAGE [COMMAND ] [ARG ...] duplicated code

OPTIONS
: Optional parameters
IMAGE
: Mirror
COMMAND
: Commands to be run
ARG
: The parameters of the command

The common values of OPTIONS
are the same as the optional parameters of the run command.

Since there are many optional parameters, only the most frequently used optional values are listed here. use

docker create --help
You can view all available parameters of the create command

-i
: Run in interactive mode, usually used with -t
-t
: Assign a pseudo terminal to the container, usually used with -i
-d
: Run the container in background mode and return the container id
-p
list: Specify port mapping, the format is
Host port: container port

-P
: Randomly assign port mapping
--name
string: Assign a name to the container
-m
bytes: limit the memory size that the container can use
-v
list: Mount the disk path of the host to a path of the container
--volumes-from
list: Bind a path of another container to a path of this container
-w
: Specify the working directory of the container, the default is the root directory
--rm
: Automatically delete when the container stops running
--hostname
string: Specify the host name of the container

Create commonly used wording
Create a centos container

docker create centos copy the code

Create a centos container, run in interactive mode when start is started

docker create -it centos copy the code

Create a centos container, run in background mode when start is started

docker create -d centos copy the code

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

docker create -it --name my-centos centos /bin/bash duplicated code

Create a spring-boot container, rename it to my-boot, and map port 80 of the host to port 8080 of the container. It runs in interactive mode at start. At this time, you can access the sping- in the container by accessing the host ip+80 port. boot project

Create Expediting IT --name My-Docker Boot -p 80 : 8080 Spring Boot- duplicated code

Create a spring-boot container, rename the container to my-boot, and bind the host/logs/my-boot/directory to the container's/logs directory, and run in interactive mode at start. At this time, the my-boot project The log can be viewed in the/logs/my-boot directory of the host

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

Create a spring-boot container, rename the container to my-boot; map port 80 of the host to port 8080 of the container; bind the path of host/logs/my-boot/to the/logs directory of the container; assign the container Maximum memory of 500M; specify the spring-boot configuration file as test; run in interactive mode at start

Create Expediting IT --name My-Docker Boot -p 80 : 8080 -v/logs/My-Boot/:/logs/-m = --spring.profiles.active Boot-500M Spring Test duplicated code

exec: execute commands in the container

The exce command can execute a command in a running container

exec syntax

docker exec [OPTIONS] CONTAINER COMMAND [ ARG ...] duplicated code

OPTIONS
: Optional parameters
CONTAINER
:container
COMMAND
: The command to execute
ARG
: The parameters of the command

Common values of OPTIONS

-d
: Command runs in the background
-i
: Keep standard input, usually and
-t
use together
-t
: Assign a pseudo terminal, usually and
-i
use together
-w
string: specify the path of the container

exec is commonly used to write
pwd command in centos container

docker exec centos pwd copy the code

Assign an input terminal to the centos container

docker exec -it centos/bin/bash duplicated code

Execute the ls command in the bin directory of the centos mirror

docker exec -w/bin centos ls duplicated code

ps: View the container list

The ps command can list a list of all containers and view the basic information of the container. Without any parameters, only the running container is displayed by default

  • The column meaning is as follows
CONTAINER IDIMAGECOMMANDCREATEDPORTSNAMES
Container idCorresponding mirrorCommand to run when the container startsCreation timeBound portContainer name

ps syntax

docker ps [OPTIONS] Copy code

OPTIONS
: Optional parameters

Common values of OPTIONS

-a
: Display all containers, only the running ones are displayed by default
-f
filter: Filter the container according to the conditions, see below for the filter conditions
-n
int: Display the last n containers created, including all states
-l
: Display the newly created container, including all status
-q
: Only display container id
-s
: Display the size of the container, the column is not displayed by default
--no-trunc
: The displayed content is not truncated, the container displayed by default is the truncated information

  • -f
    The parameter represents the mirror image searched out according to the filter condition, the syntax is as follows

docker ps

-f KEY=VALUE

  • The optional values of KEY are as follows

id
: Filter by container id
name
: View the container that contains the given field in the container name
exited
: Filter according to the error code of the container exit
status
: Filter according to the status of the container. The optional status values are: created, paused, exited, dead, running, restarting, and removing
before
: Only display containers created before a certain container
since
: Only display containers created after a certain container
volume
: Filter containers bound to a certain directory, only for running containers
publish
: Filter based on the host port, only for running containers
expose
: Filter according to the container port, only for the running container

Commonly used ps to
view the running container

docker ps copy 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, the display content is not truncated

docker ps -a --no-trunc duplicated code

View the container that contains cent in the container name

docker ps -f name = cent duplicated code

View the container whose status is created

docker ps -f status = created duplicated code

View the container created before centos

docker ps -f before = centos duplicated code

View the container that is bound to port 80 of the host and is running

PS = -f publish Docker 80 duplicated code

inspect: Obtain the metadata of the container or image

The inspect command can obtain metadata information of one or more containers or images

Metadata information can be understood as the details of the container or image, which is better than

ps
The content displayed by the command is much more detailed. For example, port mapping, mounting directory, etc., the display format is json type

inspect syntax

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

OPTIONS
: Optional parameters
CONTAINER
:container
IMAGE
: Mirror

Common values of OPTIONS

-f
string: Format the output result, inspect displays the details of the entire file by default, and the -f parameter can specify to display only certain attributes
--s
: Only valid for the container, displaying the number and size of the container's configuration file, and there will be two more parameters, SizeRw and SizeRootFs, in the displayed result
--type
string: Specify the type to inspect, container means container, image means image, the default is container. For example, if I have a tomcat image and a container named tomcat, I can use the --type parameter to specify whether to inspect is a tomcat container or a tomcat image

Inspect is commonly used to
view the metadata information of the tomcat container

docker inspect tomcat copy the code

View the metadata information of the tomcat mirror

docker inspect --type = image tomcat duplicated code

View the ip address of the tomcat container

Inspect --format = Docker '{{} {} {Range .NetworkSettings.Networks. the IPAddress} End {} {}}' Tomcat copy the code

View the ip address of the tomcat container

docker inspect tomcat | grep IPAddress copy the code

View the port mapping of the tomcat container

Inspect -f Docker ".HostConfig.PortBindings {} {}" Tomcat copy the code

View the mount directory of the tomcat container

Inspect -f Docker ".HostConfig.Binds {} {}" Tomcat copy the code

stats: monitor the resource usage of the container

The stats command can monitor the resource usage of the container, such as cpu usage, memory usage, etc. Refresh every second, until use

ctrl+c
drop out

  • The column meaning is as follows
CONTAINER IDNAMECPU%MEM USAGE/LIMITMEM%NET I/OBLOCK I/OPIDS
Container idContainer nameCPU usage percentageUse content size/maximum available memoryPercentage of memory usageNetwork IODisk IOThe number of threads or processes in the container

stats syntax

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

OPTIONS
: Optional parameters
CONTAINER
:container

Common values of OPTIONS

-a
filter: Display the resource usage of all containers, by default only the running ones are displayed
--format
string: formatted output result
--no-stream
: Refresh without interval, only display the first statistical result
--no-trunc
: Do not truncate the displayed information. By default, some fields only display brief information, such as container id

Commonly used stats to
monitor the resource usage of all running containers

docker statscopy code

Monitor the resource usage of all containers, including unstarted containers

docker stats -a copy the code

Only monitor the resource usage of the centos container

docker stats centos copy the code

Monitor the resource usage of the centos container, the display result is not refreshed

docker stats --no-stream centos duplicated code

top: View process information running in the container

top can view the process information of the container,

docker exec CONTAINER ps
You can also view the process of the container.

The difference is that the former looks at the process id of the container running on the host. The latter looks at the process id in the container

top syntax

docker top CONTAINER [ps OPTIONS] Copy code

CONTAINER
:container
OPTIONS
: Optional parameters of the ps command

Top commonly used wording
View the host process id of the centos mirror

docker top centoscopy code

rename: rename the container

rename can rename the container. If the container name is not specified with the --name parameter when the container is run, you can use rename to name it

rename syntax

docker rename CONTAINER NEW_NAME copy the code


Rename is commonly used to rename the centos container to my-centos

docker rename centos my-centos duplicated code

attach: connect to the container

Attach can be connected to the container. This container must be a running container. When it is not running, an error will be reported.

When using

ctrl+c
or
exit
Waiting for the command to exit the container will cause the container to stop running. Therefore, it is not recommended to use this command in a production environment . The production environment can use the exec command to enter the container

attach syntax

docker attach [OPTIONS] CONTAINER duplicated code

OPTIONS
: Optional parameters
CONTAINER
:container

Common values of OPTIONS

--sig-proxy=false
boolean: The default is true, when it is false, it can prevent the container from encountering
ctrl+c
Stop running when exiting the signal

Attach is commonly used to write
into the running centos mirror

docker attach centoscopy code

update: update the configuration of one or more containers

update can update the configuration of the container

update syntax

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

OPTIONS
: Optional parameters
CONTAINER
:container

Common values of OPTIONS

-m
bytes: Specify the memory size of the container, the unit can be b, k, m, g
--memory-swap
bytes:
--cpu
demecial: cpu resources, such as 1.5 means that 1.5 cpu resources of the host can be used
--cpuset-cpus
string: The container can use the host's cpu kernel number,
0-3
Indicates that all 4 cores can be used,
1,3
Indicates that only cores 1 and 3 can be used
--restart
string: Specifies the exit restart strategy of the container. no: do not restart; on-failure: restart when the container exits abnormally; on-failure: 3: restart 3 times when abnormal exit; alaways: always restart; unless-stopped: always restart the container when the container exits, but Do not consider containers that have been stopped when the Docker daemon is started
--pids-limit
int: limit the number of container processes or threads, the default -1 means no limit

Update is commonly used to
update the memory of the centos mirror to 2G

Update the swap---memory Docker - . 1 -m 2g CentOS duplicated code

Update container restart strategy

Update --restart ON-failure Docker: . 3 CentOS copy the code

Update the maximum number of threads of the tomcat container to 2000

Update --pids-limit Docker 2000 Tomcat Copy the code

logs: View the logs of the container

logs syntax

docker logs [OPTIONS] CONTAINER duplicated code

OPTIONS
: Optional parameters
CONTAINER
:container

Common values of OPTIONS

-f
: Real-time output of trace log
--until
string: View the log before a certain point in time, format: 2021-06-03T19:30:20Z. Or use a relative time of 10m, which means 10 minutes ago
--since
string: View the log after a certain point in time, format: 2021-06-03T19:30:20Z. Use a relative time of 10m, which means within 10 minutes
-n
int: View the last few lines of log, display all by default
-t
: Show timestamp in log


Logs are commonly written to view the last 10 lines of tomcat logs

-n logs Docker 10 Tomcat Copy the code

View the last 10 lines of tomcat log, and monitor the log output in real time

-n logs Docker 10 -f Tomcat Copy the code

View the logs of the last 10 minutes

docker logs --since 10m tomcat copy the code

View the log between 9:00 and 10:00 on June 3

logs --since Docker 2021 - 06 -03T9: 00 : 00 --until 2021 - 06 -03T10: 00 : 00 Tomcat Copy the code

wait: Block the container until the container exits and print its exit code

The wait command can block one or more containers until the container exits and print out their exit code

wait syntax

docker wait CONTAINER [CONTAINER...] Copy code

CONTAINER
:container

The commonly used wording of wait
blocks the centos container until it exits and prints the exit status code

docker wait centoscopy code

At this time, a new terminal is opened, the centos container is stopped, and a status code can be seen by switching to the wait terminal.

port: List the port mapping

port syntax

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

CONTAINER
:container
PRIVATE_PORT
: Container port
PROTO
: Protocol used by the port

Common wording of port
View the port mapping of my-boot container

docker port my-boot copy the code

View the host port mapped by port 8080 of the my-boot container

My Boot Port-Docker 8080 copy the code

View the host port mapped by port 8080 of the my-boot container using the tcp protocol

My Boot Port-Docker 8080/TCP duplicated code

export: package the container into a file

The export command can pack the container into a file, which is easier to confuse with the save command

The difference between export and save is that export is a container, and save is a mirror.

The export package is the snapshot of the container at the time, and the history and metadata information of the container will be lost. In addition, when the exported file is imported into a mirror, the name and version number of the mirror can be re-specified

export syntax

docker export [OPTIONS] CONTAINER duplicated code

OPTIONS
: Optional parameters
CONTAINER
:container

Common values of OPTIONS

-o
string: Specify the package file

Export commonly used wording
Pack the my-boot container into the my-boot.tar file

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

import: Import the image from a local file or a remote file to the local warehouse

Import can import mirrors from local files or remote files to the local warehouse

If it is imported from a file, this file needs to be the file exported by the export command

import syntax

Docker Import [the OPTIONS] File | the URL of | - [the REPOSITORY [: TAG]] Copy the code

OPTIONS
: Optional parameters
file
: File address
URL
: URL address
-
: Import from the standard input terminal, usually used with the cat command in linux
REPOSITORY
: Local mirror warehouse address
TAG
Mirror version number

Common values of OPTIONS

-m
string: Add description information
-c
list: Use the dokerfile command for the created container

Import commonly used wording
Create a mirror from the my-boot.tar file

/tmp/my-boot.tar CAT | Docker Import - Copy the code

Create image from my-boot.tar file

Docker Import/tmp/my-boot.tar copy the code

Create a mirror from the my-boot.tar file, and specify the mirror name as my-boot-test and the version number as 1.0

Docker Import/tmp/my-boot.tar Boot-My-Test: 1.0 copy the code

Create a mirror from the my-boot.tar file, the remarks are test, and specify the mirror name as my-boot-test and the version number as 1.0

Docker Import --message 'test'/tmp/my-boot.tar Boot-My-Test: 1.0 copy the code

Create a mirror from the my-boot.tar file of the remote server

Docker Import HTTP: //192.168.100.1:8080/images/my-boot.tar copy the code

The content comes from the official account

Helian Xiaowu
,Please indicate the source