普拉多VX

人生一路,不问来时,不知归期

0%

docker 基础命令使用

docker命令

用于搜索docker registory中镜像信息

其中第一个(没有前缀的称为顶级/顶层仓库),一般是docker官方的仓库。后面xxxx/nginx表示用户仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
luodi@roddydeMacBook-Pro:~$ docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 13590 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1857 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 782 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 126
bitnami/nginx Bitnami nginx Docker Image 88 [OK]
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 87 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 77
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 72 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 59 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 53 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 37
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 31 [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 17
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 14
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 13
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
raulr/nginx-wordpress Nginx front-end for the official wordpress:f… 13 [OK]
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 13 [OK]
mailu/nginx Mailu nginx frontend 7 [OK]
bitwarden/nginx The Bitwarden nginx web server acting as a r… 7
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK]
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 6 [OK]
wodby/nginx Generic nginx 1 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 1 [OK]
luodi@roddydeMacBook-Pro:~$

默认安装的镜像大部分用的centos/ubuntu,如果需要自定义,大部分都是自己制作镜像。

alpine版本:专门用于制作的小镜像
stable版本: 稳定版本镜像
latest: 最新版本镜像

pull

pull 用于下载镜像

1
2
3
4
5
6
7
8
luodi@roddydeMacBook-Pro:~$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
bf5952930446: Pull complete
ba755a256dfe: Downloading [===============================================> ] 24.88MB/26.34MB
c57dd87d0b93: Download complete
d7fbf29df889: Download complete
1f1070938ccd: Download complete

images

列出本地下载的镜像,该命令和docker image ls 效果一致。

1
2
3
4
5
6
7
8
9
10
11
luodi@roddydeMacBook-Pro:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 08393e824c32 6 days ago 132MB
cfcontorl 1.0.0 1a1b058798ce 4 weeks ago 97.6MB
<none> <none> 252cf95e99e7 4 weeks ago 97.6MB
ubuntu latest adafef2e596e 5 weeks ago 73.9MB
zookeeper latest 6982b35ff928 6 weeks ago 252MB
mysql 5.7.30 9cfcce23593a 2 months ago 448MB
mysql latest be0dbf01a0f3 2 months ago 541MB
wurstmeister/kafka latest f1905dce9659 3 months ago 431MB
mysql 8.0.19 0c27e8e5fcfa 3 months ago 546MB

“ 这种镜像在Docker官方文档中被称作dangling images,指的是没有标签并且没有被容器使用的镜像。

rm

删除一个容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
luodi@roddydeMacBook-Pro:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5abc6ea0c7f8 wurstmeister/kafka "start-kafka.sh" 4 weeks ago Exited (255) 3 weeks ago 0.0.0.0:9092->9092/tcp kafka
a728e2d2d00e zookeeper "/docker-entrypoint.…" 4 weeks ago Exited (255) 3 weeks ago 2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:32768->2181/tcp zookeeper
32dead838953 adafef2e596e "/bin/sh -c 'apt-get…" 4 weeks ago Exited (100) 4 weeks ago jolly_ellis
186f3d1a87c2 adafef2e596e "/bin/sh -c 'apt-get…" 4 weeks ago Exited (100) 4 weeks ago objective_mendeleev
81a7d83a1333 cfcontorl:1.0.0 "/opt/Cfcontorl" 4 weeks ago Exited (1) 4 weeks ago cfcontorl
392dce6fffe1 mysql:5.7.30 "docker-entrypoint.s…" 5 weeks ago Up 3 weeks 33060/tcp, 0.0.0.0:3307->3306/tcp mysql5
luodi@roddydeMacBook-Pro:~$ docker rm cfcontorl
cfcontorl
luodi@roddydeMacBook-Pro:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5abc6ea0c7f8 wurstmeister/kafka "start-kafka.sh" 4 weeks ago Exited (255) 3 weeks ago 0.0.0.0:9092->9092/tcp kafka
a728e2d2d00e zookeeper "/docker-entrypoint.…" 4 weeks ago Exited (255) 3 weeks ago 2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:32768->2181/tcp zookeeper
32dead838953 adafef2e596e "/bin/sh -c 'apt-get…" 4 weeks ago Exited (100) 4 weeks ago jolly_ellis
186f3d1a87c2 adafef2e596e "/bin/sh -c 'apt-get…" 4 weeks ago Exited (100) 4 weeks ago objective_mendeleev
392dce6fffe1 mysql:5.7.30 "docker-entrypoint.s…" 5 weeks ago Up 3 weeks 33060/tcp, 0.0.0.0:3307->3306/tcp mysql5

