Subscribe to "Cloud Native Core Technology" column
Docker common command summary
-
- 1 Help command
- 2 Mirror command
- 3 Container Commands
-
- 3.1 Create a new container and run
- 3.2 List containers
- 3.3 Exit the container
- 3.4 Start the container
- 3.5 Restart the container
- 3.6 Stop the container
- 3.7 Force stop the container
- 3.8 Deleting a stopped container
- 3.9 Start the daemon container
- 3.10 Viewing container logs
- 3.11 View the processes running in the container
- 3.12 View the internal details of the container
- 3.13 Entering a running container and interacting with the command line
- 3.14 Copying files from the container to the host
- 4 Command Summary
1 Help command
-
docker version
-
docker info
-
docker --help
2 Mirror command
2.1 List mirrors
docker images [OPTIONS] [REPOSITORY[:TAG]]
- REPOSITORY : Repository source representing the mirror
- TAG : the tag of the mirror
- IMAGE ID : Image ID
- CREATED : Image creation time
- SIZE : Image size
The same repository source can have multiple TAGs, representing different versions of the repository source. We use REPOSITORY:TAG to define different images.
If you don't specify a version tag for an image, e.g. you only use ubuntu, docker will use the ubuntu:latest image by default
OPTIONS Description:
-a
: List all local images (including intermediate image layers, by default, the intermediate image layers are filtered out);
--digests
: Display the summary information of the mirror;
-f
: Display images that meet the conditions;
The currently supported filter configuration key is
- dangling: Displays images marked as empty, with values only true and false
docker images -f dangling=true
- label: This is to filter according to the label, where the value of label is configured by docker when compiling or configured in the Dockerfile
- before: This is to filter according to time, where the value of before represents the image list before a certain image construction time
docker images -f before=true
- since: just the opposite of before, it means the image reference built after a certain image is built: this is to add a regular to match
docker images -f since=true
- reference: This is to add a regular to match
docker images --filter reference=*:*
--format
: The template file specifying the return value;
docker images --format "{ {.ID}}\t{ {.Repository}}"
--no-trunc
: Display complete image information;
-q
: Only display the mirror ID.
2.2 Find mirrors
docker search [OPTIONS] TERM
- docker search image name
- docker search [OPTIONS] Image name
--no-trunc
: Show full image description
-s
: List images whose collections are not less than the specified value.
--automated
: List only images of type automated build;
--filter
,-f
: Filter output based on given conditions
--format
: Display output using template formatting
--limit
: Max number of search results, default 25
--no-trunc
: Disable output truncation
2.3 Download mirror
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
- docker pull image name
- docker pull image name[:TAG]
2.4 Delete mirror
docker rmi [OPTIONS] IMAGE [IMAGE...]
- docker rmi -f image ID
- docker rmi -f image name 1:TAG image name 2:TAG
- docker rmi -f $(docker images -qa)
3 Container Commands
为了更好的学习和演示,我在这里下载一个ubuntu的镜像
3.1 Create a new container and run
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS description:
-a
stdin: specifies the content type of standard input and output, three options: STDIN/STDOUT/STDERR;
-d
: Run the container in the background and return the container ID;
-i
: run the container in interactive mode, usually used with -t;
-P
: Random port mapping, the internal port of the container is randomly mapped to the port of the host
-p
: Specify the port mapping, the format is: host (host) port: container port
-t
: reassigns a pseudo input terminal for the container, usually used with -i;
--name="nginx-lb"
: specify a name for the container;
--dns 8.8.8.8
: Specify the DNS server used by the container, the default is the same as the host;
--dns-search example.com
: Specify the container DNS search domain name, the default is the same as the host;
-h "mars"
: Specify the hostname of the container;
-e username="ritchie"
: set environment variables;
--env-file=[]
: Read environment variables from the specified file;
--cpuset="0-2" or --cpuset="0,1,2"
: Bind the container to run on the specified CPU;
-m
: Set the maximum memory used by the container;
--net="bridge"
: Specify the network connection type of the container, support bridge/host/none/container: four types;
--link=[]
: add a link to another container;
--expose=[]
: Open a port or a group of ports;
--volume
, -v
: bind a volume
实例
- Start a container in background mode using the docker image nginx:latest and name the container mynginx.
docker run --name mynginx -d nginx:latest
- Start a container in background mode using the mirror nginx:latest and map the container's port 80 to a random port on the host.
docker run -P -d nginx:latest
- Use the mirror nginx:latest to start a container in background mode, map port 80 of the container to port 80 of the host, and map the directory /data of the host to /data of the container.
docker run -p 80:80 -v /data:/data -d nginx:latest
- Bind the container's port 8080 and map it to port 80 on the localhost 127.0.0.1.
docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
- Use the image nginx:latest to start a container in interactive mode and execute the /bin/bash command inside the container.
docker run -it nginx:latest /bin/bash
- Start an interactive container
docker run -it centos /bin/bash
3.2 List containers
docker ps [OPTIONS]
OPTIONS Description:
-a
: Show all containers, including not running ones.
-f
: Filter the displayed content based on conditions.
--format
: Specifies the template file for the return value.
-l
: Show recently created containers.
-n
: List the n most recently created containers.
--no-trunc
: Do not truncate output.
-q
: silent mode, only the container number is displayed.
-s
: Displays the total file size.
3.3 Exit the container
- container stops exiting
exit
- Container does not stop exiting
ctrl+P+Q
3.4 Start the container
docker start 容器ID或者容器名
3.5 Restart the container
docker restart 容器ID或者容器名
3.6 Stop the container
docker stop 容器ID或者容器名
3.7 Force stop the container
docker kill 容器ID或者容器名
3.8 Deleting a stopped container
docker rm 容器ID
To delete multiple containers at once:
docker rm -f $(docker ps -a -q)
docker ps -a -q | xargs docker rm
3.9 Start the daemon container
docker run -d 容器名
Start a container in background mode using the image ubuntu:latest
docker run -d ubuntu
Problem : Check with docker ps -a, you will find that the container has exited
An important point to note: For a Docker container to run in the background, there must be a foreground process.
If the commands run by the container are not those that have been suspended (such as running top, tail), they will automatically exit.
This is the mechanism of docker, such as your web container, we take nginx as an example, under normal circumstances, we only need to start the response service when configuring the startup service.
E.gservice nginx start
However, in this way, nginx runs in the background process mode, which leads to no application running in the docker foreground.
After such a container starts in the background, it will immediately commit suicide because he thinks he has nothing to do.So, the best solution is to run the program you want to run as a foreground process
3.10 Viewing container logs
docker logs -f -t --tail 容器ID
-t
is the join timestamp-f
Follow the latest log print--tail
Numbers show how many last
3.11 View the processes running in the container
docker top 容器ID
3.12 View the internal details of the container
docker inspect 容器ID
3.13 Entering a running container and interacting with the command line
docker exec -it 容器ID bash
重新进入docker attach 容器ID
The above two differences
- attach directly enters the terminal of the container startup command and does not start a new process
- exec is to open a new terminal in the container and can start a new process
3.14 Copying files from the container to the host
docker cp 容器ID:容器内路径 目的主机路径
4 Command Summary
project | Value |
---|---|
attach Attach to a running container |
The attach connection under the current shell specifies the running image |
build Build an image from a Dockerfile |
Customize the image via Dockerfile |
commit Create a new image from a container changes |
Commit the current container as a new image |
cp Copy files/folders from the containers filesystem to the host path |
Copy the specified file or directory from the container to the host |
create Create a new container |
Create a new container, same as run, but do not start the container |
diff Inspect changes on a container’s filesystem |
View docker container changes |
events Get real time events from the server |
Get container real-time events from docker service |
exec Run a command in an existing container |
Run a command on an existing container |
export Stream the contents of a container as a tar archive |
Export the content stream of the container as a tar archive [corresponds to import] |
history Show the history of an image |
Show a mirror formation history |
images List images |
List the current image of the system |
import Create a new filesystem image from the contents of a tarball |
#Create a new filesystem image from the contents of the tar package [corresponding to export] |
info Display system-wide information |
#Display system related information |
inspect Return low-level information on a container |
View container details |
kill Kill a running container |
kill specifies the docker container |
load Load an image from a tar archive |
Load an image from a tarball [corresponds to save] |
login Register or Login to the docker registry server |
Register or log in to a docker source server |
logout Log out from a Docker registry server |
Exit from the current Docker registry |
logs Fetch the logs of a container |
Output current container log information |
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT |
View the internal source port of the container corresponding to the mapped port |
pause Pause all processes within a container |
Pause the container |
ps List containers |
list containers |
pull Pull an image or a repository from the docker registry server |
Pull the specified image or library image from the docker image source server |
push Push an image or a repository to the docker registry server |
Push the specified image or library image to the docker source server |
restart Restart a running container |
Restart running containers |
rm Remove one or more containers |
remove one or more containers |
rmi Remove one or more images |
Remove one or more images [no container can use the image to delete, otherwise you need to delete the relevant container to continue or -f force deletion] |
run Run a command in a new container |
Create a new container and run a command |
save Save an image to a tar archive |
Save an image as a tar package [corresponding to load] |
search Search for an image on the Docker Hub |
Search for images in docker hub |
start Start a stopped containers |
start the container |
stop Stop a running containers |
stop container |
tag Tag an image into a repository |
Tag the image in the source |
top Lookup the running processes of a container |
View information about processes running in a container |
unpause Unpause a paused container |
Unpause the container |
version Show the docker version information |
View docker version number |
wait Block until a container stops, then print its exit code |
Intercept the exit status value when the container is stopped |