Search K
Appearance
Appearance
❯ docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Common Commands:
run Create and run a new container from an image
exec Execute a command in a running container
ps List containers
build Build an image from a Dockerfile
pull Download an image from a registry
push Upload an image to a registry
images List images
login Log in to a registry
logout Log out from a registry
search Search Docker Hub for images
version Show the Docker version information
info Display system-wide information
Management Commands:
builder Manage builds
buildx* Docker Buildx (Docker Inc., v0.11.2)
compose* Docker Compose (Docker Inc., v2.21.0)
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
plugin Manage plugins
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Swarm Commands:
swarm Manage Swarm
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
export Export a container's filesystem as a tar archive
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Global Options:
--config string Location of client config files (default "/home/ubuntu/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket to connect to
-l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/home/ubuntu/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/ubuntu/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/ubuntu/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Run 'docker COMMAND --help' for more information on a command.
For more help on how to use Docker, head to https://docs.docker.com/go/guides/❯ docker image --help
Usage: docker image COMMAND
Manage images
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Download an image from a registry
push Upload an image to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Run 'docker image COMMAND --help' for more information on a command.❯ docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Execute a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Create and run a new container from an image
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.| 命令 | 说明 | 文档地址 |
|---|---|---|
docker pull | 拉取镜像 | docker pull |
docker push | 推送镜像到 DockerRegistry | docker push |
docker images | 查看本地镜像 | docker images |
docker rmi | 删除本地镜像 | docker rmi |
docker run | 创建并运行容器(不能重复创建) | docker run |
docker stop | 停止指定容器 | docker stop |
docker start | 启动指定容器 | docker start |
docker restart | 重新启动容器 | docker restart |
docker rm | 删除指定容器 | docker rm |
docker ps | 查看容器 | docker ps |
docker logs | 查看容器运行日志 | docker logs |
docker exec | 进入容器 | docker exec |
docker save | 保存镜像到本地压缩文件 | docker save |
docker load | 加载本地压缩文件到镜像 | docker load |
docker inspect | 查看容器详细信息 | docker inspect |

INFO
默认情况下,每次重启虚拟机我们都需要手动启动 Docker 和 Docker 中的容器。通过命令可以实现开机自启。
# Docker 开机自启
systemctl enable docker
# Docker 容器开机自启
docker update --restart=always [容器名/容器id]我们以 nginx 为例,演示一下常见命令的使用。
❯ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
578acb154839: Pull complete
e398db710407: Pull complete
85c41ebe6d66: Pull complete
7170a263b582: Pull complete
8f28d06e2e2e: Pull complete
6f837de2f887: Pull complete
c1dfc7e1671e: Pull complete
Digest: sha256:86e53c4c16a6a276b204b0fd3a8143d86547c967dc8258b3d47c3a21bb68d3c6
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest❯ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c20060033e06 37 hours ago 187MB
mysql latest a3b6608898d6 9 days ago 596MB
hello-world latest 9c7a54a9a43c 6 months ago 13.3kB❯ docker run -d \
--name nginx \
-p 80:80 \
nginx
29143181805fdc92ec8b9f1029fa709f1c4c9413c5991546100248fb9094eb6c❯ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29143181805f nginx "/docker-entrypoint.…" 53 seconds ago Up 52 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
0b9bf3779b50 mysql "docker-entrypoint.s…" 55 minutes ago Up 55 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
# 查看所有容器(包括已停止的)
❯ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29143181805f nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
0b9bf3779b50 mysql "docker-entrypoint.s…" 56 minutes ago Up 56 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
39805df2c14c hello-world "/hello" About an hour ago Exited (0) About an hour ago goofy_allen
# 查看最近一次运行的容器
❯ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29143181805f nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
# 输出内容格式化 docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"
❯ docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}"
CONTAINER ID NAMES IMAGE STATUS
29143181805f nginx nginx Up 4 minutes
0b9bf3779b50 mysql mysql Up 59 minutes
# 访问网页
❯ curl http://xxx.xxx.xxx.xxx:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>❯ docker stop nginx
nginx
❯ docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"
CONTAINER ID IMAGE PORTS STATUS NAMES
29143181805f nginx Exited (0) 14 seconds ago nginx
0b9bf3779b50 mysql 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp Up About an hour mysql
39805df2c14c hello-world Exited (0) About an hour ago goofy_allen
❯ curl http://xxx.xxx.xxx.xxx:80
curl: (7) Failed to connect to xxx.xxx.xxx.xxx port 80 after 3 ms: Connection refused❯ docker start nginx
nginx
❯ docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"
CONTAINER ID IMAGE PORTS STATUS NAMES
29143181805f nginx 0.0.0.0:80->80/tcp, :::80->80/tcp Up 3 seconds nginx
0b9bf3779b50 mysql 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp Up About an hour mysql
39805df2c14c hello-world Exited (0) About an hour ago goofy_allen❯ docker restart nginx
nginx
❯ docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"
CONTAINER ID IMAGE PORTS STATUS NAMES
29143181805f nginx 0.0.0.0:80->80/tcp, :::80->80/tcp Up 8 seconds nginx
0b9bf3779b50 mysql 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp Up About an hour mysql
39805df2c14c hello-world Exited (0) About an hour ago goofy_allen❯ docker inspect nginx
[
{
"Id": "29143181805fdc92ec8b9f1029fa709f1c4c9413c5991546100248fb9094eb6c",
"Created": "2023-11-02T17:51:17.487710841Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
......
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "de6d810f21370c22aa024539030d3c709302c684d54d4b5c9a3a1eec61d0da95",
"EndpointID": "f413a9233b761c49e5daa82bbe96d3bf415018d9125f27c282f4624dc07d65b2",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]❯ docker logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
......
2023/11/02 18:00:43 [notice] 1#1: using the "epoll" event method
2023/11/02 18:00:43 [notice] 1#1: nginx/1.25.3
2023/11/02 18:00:43 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2023/11/02 18:00:43 [notice] 1#1: OS: Linux 5.15.0-88-generic
2023/11/02 18:00:43 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/11/02 18:00:43 [notice] 1#1: start worker processes
2023/11/02 18:00:43 [notice] 1#1: start worker process 22
2023/11/02 18:00:43 [notice] 1#1: start worker process 23
2023/11/02 18:00:43 [notice] 1#1: start worker process 24
2023/11/02 18:00:43 [notice] 1#1: start worker process 25❯ docker exec -it nginx /bin/bash
root@29143181805f:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@29143181805f:/#
exit
❯ docker exec -it mysql mysql -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.2.0 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> ^DBye
❯ docker exec -it nginx /bin/ls -la
total 72
drwxr-xr-x 1 root root 4096 Nov 2 17:51 .
drwxr-xr-x 1 root root 4096 Nov 2 17:51 ..
-rwxr-xr-x 1 root root 0 Nov 2 17:51 .dockerenv
lrwxrwxrwx 1 root root 7 Oct 30 00:00 bin -> usr/bin
drwxr-xr-x 2 root root 4096 Sep 29 20:04 boot
drwxr-xr-x 5 root root 340 Nov 2 18:00 dev
drwxr-xr-x 1 root root 4096 Nov 1 05:12 docker-entrypoint.d
-rwxrwxr-x 1 root root 1620 Nov 1 05:11 docker-entrypoint.sh
drwxr-xr-x 1 root root 4096 Nov 2 17:51 etc
drwxr-xr-x 2 root root 4096 Sep 29 20:04 home
lrwxrwxrwx 1 root root 7 Oct 30 00:00 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Oct 30 00:00 lib32 -> usr/lib32
lrwxrwxrwx 1 root root 9 Oct 30 00:00 lib64 -> usr/lib64
lrwxrwxrwx 1 root root 10 Oct 30 00:00 libx32 -> usr/libx32
drwxr-xr-x 2 root root 4096 Oct 30 00:00 media
drwxr-xr-x 2 root root 4096 Oct 30 00:00 mnt
drwxr-xr-x 2 root root 4096 Oct 30 00:00 opt
dr-xr-xr-x 233 root root 0 Nov 2 18:00 proc
drwx------ 1 root root 4096 Nov 2 18:04 root
drwxr-xr-x 1 root root 4096 Nov 2 18:00 run
lrwxrwxrwx 1 root root 8 Oct 30 00:00 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Oct 30 00:00 srv
dr-xr-xr-x 13 root root 0 Nov 2 18:00 sys
drwxrwxrwt 1 root root 4096 Nov 1 05:12 tmp
drwxr-xr-x 1 root root 4096 Oct 30 00:00 usr
drwxr-xr-x 1 root root 4096 Oct 30 00:00 vardocker stop),否则会报错-f 参数强制删除# 强制删除
❯ docker rm nginx
Error response from daemon: You cannot remove a running container 29143181805fdc92ec8b9f1029fa709f1c4c9413c5991546100248fb9094eb6c. Stop the container before attempting removal or force remove
❯ docker rm nginx -f
nginx
❯ docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}"
CONTAINER ID NAMES IMAGE STATUS
0b9bf3779b50 mysql mysql Up About an hour
39805df2c14c goofy_allen hello-world Exited (0) About an hour ago
❯ docker rm mysql # 删除容器
Error response from daemon: You cannot remove a running container 0b9bf3779b5036c83b9a390cef5c860ad2d8d32c2bb6f69f6a450b0031a09570. Stop the container before attempting removal or force remove
❯ docker stop mysql # 停止容器
mysql
❯ docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}" # 查看容器
CONTAINER ID NAMES IMAGE STATUS
0b9bf3779b50 mysql mysql Exited (0) 7 seconds ago
39805df2c14c goofy_allen hello-world Exited (0) About an hour ago
❯ docker rm mysql
mysql
❯ docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}"
CONTAINER ID NAMES IMAGE STATUS
39805df2c14c goofy_allen hello-world Exited (0) About an hour ago❯ docker rmi --help # 查看帮助
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Aliases:
docker image rm, docker image remove, docker rmi
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
❯ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c20060033e06 37 hours ago 187MB
mysql latest a3b6608898d6 9 days ago 596MB
hello-world latest 9c7a54a9a43c 6 months ago 13.3kB
❯ docker rmi c20060033e06 # 删除镜像(或者使用 `docker rmi nginx:latest`)
Untagged: nginx:latest
Untagged: nginx@sha256:86e53c4c16a6a276b204b0fd3a8143d86547c967dc8258b3d47c3a21bb68d3c6
Deleted: sha256:c20060033e06f882b0fbe2db7d974d72e0887a3be5e554efdb0dcf8d53512647
Deleted: sha256:32cf0b3c70998330104acfc1a02f6aae2f8a728bc6ad91c2415fda501593fa81
Deleted: sha256:79cae367d5d42d54224c249db6a06234d077ea32168bed4261141c1016469623
Deleted: sha256:32b31b2b0563a28eb36f90bcd835540ead275b63cf48ed85e998a8df0b1845ff
Deleted: sha256:203226d708244adf6c392a28e22e3a7e276270da43094205137c404fc9949691
Deleted: sha256:9969d65d0618198425e0be29ec6194be71b36c856ce9fe2142dd54163aef6eae
Deleted: sha256:e9c1515f88f6e5652bde5583f0d1ddb7dd4097380aa66d90b722c53fbea3ebab
Deleted: sha256:ec983b16636050e69677eb81537e955ab927757c23aaf73971ecf5f71fcc262a
❯ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest a3b6608898d6 9 days ago 596MB
hello-world latest 9c7a54a9a43c 6 months ago 13.3kB❯ docker save --help # 查看帮助
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Aliases:
docker image save, docker save
Options:
-o, --output string Write to a file, instead of STDOUT
❯ docker save -o hello-world.tar hello-world # 保存镜像到本地压缩文件
❯ ls -la hello-world.tar
-rw------- 1 ubuntu ubuntu 24064 Nov 3 02:13 hello-world.tar
❯ docker images # 查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest a3b6608898d6 9 days ago 596MB
hello-world latest 9c7a54a9a43c 6 months ago 13.3kB
❯ docker rmi hello-world:latest # 删除镜像
Untagged: hello-world:latest
Untagged: hello-world@sha256:88ec0acaa3ec199d3b7eaf73588f4518c25f9d34f58ce9a0df68429c5af48e8d
Deleted: sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d
Deleted: sha256:01bb4fce3eb1b56b05adf99504dafd31907a5aadac736e36b27595c8b92f07f1
❯ docker images # 查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest a3b6608898d6 9 days ago 596MB❯ docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Aliases:
docker image load, docker load
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
❯ docker load -i hello-world.tar # 加载本地压缩文件到镜像
01bb4fce3eb1: Loading layer [==================================================>] 14.85kB/14.85kB
Loaded image: hello-world:latest
❯ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest a3b6608898d6 9 days ago 596MB
hello-world latest 9c7a54a9a43c 6 months ago 13.3kBdocker pull nginx 或 docker pull nginx:latestdocker images 或 docker image lsdocker run -d --name nginx -p 80:80 nginxdocker ps -adocker stop nginxdocker start nginxdocker exec -it nginx /bin/bashdocker stop nginx && docker rm nginx 或 docker rm nginx -f❯ dkpl nginx:1.25.3-alpine # docker pull nginx:1.25.3-alpine 拉取 nginx 镜像
1.25.3-alpine: Pulling from library/nginx
96526aa774ef: Already exists
740091335c74: Already exists
da9c2e764c5b: Already exists
ade17ad21ef4: Already exists
4e6f462c8a69: Already exists
1324d9977cd2: Already exists
1b9b96da2c74: Already exists
153aef7ca07f: Already exists
Digest: sha256:db353d0f0c479c91bd15e01fc68ed0f33d9c4c52f3415e63332c3d0bf7a4bb77
Status: Downloaded newer image for nginx:1.25.3-alpine
docker.io/library/nginx:1.25.3-alpine
❯ dkIls # docker images 查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.25.3-alpine b135667c9898 4 weeks ago 47.7MB
❯ dk run -d --name nginx -p 80:80 nginx:1.25.3-alpine # 创建并运行 nginx 容器
4d3b9eff4034c62b043b842ebb4945dae3a5d1a25d6e982b3c9b146b9494266e
❯ dkpsf -a # docker ps -a 查看容器
CONTAINER ID IMAGE PORTS STATUS NAMES
4d3b9eff4034 nginx:1.25.3-alpine 0.0.0.0:80->80/tcp, :::80->80/tcp Up 9 seconds nginx
❯ dk stop nginx # docker stop nginx 停止容器
nginx
❯ dk start nginx # docker start nginx 启动容器
nginx
❯ dex nginx whoami # docker exec nginx whoami 进入容器查看当前用户
root
❯ dex nginx which sh # docker exec nginx which sh 进入容器查看 sh 路径
/bin/sh
❯ dk stop nginx && dk rm nginx # docker stop nginx && docker rm nginx 删除容器
nginx
❯ dkpsf -a # docker ps -a 查看容器
CONTAINER ID IMAGE PORTS STATUS NAMES