rmi

删除一个镜像

1
2
3
4
5
6
7
8
9
luodi@roddydeMacBook-Pro:~$ docker rmi --help

Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Options:
-f, --force Force removal of the image //强制删除一个镜像
--no-prune Do not delete untagged parents

删除

1
2
3
4
5
luodi@roddydeMacBook-Pro:~$ docker rmi busybox
Untagged: busybox:latest
Untagged: busybox@sha256:4f47c01fa91355af2865ac10fef5bf6ec9c7f42ad2321377c21e844427972977
Deleted: sha256:018c9d7b792b4be80095d957533667279843acf9a46c973067c8d1dff31ea8b4
Deleted: sha256:514c3a3e64d4ebf15f482c9e8909d130bcd53bcc452f0225b0a04744de7b8c43

ps (container ls)

列出容器

1
2
ps -a   //all 列出所有容器
ps // 列出正在运行的容器
1
2
3
4
5
6
7
8
luodi@roddydeMacBook-Pro:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5abc6ea0c7f8 wurstmeister/kafka "start-kafka.sh" 4 weeks ago Exited (255) 3 weeks ago 0.0.0.0:9092->9092/tcp kafka
a728e2d2d00e zookeeper "/docker-entrypoint.…" 4 weeks ago Exited (255) 3 weeks ago 2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:32768->2181/tcp zookeeper
32dead838953 adafef2e596e "/bin/sh -c 'apt-get…" 4 weeks ago Exited (100) 4 weeks ago jolly_ellis
186f3d1a87c2 adafef2e596e "/bin/sh -c 'apt-get…" 4 weeks ago Exited (100) 4 weeks ago objective_mendeleev
81a7d83a1333 cfcontorl:1.0.0 "/opt/Cfcontorl" 4 weeks ago Exited (1) 4 weeks ago cfcontorl
392dce6fffe1 mysql:5.7.30 "docker-entrypoint.s…" 5 weeks ago Up 3 weeks 33060/tcp, 0.0.0.0:3307->3306/tcp mysql5
1
2
3
4
luodi@roddydeMacBook-Pro:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
392dce6fffe1 mysql:5.7.30 "docker-entrypoint.s…" 5 weeks ago Up 3 weeks 33060/tcp, 0.0.0.0:3307->3306/tcp mysql5
luodi@roddydeMacBook-Pro:~$

start

启动一个容器

1
2
3
4
5
6
luodi@roddydeMacBook-Pro:~$ docker start zookeeper
zookeeper
luodi@roddydeMacBook-Pro:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a728e2d2d00e zookeeper "/docker-entrypoint.…" 4 weeks ago Up 2 seconds 2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:32768->2181/tcp zookeeper
392dce6fffe1 mysql:5.7.30 "docker-entrypoint.s…" 5 weeks ago Up 3 weeks 33060/tcp, 0.0.0.0:3307->3306/tcp mysql5

restart

重启一个容器

1
2
3
luodi@roddydeMacBook-Pro:~$ docker restart zookeeper
zookeeper
luodi@roddydeMacBook-Pro:~$

stop(-15)/kill(-9)

stop 停止一个或者多个容器
kill 强制停止一个或多个容器(不建议生产使用)

