文章目录
- docker常用命令总结
- 1、docker search:查找镜像
- 2、docker pull:从镜像仓库拉取镜像(不指定版本,默认拉取latest标签的)
- 3、docker ps:查看容器信息
- 4、docker run:利用基础镜像启动一个容器(本地没有则去docker hub上下载)
- 5、docker stop:停止一个或多个正在运行的容器,多个容器空格隔开
- 6、docker rm:删除一个或多个容器
- 7、docker exec:在正在运行的容器中运行命令
- 8、docker cp:拷贝文件在主机与容器之间(只需容器存在,不管容器状态)
- 9、docker logs:查看容器日志
docker常用命令总结
1、docker search:查找镜像
用法:docker search [OPTIONS] TERM
cyf@KobeBryant:~/command$ docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 11785 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1635 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 731 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 73
bitnami/nginx Bitnami nginx Docker Image 69 [OK]
.....
2、docker pull:从镜像仓库拉取镜像(不指定版本,默认拉取latest标签的)
cyf@KobeBryant:~/command$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
f5d23c7fed46: Pull complete
918b255d86e5: Pull complete
8c0120a6f561: Pull complete
Digest: sha256:eb3320e2f9ca409b7c0aa71aea3cf7ce7d018f03a372564dbdb023646958770b
Status: Downloaded newer image for nginx:latest
2.1、-a:下载镜像仓库所有标签的镜像
cyf@KobeBryant:~/command$ docker pull -a cuiyf/node
8.12.0-alpine: Pulling from cuiyf/node
4fe2ade4980c: Pull complete
eeb7d76f44e7: Pull complete
e35f88fcc259: Pull complete
221d2ad5c5a0: Pull complete
1295c0e77969: Pull complete
Digest: sha256:85ce6df5365c798dd23a89df47c6f056bbb24d1cbcf3fa679962dcd680120558
8.12.0: Pulling from cuiyf/node
Digest: sha256:85ce6df5365c798dd23a89df47c6f056bbb24d1cbcf3fa679962dcd680120558
Status: Downloaded newer image for cuiyf/node
3、docker ps:查看容器信息
3.1、查看运行中的容器
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
08abcf3e4eaf nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 80/tcp wonderful_matsumoto
3.2、-a:显示所有容器,包括运行的和停止的
cyf@KobeBryant:~/command$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
08abcf3e4eaf nginx "nginx -g 'daemon of…" 5 minutes ago Up 5 minutes 80/tcp wonderful_matsumoto
b0e07e712ecd gitlab/gitlab-runner:v11.1.0 "/usr/bin/dumb-init …" 18 hours ago Exited (0) 18 hours ago gitlab-runner
9af0d61d3dbc twang2218/gitlab-ce-zh:11.1 "/assets/wrapper" 19 hours ago Exited (0) 18 hours ago gitlab
3.3、-q:只显示容器id
cyf@KobeBryant:~/command$ docker ps -q
08abcf3e4eaf
3.4、-l:显示最新创建的一个容器(包括所有状态)
cyf@KobeBryant:~/command$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
08abcf3e4eaf nginx "nginx -g 'daemon of…" 7 minutes ago Up 7 minutes 80/tcp wonderful_matsumoto
3.5、-n:显现最新创建的容器个数
cyf@KobeBryant:~/command$ docker ps -n 2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
08abcf3e4eaf nginx "nginx -g 'daemon of…" 9 minutes ago Up 9 minutes 80/tcp wonderful_matsumoto
b0e07e712ecd gitlab/gitlab-runner:v11.1.0 "/usr/bin/dumb-init …" 18 hours ago Exited (0) 18 hours ago gitlab-runner
4、docker run:利用基础镜像启动一个容器(本地没有则去docker hub上下载)
用法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
,各个OPTIONS使用示例如下
4.1、–rm:容器退出或终止,将自动删除容器
cyf@KobeBryant:~/command$ docker run --rm nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
f5d23c7fed46: Pull complete
918b255d86e5: Pull complete
8c0120a6f561: Pull complete
Digest: sha256:eb3320e2f9ca409b7c0aa71aea3cf7ce7d018f03a372564dbdb023646958770b
Status: Downloaded newer image for nginx:latest
Ctrl + C
后容器退出,容器自动删除
^Ccyf@KobeBryant:~/command$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0e07e712ecd gitlab/gitlab-runner:v11.1.0 "/usr/bin/dumb-init …" 18 hours ago Exited (0) 18 hours ago gitlab-runner
9af0d61d3dbc twang2218/gitlab-ce-zh:11.1 "/assets/wrapper" 19 hours ago Exited (0) 18 hours ago gitlab
4.2、-d:在后台运行容器并打印容器ID
cyf@KobeBryant:~/command$ docker run -d nginx
625b11b160c5cc0fd4bc14d840bc44866ff6bd50ccd1235914ddff73b86be254
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
625b11b160c5 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 80/tcp epic_edison
4.3、-p:将容器的端口暴露给宿主机上的指定端口(主机端口:容器端口)
cyf@KobeBryant:~/command$ docker run -d -p 80:80 nginx
ae3756d2f212e0df6bc733ecdee3d59520a219ed549d51aef8842659b80fa4d4
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae3756d2f212 nginx "nginx -g 'daemon of…" 4 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp lucid_mcnulty
也可以限定ip,默认0.0.0.0
cyf@KobeBryant:~/command$ docker run -d -p 192.168.10.24:80:80 nginx
63beb1c2542d4d3c625f96465ab2db0a6c27cc05934dcf2fa88aa6a9e326f350
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63beb1c2542d nginx "nginx -g 'daemon of…" 2 seconds ago Up 1 second 192.168.10.24:80->80/tcp zealous_allen
也可以限定协议,默认TCP
cyf@KobeBryant:~/command$ docker run -d -p 80:80/udp nginx
2a1069375e56f95a5b946ccfffbc4a3feff66054206db1ff53a37678affec3e0
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a1069375e56 nginx "nginx -g 'daemon of…" 2 seconds ago Up 1 second 80/tcp, 0.0.0.0:80->80/udp vibrant_zhukovsky
4.4、-P:将容器的端口暴露给宿主机上的随机端口(主机端口:容器端口)
cyf@KobeBryant:~/command$ docker run -d -P nginx
53c9d47453466c1e613600c3ea4e48782781bdb6c9f77e1e8f8573c593f58cf8
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53c9d4745346 nginx "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 0.0.0.0:32768->80/tcp confident_keller
4.5、-v:
4.6、-e:设置环境变量
cyf@KobeBryant:~/command$ docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
bc6b59891989c12f9ea86985c2ee813ec412de88f69e13614ecb6ad4165819ee
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc6b59891989 mysql "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp dreamy_chaum
4.7、–name:设置容器名称,没有设置此option将随机分配一个名称
cyf@KobeBryant:~/command$ docker run -d -p 80:80 --name nginx nginx
d3a3dcff89dc466fd8c0fda7e24088f349e7baab626fb7b554649dd98f16b9ed
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3a3dcff89dc nginx "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp nginx
4.8、–restart:容器退出时的重启策略,默认不重启
4.8.1、–restart=no:默认策略,在容器退出时不重启容器
cyf@KobeBryant:~/command$ docker run -d -p 80:80 --name nginx --restart=no nginx
80350d1e93c720ac7dc9f5ba179f42cae6b9b601511e0dd79bcc7218bc66edb2
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80350d1e93c7 nginx "nginx -g 'daemon of…" 6 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp nginx
4.8.2、–restart=always:在容器退出时总是重启容器
cyf@KobeBryant:~/command$ docker run -d -p 3306:3306 --name mysql --restart=always mysql
4834856757c1a1ad87f2cc5ceb7e25e91142e43d04915bfd545bcfd5f8a0f2f8
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4834856757c1 mysql "docker-entrypoint.s…" 2 seconds ago Up Less than a second 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4834856757c1 mysql "docker-entrypoint.s…" 6 seconds ago Restarting (1) 1 second ago mysql
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4834856757c1 mysql "docker-entrypoint.s…" 12 seconds ago Restarting (1) 3 seconds ago mysql
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4834856757c1 mysql "docker-entrypoint.s…" 13 seconds ago Up Less than a second 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4834856757c1 mysql "docker-entrypoint.s…" 15 seconds ago Restarting (1) 1 second ago mysql
4.8.3、–restart=on-failure:3:在容器非正常退出时重启容器(退出状态非0),最多重启3次
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05e1189ab71c mysql "docker-entrypoint.s…" 3 seconds ago Restarting (1) Less than a second ago mysql
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05e1189ab71c mysql "docker-entrypoint.s…" 4 seconds ago Restarting (1) Less than a second ago mysql
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05e1189ab71c mysql "docker-entrypoint.s…" 4 seconds ago Restarting (1) Less than a second ago mysql
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cyf@KobeBryant:~/command$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05e1189ab71c mysql "docker-entrypoint.s…" 15 seconds ago Exited (1) 10 seconds ago mysql
b0e07e712ecd gitlab/gitlab-runner:v11.1.0 "/usr/bin/dumb-init …" 21 hours ago Exited (0) 20 hours ago gitlab-runner
9af0d61d3dbc twang2218/gitlab-ce-zh:11.1 "/assets/wrapper" 21 hours ago Exited (0) 20 hours ago gitlab
4.8.4、–restart=unless-stopped:在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器
cyf@KobeBryant:~/command$ docker run -d -p 3306:3306 --name mysql --restart=unless-stopped mysql
648d9a1773b14620b9551d4db3ef9f4e9f9dda601830a06e1d2cbbcd01f0c964
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
648d9a1773b1 mysql "docker-entrypoint.s…" 10 seconds ago Restarting (1) 1 second ago mysql
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
648d9a1773b1 mysql "docker-entrypoint.s…" 11 seconds ago Restarting (1) 1 second ago mysql
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
648d9a1773b1 mysql "docker-entrypoint.s…" 11 seconds ago Restarting (1) 2 seconds ago mysql
5、docker stop:停止一个或多个正在运行的容器,多个容器空格隔开
用法:docker stop 容器名/容器id
,停止后STATUS
为Exited
cyf@KobeBryant:~/command$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
625b11b160c5 nginx "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 80/tcp epic_edison
cyf@KobeBryant:~/command$ docker stop 625b11b160c5
625b11b160c5
cyf@KobeBryant:~/command$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
625b11b160c5 nginx "nginx -g 'daemon of…" 3 minutes ago Exited (0) 21 seconds ago epic_edison
b0e07e712ecd gitlab/gitlab-runner:v11.1.0 "/usr/bin/dumb-init …" 18 hours ago Exited (0) 18 hours ago gitlab-runner
9af0d61d3dbc twang2218/gitlab-ce-zh:11.1 "/assets/wrapper" 19 hours ago Exited (0) 18 hours ago gitlab
6、docker rm:删除一个或多个容器
用法:docker rm [OPTIONS] CONTAINER [CONTAINER...]
6.1、不加options,只能删除停止的容器
cyf@KobeBryant:~/command$ docker rm 625b11b160c5
625b11b160c5
cyf@KobeBryant:~/command$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0e07e712ecd gitlab/gitlab-runner:v11.1.0 "/usr/bin/dumb-init …" 18 hours ago Exited (0) 18 hours ago gitlab-runner
9af0d61d3dbc twang2218/gitlab-ce-zh:11.1 "/assets/wrapper" 19 hours ago Exited (0) 18 hours ago gitlab
6.2、-f:强制删除正在运行的容器
cyf@KobeBryant:~/command$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c77147e32c13 nginx "nginx -g 'daemon of…" 4 seconds ago Up 2 seconds 80/tcp eloquent_keldysh
b0e07e712ecd gitlab/gitlab-runner:v11.1.0 "/usr/bin/dumb-init …" 19 hours ago Exited (0) 18 hours ago gitlab-runner
9af0d61d3dbc twang2218/gitlab-ce-zh:11.1 "/assets/wrapper" 19 hours ago Exited (0) 18 hours ago gitlab
cyf@KobeBryant:~/command$ docker rm -f c77147e32c13
c77147e32c13
cyf@KobeBryant:~/command$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0e07e712ecd gitlab/gitlab-runner:v11.1.0 "/usr/bin/dumb-init …" 19 hours ago Exited (0) 18 hours ago gitlab-runner
9af0d61d3dbc twang2218/gitlab-ce-zh:11.1 "/assets/wrapper" 19 hours ago Exited (0) 18 hours ago gitlab
6.3、-l:删除指定的链接
6.4、-v:删除与容器关联的卷
7、docker exec:在正在运行的容器中运行命令
用法:docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
7.1、-it:通常-it在一起使用,
8、docker cp:拷贝文件在主机与容器之间(只需容器存在,不管容器状态)
8.1、拷贝容器内文件到宿主机(可以是文件,也可以是目录)
用法:docker cp 容器名/容器ID:/path/to/
cyf@KobeBryant:~/command$ docker cp master:/etc/mysql .
cyf@KobeBryant:~/command$ ls
mysql
8.2、拷贝宿主机文件到容器
cyf@KobeBryant:~/command$ docker cp test.txt master:/tmp
cyf@KobeBryant:~/command$ docker exec -it master bash
root@bf75e5fa4ad0:/# ls /tmp/
percona-xtrabackup-2.4.14-Linux-x86_64 percona-xtrabackup-2.4.14-Linux-x86_64.libgcrypt145.tar.gz test.sh test.txt xtrabackup.tar.gz
8.3、-a:存档模式(复制所有uid / gid信息)
8.4、-L:始终遵循SRC_PATH中的符号链接
9、docker logs:查看容器日志
9.1、-f:实时输出日志
cyf@KobeBryant:~/k8s$ docker logs nginx -f
172.17.0.1 - - [16/Sep/2019:06:41:40 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"
172.17.0.1 - - [16/Sep/2019:06:41:40 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://localhost/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"
2019/09/16 06:41:40 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost", referrer: "http://localhost/"
172.17.0.1 - - [16/Sep/2019:06:41:50 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"