1. Container life cycle management
(1)docker run
Command description
Create a new container and run a command
grammar
docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
OPTIONS Description:
-a stdin: Specify the standard input and output content type,Optional STDIN/STDOUT/STDERR Three items; -d: Run containers in the background,and return to the containerID; -i: Run containers in interactive mode,Usually with -t Use simultaneously; -P: Random port mapping,The container's internal ports are randomly mapped to the host's ports -p: Specify port mapping,The format is:Host(Host)port:容器port -t: Reassign a pseudo-input terminal to the container,Usually with -i Use simultaneously; --name="nginx-lb": Assign a name to the container; --dns 8.8.8.8: Specify the container to useDNSserver,默认和Host一致; --dns-search : Specify containerDNSSearch for domain names,默认和Host一致; -h "mars": Specify container的hostname; -e username="ritchie": Set environment variables; --env-file=[]: Reading environment variables from the specified file; --cpuset="0-2" or --cpuset="0,1,2": Bind the container to the specifiedCPUrun; -m :Set the maximum memory value of the container; --net="bridge": Specify container的网络连接类型,support bridge/host/none/container: Four types; --link=[]: Add link to another container; --expose=[]: 开放一个port或一组port; --volume , -v: Bind a volume
Common examples
Use docker image fate:latest background mode to start a container and name the container myfate.
docker run --name myfate -d fate:latest
Start a container using mirror fate:latest background mode and map the container's port 80 to the host random port.
docker run -P -d fate:latest
Using the mirror fate:latest, start a container in the background mode, map the container's port 80 to the host's port 80, and the host's directory /data is mapped to the container's /data.
docker run -p 80:80 -v /data:/data -d fate:latest
Bind port 8080 of the container and map it to port 80 of localhost 127.0.0.1.
$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
Use mirror fate:latest to start a container in interactive mode, and execute the /bin/bash command inside the container.
wh@wh-pc:~$ docker run -it fate:latest /bin/bash root@b8573233d675:/#
(2)start/stop/restart
Command description:
docker start
: Start one or more containers that have been stopped
docker stop
:Stop a running container
docker restart
:Restart the container
How to use:
Start the stopped container myfate
docker start myfate
Stop running container myfate
docker stop myfate
Restart the container myfate
docker restart myfate
(3)docker kill
Command description
Kill a running container.
Example
Kill the running container myfate
wh@wh-pc:~$ docker kill -s KILL myfate
(4)docker rm
Command description
Delete one or more containers
grammar
docker rm [OPTIONS] CONTAINER [CONTAINER…]
OPTIONS Description:
-f :pass SIGKILL Signal force deletion of a running container。 -l :Remove network connections between containers,Not the container itself。 -v :Delete the volume associated with the container。
Common examples
Forced deletion of containers fate01 and fate02:
docker rm -f fate01 fate02
Remove the connection of container fate01 to container fate02, the connection name db:
docker rm -l db
Delete the container fate, and delete the data volume mounted by the container:
docker rm -v fate
Delete all stopped containers:
docker rm $(docker ps -a -q)
Kill all running containers
docker kill $(docker ps -a -q)
Delete all stopped containers
docker rm $(docker ps -a -q)
Delete all images without dangling tags
docker rmi $(docker images -q -f dangling=true)
Delete the specified image by the image's id
docker rmi <image id>
Delete all images
docker rmi $(docker images -q)
(5)pause/unpause
Command descriptiondocker pause
: Pause all processes in the container.docker unpause
:Restore all processes in the container.
grammar
docker pause CONTAINER [CONTAINER…]
docker unpause CONTAINER [CONTAINER…]
Common examples
Pauses the service provided by the database container fate.
docker pause fate
Recover the database container fate provides services.
docker unpause fate
(6)create
Command descriptiondocker create
: Create a new container but not start it
The same as docker run
grammar
docker create [OPTIONS] IMAGE [COMMAND] [ARG…]
Syntax is the same as docker run
Common examples
Create a container using docker image fate:latest and name the container myfate
wh@wh-pc:~$ docker create --name myfate fate:latest
(7)docker exec
Command description
Execute commands in running container
grammar
docker exec [OPTIONS] CONTAINER COMMAND [ARG…]
OPTIONS Description:-d
:Separate mode: Run in the background-i
: Keep STDIN on even if there is no attachment-t
: Assign a pseudo-terminal
Common examples
Execute the /root/ script in interactive mode in container myfate:
wh@wh-pc:~$ docker exec -it myfate /bin/sh /root/
Open an interactive mode terminal in the container fate:
wh@:~$ docker exec -i -t myfate /bin/bash
You can also view the running container through the docker ps -a command and then enter the container using the container ID.
View the container ID that is already running:
# docker ps -a ... 9df70f9a0714 openjdk "/usercode/…" ...
The 9df70f9a0714 in the first column is the container ID.
Execute bash on the specified container via the exec command:
# docker exec -it 9df70f9a0714 /bin/bash
(8)docker ps
Command description
List containers
grammar
docker ps [OPTIONS]
OPTIONS Description:-a
: Show all containers, including those not running.-f
:Filter the displayed content according to the conditions.–format
: Specifies the template file that returns the value.-l
: Shows the recently created container.-n
: List n containers that have been recently created.–no-trunc
:Do not truncate the output.-q
:Silent mode, only display container number.-s
: Shows the total file size.
Common examples
Lists all running container information.
wh@wh-pc:~$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES 09b93464c2f7 fate:latest "fate -g 'daemon off" ... 80/tcp, 443/tcp myfate 96f7f14e99ab mysql:5.6 "" ... 0.0.0.0:3306->3306/tcp mymysql
Output details:CONTAINER ID
: Container ID.IMAGE
: The mirror used.COMMAND
: Commands that run when starting the container.CREATED
: The creation time of the container.STATUS
: Container status.
There are 7 states:
created
(Created)
restarting
(Restarting)
running
(In operation)
removing
(Migrating)
paused
(pause)
exited
(stop)
dead
(die)
PORTS
: Port information of the container and the connection type used (tcp\udp).NAMES
: Automatically allocated container name.
Lists the 5 most recently created container information.
wh@whpc:~$ docker ps -n 5 CONTAINER ID IMAGE COMMAND CREATED 09b93464c2f7 fate:latest "fate -g 'daemon off" 2 days ago ... b8573233d675 fate:latest "/bin/bash" 2 days ago ... b1a0703e41e7 fate:latest "fate -g 'daemon off" 2 days ago ... f46fb1dec520 5c6e1090e771 "/bin/sh -c 'set -x \t" 2 days ago ... a63b4a5597de 860c279d2fec "bash" 2 days ago ..
Filter by tag
$ docker run -d --name=test-nginx --label color=blue nginx $ docker ps --filter "label=color" $ docker ps --filter "label=color=blue"
Filter by name
$ docker ps --filter"name=test-nginx"
Filter by status
$ docker ps -a --filter 'exited=0' $ docker ps --filter status=running $ docker ps --filter status=paused
Filter by mirror
#Mirror Name$ docker ps --filter ancestor=nginx #Mirror ID$ docker ps --filter ancestor=d0e008c6cf02
Filter according to startup sequence
$ docker ps -f before=9c3527ed70ce $ docker ps -f since=6e63f6ff38b0
(9)docker inspect
Command description
docker inspect: Get the metadata of the container/mirror.
grammar
docker inspect [OPTIONS] NAME|ID [NAME|ID…]
OPTIONS Description:-f
: Specifies the template file that returns the value.-s
: Shows the total file size.–type
: Returns JSON for the specified type.
Common examples
Get the meta information of the mirror fate:1.6.
wh@wh-pc:~$ docker inspect fate:1.6 [ { "Id": "sha256:2c0964ec182ae9a045f866bbc2553087f6e42bfc16074a74fb820af235f070ec", "RepoTags": [ "fate:1.6" ], "RepoDigests": [], "Parent": "", "Comment": "", "Created": "2016-05-24T04:01:41.168371815Z", "Container": "e0924bc460ff97787f34610115e9363e6363b30b8efa406e28eb495ab199ca54", "ContainerConfig": { "Hostname": "b0cf605c7757", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "3306/tcp": {} }, ...
Get the IP of the running container mymysql.
wh@wh-pc:~$ docker inspect --format='{{range .}}{{.IPAddress}}{{end}}' myfate 192.17.0.3
(10)top
Command description
View the process information running in the container and supports the ps command parameter.
Common examples
wh@wh-pc:~/mysql$ docker top mysql UID PID PPID C STIME TTY TIME CMD 999 40347 40331 18 00:58 ? 00:00:02 mysqld
The above is the detailed content of the commonly used Docker commands and examples summary analysis. For more information about Docker commands and examples summary, please pay attention to my other related articles!