1
2
3
4
5
6
luodi@roddydeMacBook-Pro:~$ docker stop zookeeper
zookeeper
luodi@roddydeMacBook-Pro:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
392dce6fffe1 mysql:5.7.30 "docker-entrypoint.s…" 5 weeks ago Up 3 weeks 33060/tcp, 0.0.0.0:3307->3306/tcp mysql5
luodi@roddydeMacBook-Pro:~$
1
2
3
4
5
6
7
8
9
10
luodi@roddydeMacBook-Pro:~$ docker kill zookeeper
zookeeper
luodi@roddydeMacBook-Pro:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
efafc195cc87 nginx:latest "/docker-entrypoint.…" 11 minutes ago Exited (0) About a minute ago ngx1
5abc6ea0c7f8 wurstmeister/kafka "start-kafka.sh" 4 weeks ago Exited (255) 3 weeks ago 0.0.0.0:9092->9092/tcp kafka
a728e2d2d00e zookeeper "/docker-entrypoint.…" 4 weeks ago Exited (137) 4 seconds ago zookeeper
32dead838953 adafef2e596e "/bin/sh -c 'apt-get…" 4 weeks ago Exited (100) 4 weeks ago jolly_ellis
186f3d1a87c2 adafef2e596e "/bin/sh -c 'apt-get…" 4 weeks ago Exited (100) 4 weeks ago objective_mendeleev
392dce6fffe1 mysql:5.7.30 "docker-entrypoint.s…" 5 weeks ago Up 3 weeks 33060/tcp, 0.0.0.0:3307->3306/tcp mysql5

run

创建并启动一个容器run = create+start

-d: daemon 用于支撑容器

1
2
3
4
5
6
7
8
luodi@roddydeMacBook-Pro:~$ docker run --name ngx1 -d  nginx:latest
efafc195cc875d0b6895b47f2b6a1215073fae043bc754bb03839865fd530fac
luodi@roddydeMacBook-Pro:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
efafc195cc87 nginx:latest "/docker-entrypoint.…" 2 seconds ago Up 1 second 80/tcp ngx1
a728e2d2d00e zookeeper "/docker-entrypoint.…" 4 weeks ago Up About a minute 2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:32769->2181/tcp zookeeper
392dce6fffe1 mysql:5.7.30 "docker-entrypoint.s…" 5 weeks ago Up 3 weeks 33060/tcp, 0.0.0.0:3307->3306/tcp mysql5
luodi@roddydeMacBook-Pro:~$

create

创建一个容器但是不启动它

创建一个ngx2的容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
luodi@roddydeMacBook-Pro:~$ docker create --name ngx2  nginx:latest
81bcf8637b40f7764fd9f2701ab2f6cb97cee5e08cd29fa096d62207dd456bfe
luodi@roddydeMacBook-Pro:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
392dce6fffe1 mysql:5.7.30 "docker-entrypoint.s…" 5 weeks ago Up 3 weeks 33060/tcp, 0.0.0.0:3307->3306/tcp mysql5
luodi@roddydeMacBook-Pro:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81bcf8637b40 nginx:latest "/docker-entrypoint.…" 4 seconds ago Created ngx2
efafc195cc87 nginx:latest "/docker-entrypoint.…" 19 minutes ago Exited (0) 10 minutes ago ngx1
5abc6ea0c7f8 wurstmeister/kafka "start-kafka.sh" 4 weeks ago Exited (255) 3 weeks ago 0.0.0.0:9092->9092/tcp kafka
a728e2d2d00e zookeeper "/docker-entrypoint.…" 4 weeks ago Exited (143) 7 minutes ago zookeeper
32dead838953 adafef2e596e "/bin/sh -c 'apt-get…" 4 weeks ago Exited (100) 4 weeks ago jolly_ellis
186f3d1a87c2 adafef2e596e "/bin/sh -c 'apt-get…" 4 weeks ago Exited (100) 4 weeks ago objective_mendeleev
392dce6fffe1 mysql:5.7.30 "docker-entrypoint.s…" 5 weeks ago Up 3 weeks 33060/tcp, 0.0.0.0:3307->3306/tcp mysql5

version

查看docker版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

luodi@roddydeMacBook-Pro:~$ docker version
Client: Docker Engine - Community
Version: 19.03.8
API version: 1.40
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:21:11 2020
OS/Arch: darwin/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.8
API version: 1.40 (minimum version 1.12)
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:29:16 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683