Docker常用命令

docker官网文档地址:https://docs.docker.com/get-docker/

安装docker流程

1、卸载旧的版本

2、需要的安装报

3、设置镜像的仓库

4、安装docker需要的依赖

5、启动

阿里云容器镜像加速地址:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

Docker基本概念

镜像

容器

仓库

容器与镜像的关系类似于面向对象编程中的对象与类。

Docker常用命令

img

Docker命令和Dockerfile指令

帮助命令

docker version

显示docker的常用信息

1
2
3
4
5
6
7
8
9
10
11
yangfan@yangfan-virtual-machine:~$ docker version
Client: Docker Engine - Community
Version: 20.10.5
API version: 1.41
Go version: go1.13.15
Git commit: 55c4c88
Built: Tue Mar 2 20:18:05 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version: dial unix /var/run/docker.sock: connect: permission denied

docker info

显示docker的信息信息,包括镜像和容器的数量

1
2
3
4
5
6
7
8
9
10
11
yangfan@yangfan-virtual-machine:~$ docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)

Server:
ERROR: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/info: dial unix /var/run/docker.sock: connect: permission denied
errors pretty printing info

docker –help

显示帮助信息

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
yangfan@yangfan-virtual-machine:~$ docker --help

Usage: docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
--config string Location of client config files (default "/home/yangfan/.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(s) 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/yangfan/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/yangfan/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/yangfan/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit

Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Build with BuildKit (Docker Inc., v0.5.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes

Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
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
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
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
login Log in to a Docker registry
logout Log out from a Docker registry
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
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
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
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/

镜像命令

docker images

查看所有本地的主机上的镜像

1
2
3
4
5
6
yangfan@yangfan-virtual-machine:~$ sudo docker images
[sudo] yangfan 的密码:
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 a70d36bc331a 6 weeks ago 449MB
centos latest 300e315adb2f 2 months ago 209MB
hello-world latest bf756fb1ae65 14 months ago 13.3kB

docker search 镜像名

查找镜像

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
yangfan@yangfan-virtual-machine:~$ sudo docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10570 [OK]
mariadb MariaDB Server is a high performing open sou… 3949 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 774 [OK]
percona Percona Server is a fork of the MySQL relati… 527 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 87
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 79
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
bitnami/mysql Bitnami MySQL Docker Image 48 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 39
prom/mysqld-exporter 37 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 27
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
circleci/mysql MySQL is a widely used, open-source relation… 20
mysql/mysql-router MySQL Router provides transparent routing be… 18
arey/mysql-client Run a MySQL client from a docker container 17 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 12 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
jelastic/mysql An image of the MySQL database server mainta… 1
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]

docker pull 镜像名

下载镜像

默认使用最新版

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
yangfan@yangfan-virtual-machine:~$ sudo docker pull citizenstig/dvwa
Using default tag: latest
latest: Pulling from citizenstig/dvwa
8387d9ff0016: Pull complete
3b52deaaf0ed: Pull complete
4bd501fad6de: Pull complete
a3ed95caeb02: Pull complete
790f0e8363b9: Pull complete
11f87572ad81: Pull complete
341e06373981: Pull complete
709079cecfb8: Pull complete
55bf9bbb788a: Pull complete
b41f3cfd3d47: Pull complete
70789ae370c5: Pull complete
43f2fd9a6779: Pull complete
6a0b3a1558bd: Pull complete
934438c9af31: Pull complete
1cfba20318ab: Pull complete
de7f3e54c21c: Pull complete
596da16c3b16: Pull complete
e94007c4319f: Pull complete
3c013e645156: Pull complete
7b3eb1ac6cfe: Pull complete
Digest: sha256:1c0ab894f0bf41351519c8388a282c0a178216e9ce8f0399a162472070379dc6
Status: Downloaded newer image for citizenstig/dvwa:latest
docker.io/citizenstig/dvwa:latest

指定版本

docker pull 镜像名[:tag]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
yangfan@yangfan-virtual-machine:~$ sudo docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Pull complete
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi

删除镜像包

docker rmi id

通过id删除镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
yangfan@yangfan-virtual-machine:~$ sudo docker rmi a70d36bc331a
Untagged: mysql:5.7
Untagged: mysql@sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Deleted: sha256:a70d36bc331a13d297f882d3d63137d24b804f29fa67158c40ad91d5050c39c5
Deleted: sha256:50c77bf7bcddd1f1d97789d80ac2404eec22c860c104e858620d2a2e321f0ef7
Deleted: sha256:14244329b83dfc8982398ee4104a548385652d2bffb957798ff86a419013efd6
Deleted: sha256:6d990477f90af28473eb601a9bca22253f6381e053c5a8edda0a4f027e124a3c
Deleted: sha256:ee0449796df204071589162fc16f8d65586312a40c68d1ba156c93c56f5e5ce8
Deleted: sha256:d08533f1e2acc40ad561a46fc6a76b54c739e6b24f077c183c5709e0a6885312
Deleted: sha256:4f9d91a4728e833d1062fb65a792f06e22e425f63824f260c8b5a64b776ddc38
Deleted: sha256:20bf4c759d1b0d0e6286d2145453af4e0e1b7ba3d4efa3b8bce46817ad4109de
Deleted: sha256:a9371bbdf16ac95cc72555c6ad42f79b9f03a82d964fe89d52bdc5f335a5f42a
Deleted: sha256:5b02130e449d94f51e8ff6e5f7d24802246198749ed9eb064631e63833cd8f1d
Deleted: sha256:ab74465b38bc1acb16c23091df32c5b7033ed55783386cb57acae8efff9f4b37
Deleted: sha256:cb42413394c4059335228c137fe884ff3ab8946a014014309676c25e3ac86864

docker rmi -f $(docker images -qa)

删除全部镜像

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
yangfan@yangfan-virtual-machine:~$ sudo docker rmi -f $(sudo docker images -a)
Untagged: mysql:5.7
Untagged: mysql@sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Deleted: sha256:a70d36bc331a13d297f882d3d63137d24b804f29fa67158c40ad91d5050c39c5
Deleted: sha256:50c77bf7bcddd1f1d97789d80ac2404eec22c860c104e858620d2a2e321f0ef7
Deleted: sha256:14244329b83dfc8982398ee4104a548385652d2bffb957798ff86a419013efd6
Deleted: sha256:6d990477f90af28473eb601a9bca22253f6381e053c5a8edda0a4f027e124a3c
Deleted: sha256:ee0449796df204071589162fc16f8d65586312a40c68d1ba156c93c56f5e5ce8
Deleted: sha256:d08533f1e2acc40ad561a46fc6a76b54c739e6b24f077c183c5709e0a6885312
Deleted: sha256:4f9d91a4728e833d1062fb65a792f06e22e425f63824f260c8b5a64b776ddc38
Deleted: sha256:20bf4c759d1b0d0e6286d2145453af4e0e1b7ba3d4efa3b8bce46817ad4109de
Deleted: sha256:a9371bbdf16ac95cc72555c6ad42f79b9f03a82d964fe89d52bdc5f335a5f42a
Deleted: sha256:5b02130e449d94f51e8ff6e5f7d24802246198749ed9eb064631e63833cd8f1d
Deleted: sha256:ab74465b38bc1acb16c23091df32c5b7033ed55783386cb57acae8efff9f4b37
Untagged: tomcat:latest
Untagged: tomcat:9.0
Untagged: tomcat@sha256:94cc18203335e400dbafcd0633f33c53663b1c1012a13bcad58cced9cd9d1305
Deleted: sha256:040bdb29ab375db2a8de090070dcbee19d43f609385a934c3f2b423e515f57bb
Deleted: sha256:03ac467e286d36301dba7daaec530f481074197d22d41bd48a32e5a2e3069573
Deleted: sha256:c56c3d4e36aac2060fdb29c95baf4b4d6916fe9a454751b77b8d89f27dd8dda4
Deleted: sha256:0f3911a87064866cd2aa7fd863afc1e02bdfa4856afa351e871f0e6a48cfd52b
Deleted: sha256:7891c5716382337382836e0af1ea998bf77c1eed9c8e806c7aac5f489952f4ba
Deleted: sha256:56dee2fc4110951264d4b134b303920c633aabbbb0264dc218518b3547320de2
Deleted: sha256:cd253bbc5a1ce457359958f4905b53a3f6f0ac167ba5065f68385600fdbae99e
Deleted: sha256:03b0292dbc6978fe1077f948ac07472a8cac23fb4c3cf92c76f5eb62891e3d5a
Deleted: sha256:19f2a825ed46bbf16a06d9d5185a0ac68196217f302e760abb4cbc3178b14cc1
Deleted: sha256:b4f0436e967b6b9b021d3ed900a5422da6d8fe8c17c51c7d7e4f9c105812f868
Deleted: sha256:4762552ad7d851a9901571428078281985074e5ddb806979dd7ad24748db4ca0
Untagged: nginx:latest
Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Deleted: sha256:f6d0b4767a6c466c178bf718f99bea0d3742b26679081e52dbf8e0c7c4c42d74
Deleted: sha256:4dfe71c4470c5920135f00af483556b09911b72547113512d36dc29bfc5f7445
Deleted: sha256:3c90a0917c79b758d74b7040f62d17a7680cd14077f734330b1994a2985283b8
Deleted: sha256:a1c538085c6f891424160d8db120ea093d4dda393e94cd4713e3fff3c82299b5
Deleted: sha256:a3ee2510dcf02c980d7aff635909612006fd1662084d6225e52e769b984abeb5
Deleted: sha256:cb42413394c4059335228c137fe884ff3ab8946a014014309676c25e3ac86864
Untagged: nginx:1.18.0-alpine
Untagged: nginx@sha256:7ae8e5c3080f6012f8dc719e2308e60e015fcfa281c3b12bf95614bd8b6911d6
Deleted: sha256:f2343e2e25078726ff14f81e5f530e390ac147eef63a4828db6cc2c4155ff88f
Deleted: sha256:c584818973ae5022b23a6b851d2c9119b32458154a9cab36181b80edcea176cf
Deleted: sha256:b6efb22fdcfd525d19e21844e59a80ca9e4b194b581961bc080977d5f69ab59a
Deleted: sha256:f1d9a117248f08168772190c89436e26770693b6d5a2c61425ca905befabe858
Deleted: sha256:2c1427c3e3c7078294a8cf7a4c7cdc556c83b3492ffb11149ecf0d1f7d96278b
Deleted: sha256:0fcbbeeeb0d7fc5c06362d7a6717b999e605574c7210eff4f7418f6e9be9fbfe
Untagged: centos:latest
Untagged: centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55
Deleted: sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error: No such image: mysql
Error: No such image: 5.7
Error: No such image: 6
Error: No such image: weeks
Error: No such image: ago
Error response from daemon: invalid reference format: repository name must be lowercase
Error: No such image: 9.0
Error: No such image: 7
Error: No such image: weeks
Error: No such image: ago
Error response from daemon: invalid reference format: repository name must be lowercase
Error: No such image: tomcat
Error: No such image: latest
Error: No such image: 040bdb29ab37
Error: No such image: 7
Error: No such image: weeks
Error: No such image: ago
Error response from daemon: invalid reference format: repository name must be lowercase
Error: No such image: latest
Error: No such image: f6d0b4767a6c
Error: No such image: 7
Error: No such image: weeks
Error: No such image: ago
Error response from daemon: invalid reference format: repository name must be lowercase
Error: No such image: nginx
Error: No such image: 1.18.0-alpine
Error: No such image: 2
Error: No such image: months
Error: No such image: ago
Error response from daemon: invalid reference format: repository name must be lowercase
Error: No such image: latest
Error: No such image: 300e315adb2f
Error: No such image: 2
Error: No such image: months
Error: No such image: ago
Error response from daemon: invalid reference format: repository name must be lowercase

容器命令

docker run [可选参数] [image/镜像名]

新建容器并启动

# 参数

–name=”Name” 容器名字

-d 后台方式运行

-it 使用交互方式运行,进入容器查看内容

-p 指定容器端口 -p 8080:8080

-p ip:主机端口:容器端口

-p 主机端口:容器端口(常用)

-p 容器端口

容器端口

-P 随机指定端口

1
2
3
4
5
yangfan@yangfan-virtual-machine:~$ sudo docker run --name nginx2 -d -p 3355:80 nginx
d5b419eeaa4675888040fe56963c692ac08ab18449abcc0554456a5eff5cb4ee
yangfan@yangfan-virtual-machine:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d5b419eeaa46 nginx "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 0.0.0.0:3355->80/tcp nginx2

# 测试 启动并进入容器

docker run -it centos /bin/bash

退出 exit

docker ps

列出当前正在运行的容器

1
2
3
yangfan@yangfan-virtual-machine:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c869b1bbcbc centos "/bin/bash" 2 hours ago Up 2 hours fervent_chatelet

参数

-a 列出所有的运行的容器+出历史运行过的容器

退出容器

exit 直接容器停止并退出

ctrl + P + Q 容器不停止退出

删除容器

docker rm 容器id 删除指定的容器,不能删除正在运行的容器

docker rm -f $(docker ps -qa) 删除所有容器

启动和停止容器的操作

docker start 容器id 启动容器

docker restart 容器id 重启容器

docker stop 容器id 停止当前正在运行的容器

docker kill 容器id 强制停止当前容器

常用其他命令

后台启动容器的坑

docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止

nginx,容器启动后,发现自己没有提供服务,就会立刻停止

查看日志

docker logs -f -t –tail 行数 容器

查看容器中进程信息

docker top 容器id

查看镜像的元数据

docker inspect 容器id

进入当前正在运行的容器

docker exec -it 容器id bashshell

docker attach 容器id

区别

docker exec 进入容器后开启一个新的终端,可以在里面操作

docker attach 进入容器正在执行的终端,不会启动新的进程

从容器内拷贝文件到主机上

docker cp 容器id:容器内路径 目的的主机路径

Docker实战操作

docker 安装 nginx

搜索

docker search 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
yangfan@yangfan-virtual-machine:~$ sudo docker search nginx
[sudo] yangfan 的密码:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14519 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1973 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 809 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 157
linuxserver/nginx An Nginx container, brought to you by LinuxS… 141
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 115 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 98 [OK]
bitnami/nginx Bitnami nginx Docker Image 94 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 89 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 66 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 49
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 47 [OK]
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 31
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 19 [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 16
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
bitwarden/nginx The Bitwarden nginx web server acting as a r… 9
flashspys/nginx-static Super Lightweight Nginx Image 9 [OK]
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 8 [OK]
mailu/nginx Mailu nginx frontend 8 [OK]
navidonskis/nginx-php5.6 Docker nginx + php5.6 on Ubuntu 7 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 2 [OK]
wodby/nginx Generic nginx 1 [OK]
下载nginx
1
2
3
4
5
6
7
8
9
10
yangfan@yangfan-virtual-machine:~$ sudo docker pull nginx:1.18.0-alpine
1.18.0-alpine: Pulling from library/nginx
0a6724ff3fcd: Pull complete
1d7c87af3754: Pull complete
9668ffa91d19: Pull complete
e81a2f5037c1: Pull complete
991b5ddb4d9e: Pull complete
Digest: sha256:7ae8e5c3080f6012f8dc719e2308e60e015fcfa281c3b12bf95614bd8b6911d6
Status: Downloaded newer image for nginx:1.18.0-alpine
docker.io/library/nginx:1.18.0-alpine
运行容器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
yangfan@yangfan-virtual-machine:~$ sudo docker run --name nginx1 -d -p 3344:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a076a628af6f: Already exists
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
2cd5726333e7eadab428a788ad440c16d00af2aecc1010e4ead0b7878aa899ca
yangfan@yangfan-virtual-machine:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2cd5726333e7 nginx "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 0.0.0.0:3344->80/tcp
进入容器查看配置文件
1
2
3
4
5
6
7
8
9
10
11
yangfan@yangfan-virtual-machine:~$ sudo docker exec -it 2cd5726333e7 /bin/bash
root@2cd5726333e7:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@2cd5726333e7:/# whoami
root
root@2cd5726333e7:/# wh
whereis which while who whoami
root@2cd5726333e7:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@2cd5726333e7:/# cd /etc/nginx/

安装tomcat

官方的使用方式

docker run -it –rm tomcat:90

–rm 一般用来测试,用完就删除

下载镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
yangfan@yangfan-virtual-machine:~$ sudo docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
b9a857cbf04d: Pull complete
d557ee20540b: Pull complete
3b9ca4f00c2e: Pull complete
667fd949ed93: Pull complete
661d3b55f657: Pull complete
511ef4338a0b: Pull complete
a56db448fefe: Pull complete
00612a99c7dc: Pull complete
326f9601c512: Pull complete
c547db74f1e1: Pull complete
Digest: sha256:94cc18203335e400dbafcd0633f33c53663b1c1012a13bcad58cced9cd9d1305
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest

运行镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
yangfan@yangfan-virtual-machine:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 a70d36bc331a 6 weeks ago 449MB
tomcat 9.0 040bdb29ab37 7 weeks ago 649MB
tomcat latest 040bdb29ab37 7 weeks ago 649MB
nginx latest f6d0b4767a6c 7 weeks ago 133MB
nginx 1.18.0-alpine f2343e2e2507 2 months ago 21.9MB
centos latest 300e315adb2f 2 months ago 209MB
yangfan@yangfan-virtual-machine:~$ sudo docker run -d -p 3344:8080 --name tomcat1 tomcat
b90a6472737fe5d319b4afad7d360a3e6394dd08fb0991bc27751c99e30b1712
yangfan@yangfan-virtual-machine:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b90a6472737f tomcat "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:3344->8080/tcp tomcat1

docker stats 容器id

查看cpu状态

1
2
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O        PIDS
eaee03688881 mysql02 0.08% 204.6MiB / 2.895GiB 6.90% 12.1kB / 20.3kB 26.7MB / 292MB 32

Commit镜像

docker commit 提交容器成为一个新的副本

docker commit -m=”提交的描述信息” -a=”作者” 容器id 目标镜像名[:TAG]

1
2
3
4
5
6
7
8
fany@ubuntu18:~$ sudo docker commit -m="commit版centos" -a="fany" 1e7b39c3c2da fany/centos:2.0 
sha256:7396d029926a4f5ba6b9f82e5172666ae63b60cd0b53fd9f607798052bd3a05b
fany@ubuntu18:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
fany/centos 2.0 7396d029926a 5 seconds ago 209MB
fany/centos 1.0 13da2d456cd9 38 hours ago 209MB
mysql 5.7 a70d36bc331a 7 weeks ago 449MB
centos latest 300e315adb2f 3 months ago 209MB

容器数据卷

使用数据卷

docker run -it -v 主机目录:容器内目录

把主机目录挂载到容器目录中

使用数据卷实战:安装mysql

下载mysql
1
docker pull mysql
运行mysql
1
2
# 官方mysql运行测试命令
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
1
2
3
4
5
6
7
8
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7

#
-d 后台运行
-p 端口映射
-v 挂载(就是docker主机和主机之间的文件相互同步)
-e 添加配置
--name 别名

具名和匿名挂载

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 匿名挂载
-v 容器内路径
docker run -d -P --name nginx01 -v /etc/nginx nginx

fany@ubuntu18:~$ sudo docker run -d -P --name nginx01 -v /etc/nginx/ nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a076a628af6f: Already exists
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
2156646a24e3b8515ad3cb295e3188a7f06d5da7d7d6d286e47e33a53fdff665

# 查看所有的volume的情况
docker volume ls
fany@ubuntu18:~$ sudo docker volume ls
DRIVER VOLUME NAME
local 1aace1413a58fe48b59552be0e13f258fc2625389c358dc63f0fa61023af7264
local 2e6cf97c31fa74eb56fbecf7467b0eeebc85286c2d0b73324f7431f3e339a4ac
local 5ac0f89b61aad729335646a84d938d45072bebaadb5446e638311d8b78f1ae09
local 6eb06715d487effeab344c9a335940e522a3c95c89dce0363b93aa30b45d320f
local 9ce9dca7966653ba788bd8c7a79444d3a7fd533f89e3d97da0bb2eabf5d20328
local 9d1e8bfccadc28cd3048078e743e139183eb1fee78759e3bbb4170394e5d473f
local 9d8414a8ce53ef619a12bde9abfadbc3937565d566dd63e1d72d61ba3771a48b
local 44a91fa46dd5003a31c9123b8bbe1d43deb4933a6503c84ce17b11eaf0d5b769
local 90d2f411f4eb4d6bd903f4ceac1d6ab57ba557b08db220414aa28bda734da62f
local 2906a33e43e822a758130a9a8ae37c2dc31cb9aa1dde45969a0a43819cc48c20
local 3871f7c9892d84ce47c536a3ad810b3dcea7f3aecb9ce0c8ae66490c3f84ebaf
local 6058f80c65d9dd81f29975267e5f1eadee371c3ae2673828452757502550a19f
local 216203260f3961bd642a69622c97b9abf6b87172c28ee20048b4fae26c3e806a
local a52876a9d6f8a6556d3bf3835c0d92a0bc3853ace46843bd685241033ab14a56
local b60310b05c6bfa2a9ed0619264f0c8db7d5beab25c8ad132d981507c5f8b2180
local d6fd04629f90fa33f8ae877cfcf9a2ae55e39c0f6f71c9b58c0692c435aeb004
local e1ea3b10c4e43b47452a9183537115a67ebac455a17530ed3df18fda2c1f5cb9
local f9fff3462e30f18f4f85d4c0d9d50ee02f5b0909ed22b79d7e6b9e638b386408

# 这里发现,这种就是匿名挂载,我们在 -v 只写了容器内的路径,没有写容器外的路径

sudo
# 具名挂载
docker run -d -P --name nginx02 -V juming-nginx:/etc/nginx nginx


docker volume ls

fany@ubuntu18:~$ sudo docker volume ls
DRIVER VOLUME NAME
local 1aace1413a58fe48b59552be0e13f258fc2625389c358dc63f0fa61023af7264
local 2e6cf97c31fa74eb56fbecf7467b0eeebc85286c2d0b73324f7431f3e339a4ac
local 5ac0f89b61aad729335646a84d938d45072bebaadb5446e638311d8b78f1ae09
local 6eb06715d487effeab344c9a335940e522a3c95c89dce0363b93aa30b45d320f
local 9ce9dca7966653ba788bd8c7a79444d3a7fd533f89e3d97da0bb2eabf5d20328
local 9d1e8bfccadc28cd3048078e743e139183eb1fee78759e3bbb4170394e5d473f
local 9d8414a8ce53ef619a12bde9abfadbc3937565d566dd63e1d72d61ba3771a48b
local 44a91fa46dd5003a31c9123b8bbe1d43deb4933a6503c84ce17b11eaf0d5b769
local 90d2f411f4eb4d6bd903f4ceac1d6ab57ba557b08db220414aa28bda734da62f
local 2906a33e43e822a758130a9a8ae37c2dc31cb9aa1dde45969a0a43819cc48c20
local 3871f7c9892d84ce47c536a3ad810b3dcea7f3aecb9ce0c8ae66490c3f84ebaf
local 6058f80c65d9dd81f29975267e5f1eadee371c3ae2673828452757502550a19f
local 216203260f3961bd642a69622c97b9abf6b87172c28ee20048b4fae26c3e806a
local a52876a9d6f8a6556d3bf3835c0d92a0bc3853ace46843bd685241033ab14a56
local b60310b05c6bfa2a9ed0619264f0c8db7d5beab25c8ad132d981507c5f8b2180
local d6fd04629f90fa33f8ae877cfcf9a2ae55e39c0f6f71c9b58c0692c435aeb004
local e1ea3b10c4e43b47452a9183537115a67ebac455a17530ed3df18fda2c1f5cb9
local f9fff3462e30f18f4f85d4c0d9d50ee02f5b0909ed22b79d7e6b9e638b386408
local juming-nginx

所有的docker容器内的卷,没有指定目录的情况下都是在 /var/lib/docker/volumes/xxx/_data

我们通过具名挂载可以方便的找到我们的一个卷,大多数情况在使用具名挂载

1
2
3
4
# 区分匿名挂载/具名挂载还是指定路径挂载
-v 容器内路径 # 匿名挂载
-v 卷名:容器内路径 # 具名挂载
-v /宿主机路径:容器内路径 # 指定路径挂载

扩展:

-v /宿主机路径:容器内路径

1
2
3
4
5
6
7
8
# 通过 -V 容器内路径:ro rw 改变读写权限
ro readonly # 只读
rw readwrite # 可读可写

# 一旦这个设置了容器权限 容器对我们挂载出来的内容就有限定了
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx
# ro 只要看到ro就说明这个路径只能通过宿主机来操作,容器内部都无法操作

初始Dockerfile

dockerfile 就是用来构建 docker镜像的构建文件,命令脚本

通过这个脚本可以生成镜像,镜像是一层一层的,脚本一个个的命令,每个命令都是一层

1
2
3
4
5
6
7
# 创建一个dockerfile文件 名字可以随机 建议dockerfile
# 文件中的内容 指令(大写) 参数
FROM centos
VOLUME ['volume01', 'volume02']
CMD echo "-----end-----"
CMD /bin/bash
# 这里的每个命令,都是镜像的一层

执行

1
docker build -f /home/dockerfile1 -t 作者名/镜像名:版本 .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fany@ubuntu18:~/docker/DockerFile$ sudo docker build -f /home/fany/docker/DockerFile/dockerfile -t fany/centos:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 300e315adb2f
Step 2/4 : VOLUME ['volume01', 'volume02']
---> Running in ac6ee9ac88e2
Removing intermediate container ac6ee9ac88e2
---> 28fabf1af5a9
Step 3/4 : CMD echo "----end----"
---> Running in 107be8d1d71a
Removing intermediate container 107be8d1d71a
---> 246ba60efd5b
Step 4/4 : CMD /bin/bash
---> Running in d94e1416d4b9
Removing intermediate container d94e1416d4b9
---> 13da2d456cd9
Successfully built 13da2d456cd9
Successfully tagged fany/centos:1.0

数据卷容器

docker run -it –name docker01 –volumes-from docker01 作者名/镜像名:版本

通过以上的方式我们可以实现容器间的数据共享

任何一个容器被删除,共享的数据不会丢失

多个mysql实现数据共享

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
docker run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7

fany@ubuntu18:~$ sudo docker run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 mysql:5.7
eaee03688881f1c55d76642cf83518d862fe6ea5239d32ac7048628cfb44778a


docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01 mysql:5.7

# 测试结果
fany@ubuntu18:~$ sudo docker exec -it eaee03688881 /bin/bash
root@eaee03688881:/# cd /var/lib/mysql
root@eaee03688881:/var/lib/mysql# ls
auto.cnf client-cert.pem ib_logfile0 ibtmp1 private_key.pem server-key.pem
ca-key.pem client-key.pem ib_logfile1 mysql public_key.pem sys
ca.pem ib_buffer_pool ibdata1 performance_schema server-cert.pem test

fany@ubuntu18:~$ sudo docker start a3a15fbd16a3
a3a15fbd16a3
fany@ubuntu18:~$ sudo docker exec -it a3a15fbd16a3 /bin/bash
root@a3a15fbd16a3:/# cd /var/lib/mysql
root@a3a15fbd16a3:/var/lib/mysql# ls
auto.cnf client-cert.pem ib_logfile0 ibtmp1 private_key.pem server-key.pem
ca-key.pem client-key.pem ib_logfile1 mysql public_key.pem sys
ca.pem ib_buffer_pool ibdata1 performance_schema server-cert.pem test

# 在eaee03688881容器中新建了test数据库 a3a15fbd16a3 也同步了test数据库

结论:

容器之间配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用为止。

但是一旦持久化到了本地,这个时候,本地的数据是不会删除的。

DockerFile

dockerfile介绍

dockerfile构建过程

dockerfile的指令

1
2
3
4
5
6
7
8
9
10
11
12
FROM	# 基础镜像,一切从这里开始构建
MEINTAINER # 镜像是谁写的, 姓名+邮箱
RUN # 镜像构建的时候需要运行的命令
ADD # 步骤:tomcat镜像,这个tomcat压缩包 添加内容
COPY # 类似ADD,将ls
WORKDIR # 镜像的工作目录
VOLUME # 挂载的目录
EXPOST # 保留端口配置yum
CMD # 指定容器启动的时候需要运行的命令 替换
ENTRYPIOINT # 指定这个容器启动的时候要运行的命令 追加
ONBUILE # 当构建一个被继承 DockerFile 这个时候就会运行 ONBUILD的指令,触发指令。
ENV # 构建的时候设置环境变量

实战测试:centos

创建一个自己的centos

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
# 1 编写DockerFile配置文件
FROM centos
MAINTAINER fany<483813121@qq.com>

ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80

CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash



# 2、通过这个文件 构建镜像
# 命令 docker build -f dockerfile文件路径 -t 镜像名:[tag] .
构建成功后显示
Successfully built 镜像id
Successfully tagged 镜像名:tag

# 3、运行测试
fany@ubuntu18:~/docker/DockerFile$ sudo docker run --name centos03 -it 2eee339e854a
[root@28ffb5be051d local]# ls
bin etc games include lib lib64 libexec sbin share src

列出本地进行的变更历史镜像命令

docker history 镜像id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
fany@ubuntu18:~/docker/DockerFile$ sudo docker history 2eee339e854a
IMAGE CREATED CREATED BY SIZE COMMENT
2eee339e854a 7 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
328c5590083f 7 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
61c489975a32 7 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
4775afd52615 7 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
2d4e54be3fdd 7 minutes ago /bin/sh -c yum -y install net-tools 23.3MB
2210b82a2fd3 7 minutes ago /bin/sh -c yum -y install vim 58MB
8ad9663738ee 8 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
9e5baf54c101 8 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
7ca038e36aec 8 minutes ago /bin/sh -c #(nop) MAINTAINER fany<483813121… 0B
300e315adb2f 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB

CMD 和 ENTRYPOINT 区别

1
2
CMD		# 指定容器启动的时候需要运行的命令 替换
ENTRYPIOINT # 指定这个容器启动的时候要运行的命令 追加

实战测试:Tomcat镜像

1、准备镜像文件 tomcat压缩包,jdk的压缩包

2、编写dockerfile文件,官方命名Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
FROM centos
MAINTAINER fany<483813121@qq.com>

COPY readme.txt /usr/local/readme.txt

ADD jdk-8u11-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.22.tar.gz /usr/local/

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYPATH

ENV JAVA_HOME /usr/local/jdk1.80_11
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.22
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

EXPOSE 8080

CMD /usr/local/apache-tomcat-9.0.22/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.0.22/bin/logs/catalina.out

3、构建镜像

1
# docker build -f diytomcat

4、启动镜像

5、访问测试

6、发布项目

发布自己的镜像

https://homenew.console.aliyun.com/home/dashboard/ProductAndService

阿里云的容器镜像服务

DocKer网络