Docker需要学些哪些东西
- docker概念
- docker安装
- docker命令
- docker镜像
- 容器数据卷
- dockerfile
- docker网络原理
- docker compose
- docker swarm
- CI/CD jenkins
Docker架构图
镜像(image)
docker镜像就好比是一个模板,可以通过这个模板创建容器服务,apache镜像==>run==>apache01容器。通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中)
容器(container)
Docker利用容器技术,独立运行一个或者一组应用,通过镜像来创建
仓库(repository)
存放镜像的地方,分为公有仓库或者私有仓库
安装docker(ubuntu16.04)
Docker帮助文档 :Docker帮助文档
1. 卸载旧版本
apt-get remove docker docker-engine docker.io containerd runc
2. 配置仓库信息
apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
3. 增加官方的GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
4. 配置仓库(阿里云)
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
5. 安装docker
sudo apt-get install docker-ce docker-ce-cli containerd.io
6. 查看docker版本
root@ubuntu-xenial:/home/wqx# docker version
Client: Docker Engine - Community
Version: 20.10.7
API version: 1.41
Go version: go1.13.15
Git commit: f0df350
Built: Wed Jun 2 11:56:47 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.7
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: b0f5bc3
Built: Wed Jun 2 11:54:58 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.6
GitCommit: d71fcd7d8303cbf684402823e425e9dd2e99285d
runc:
Version: 1.0.0-rc95
GitCommit: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
docker-init:
Version: 0.19.0
GitCommit: de40ad0
7. 因为官方镜像很慢,所以配置阿里云镜像加速
注册阿里云并登陆,找到弹性计算->镜像服务ACR->进入控制台->镜像工具->镜像加速器
8. 测试,启动一个官方的测试镜像hello-world:docker run hello-world
root@ubuntu-xenial:/home/wqx# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:0fe98d7debd9049c50b597ef1f85b7c1e8cc81f59c8d623fcb2250e8bec85b38
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
9. 查看镜像 docker images
root@ubuntu-xenial:/home/wqx# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 5 months ago 13.3kB
10. 卸载docker
卸载依赖
sudo apt-get purge docker-ce docker-ce-cli containerd.io
删除资源
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containe
11. docker默认工作路径 /var/lib/docker/
12. run 运行流程
底层原理
Docker是怎么工作的?
Docker是一个Client-Server架构的系统,docker的守护进程运行在主机上,通过socket从客户端访问。
DockerServer接收到Docker-Client的指令,就会执行这个命令。
docker为什么比VM快?
1、docker有着比虚拟机更少的抽象层
2、docker利用的是宿主机的内核,vm需要的是GuestOS
所以说,新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导。虚拟机是加载Guest OS,分钟级别的,而docker是利用宿主机的操系统,省略了这个复杂的过程,是秒级的。
Docker常用命令
帮助命令
- docker version :docker的版本信息
- docker info:显示docker的系统信息
- docker 命令 --help:帮助命令
镜像命令
docker images 查看镜像
root@ubuntu-xenial:/home/wqx# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 5 months ago 13.3kB
解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
可选项
-a,--all # 列出所有镜像
-q,--quiet # 只显示镜像的id
docker search 查找镜像
docker search 镜像名
root@ubuntu-xenial:/home/wqx# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11312 [OK]
mariadb MariaDB Server is a high performing open sou… 4299 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 839 [OK]
解释
可选项,可以通过收藏来过滤
--filter=STARS=3000 #搜索收藏数大于3000的
docker pull 下载镜像
docker pull 镜像名
root@ubuntu-xenial:/home/wqx# docker pull mysql
Using default tag: latest # 如果不写tag,默认就是latest
latest: Pulling from library/mysql
e1acddbe380c: Pull complete # 分层下载,docker images的核心,联合文件系统
bed879327370: Pull complete
03285f80bafd: Pull complete
ccc17412a00a: Pull complete
1f556ecc09d1: Pull complete
adc5528e468d: Pull complete
1afc286d5d53: Pull complete
6c724a59adff: Pull complete
0f2345f8b0a3: Pull complete
c8461a25b23b: Pull complete
3adb49279bed: Pull complete
77f22cd6c363: Pull complete
Digest: sha256:d45561a65aba6edac77be36e0a53f0c1fba67b951cb728348522b671ad63f926 # 签名
Status: Downloaded newer image for mysql:latest # 真实地址
docker.io/library/mysql:latest
docker pull mysql
等价于
docker pull mysqldocker.io/library/mysql:latest
指定版本下载
docker pull mysql:5.7
root@ubuntu-xenial:/home/wqx# docker pull mysql:5.7
5.7: Pulling from library/mysql
e1acddbe380c: Already exists
bed879327370: Already exists
03285f80bafd: Already exists
ccc17412a00a: Already exists
1f556ecc09d1: Already exists
adc5528e468d: Already exists
1afc286d5d53: Already exists
4d2d9261e3ad: Pull complete
ac609d7b31f8: Pull complete
53ee1339bc3a: Pull complete
b0c0a831a707: Pull complete
Digest: sha256:7cf2e7d7ff876f93c8601406a5aa17484e6623875e64e7acc71432ad8e0a3d7e
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
docker rmi -f 容器id
docker rmi -f 容器id # 删除指定的容器
docker rmi -f 容器id 容器id 容器id # 删除多个容器
docker rmi -f $(docker images -aq) # 删除全部的容器
root@ubuntu-xenial:/home/wqx# docker rmi -f 5a4e492065c7
Untagged: mysql:latest
Untagged: mysql@sha256:d45561a65aba6edac77be36e0a53f0c1fba67b951cb728348522b671ad63f926
Deleted: sha256:5a4e492065c722ec8cc7413552bafc6fd5434c5ad90797e898ccc4e347e21aa5
Deleted: sha256:816bf92c28337cd97393cb1fab7697398c9b657baa8e80ad394e1193bdd7c323
Deleted: sha256:4d9cad11f64589b397b1033d6f14cea1794efbee0a9923932fb3394a67cb9812
Deleted: sha256:d8cc28a31b469770ffe2239a29aaef8745fedc4df3c545b1615ba864796709a3
Deleted: sha256:c65a377e07f1205f3c28f7067d2ac4da3a65fbe7c1b4606b48028db3818d2f4c
Deleted: sha256:7dcd840f1d4be1a565f470453489932a17c62ed36b959d7839f004606ba22f0b
容器命令
有了镜像才能创建容器,下载一个centos进行来测试学习
docker pull centos
新建容器并启动 docker run
docker run [可选参数] image
启动并进入容器
docker run [可选参数] image
docker run -it centos /bin/bash
#参数说明
--name="Name" # 容器名字,比如apache01,apache02,用来区分容器
-d # 后台方式运行
-it # 使用交互方式运行,进入容器查看内容
-p # 指定容器的端口, -p 8080:8080
-p ip:主机端口:容器端口
-p ip:主机端口:容器端口(常用)
-p 容器端口
-P # 随机指定端口
#启动并进入容器
root@ubuntu-xenial:/home/wqx# docker run -it centos /bin/bash
[root@5e58028ac0fa /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@5e58028ac0fa /]#
查看运行中的容器 docker ps
docker ps -aq
#docker ps 命令 # 列出当前正在运行的容器
-a # 列出当前正在运行的容器+历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
root@ubuntu-xenial:/home/wqx# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@ubuntu-xenial:/home/wqx# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e58028ac0fa centos "/bin/bash" 5 minutes ago Exited (127) 2 minutes ago wonderful_wiles
46e72f77c6ee hello-world "/hello" 4 hours ago Exited (0) 4 hours ago sweet_banach
root@ubuntu-xenial:/home/wqx# docker ps -n=2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e58028ac0fa centos "/bin/bash" 5 minutes ago Exited (127) 3 minutes ago wonderful_wiles
46e72f77c6ee hello-world "/hello" 4 hours ago Exited (0) 4 hours ago sweet_banach
root@ubuntu-xenial:/home/wqx# docker ps -aq
5e58028ac0fa
46e72f77c6ee
root@ubuntu-xenial:/home/wqx#
退出容器 docker
exit # 直接退出容器并停止
Ctrl + P + Q # 容器不停止退出
删除容器 docker rm
docker rm 容器id
docker rm 容器id # 删除指定的容器,不能删除正在运行的容器
docker rm -f $(docker images -aq) # 删除所有的容器
docker rm -a -q | xargs docker rm # 删除所有的容器
启动和停止容器 docker start
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止容器
docker kill 容器id # 强制停止
常用其他命令(重点)
docker run -d 镜像名
#docker run -d 镜像名
root@ubuntu-xenial:/home/wqx# docker run -d centos
9e4948e3d47b4b810f402fe3f47c5abd7b1a679c2c2bd0bd304c1b59d0448b7f
root@ubuntu-xenial:/home/wqx# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
执行docker ps命令后,发现centos停止了
原因:docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止。
查看日志 docker logs
docker logs -tf 容器id
docker logs
-tf # 显示日志
--tail [number] # 显示日志条数
root@ubuntu-xenial:/home/wqx# docker logs -tf --tail 10 1d5250a18dd7
查看容器中的进程信息 docker top
docker top 容器id
#docker top 容器id
root@ubuntu-xenial:/home/wqx# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d5250a18dd7 centos "/bin/bash" 6 minutes ago Up 6 minutes recursing_noether
root@ubuntu-xenial:/home/wqx# docker top 1d5250a18dd7
UID PID PPID C STIME TTY TIME CMD
root 11386 11363 0 07:27 pts/0 00:00:00 /bin/bash
root@ubuntu-xenial:/home/wqx#
查看容器元信息 docker inspect
docker inspect 容器id
#docker inspect 容器id
root@ubuntu-xenial:/home/wqx# docker inspect 1d5250a18dd7
进入当前正在运行的容器 docker exec -it
docker exec -it 容器id
#docker exec -it 容器id # 进入容器后开启一个新的终端,可以在里面操作(常用)
root@ubuntu-xenial:/home/wqx# docker exec -it 1d5250a18dd7 /bin/bash
[root@1d5250a18dd7 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@1d5250a18dd7 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 07:27 pts/0 00:00:00 /bin/bash
root 16 0 0 07:39 pts/1 00:00:00 /bin/bash
root 32 16 0 07:40 pts/1 00:00:00 ps -ef
进入当前正在运行的容器 docker attach 容器id
docker attach 容器id
#docker attach 容器id # 进入容器正在执行的终端,不会启动新的进程
root@ubuntu-xenial:/home/wqx# docker attach 1d5250a18dd7
[root@1d5250a18dd7 /]#
从容器内拷贝信息到主机 docker cp
docker cp 容器id:容器路径 主机目录
#启动一个容器
root@ubuntu-xenial:/home/wqx# docker run -it centos
[root@f28d05b5cc8e /]# root@ubuntu-xenial:/home/wqx# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f28d05b5cc8e centos "/bin/bash" 15 seconds ago Up 13 seconds tender_davinci
#进入容器
root@ubuntu-xenial:/home/wqx# docker attach f28d05b5cc8e
[root@f28d05b5cc8e /]# cd home/
#在容器/home/目录下新建一个php文件
[root@f28d05b5cc8e home]# touch wqx.php
[root@f28d05b5cc8e home]# ls
wqx.php
#退出容器,回到主机
[root@f28d05b5cc8e home]# exit
exit
#查看主机内没有wqx.php文件
root@ubuntu-xenial:/home/wqx# ll
total 12
drwxr-xr-x 3 root root 4096 Aug 16 07:05 ./
drwxr-xr-x 6 root root 4096 Aug 16 09:00 ../
#拷贝文件到主机的/home/wqx目录下
root@ubuntu-xenial:/home/wqx# docker cp f28d05b5cc8e:/home/wqx.php /home/wqx/
#查看主机的/home/wqx目录
root@ubuntu-xenial:/home/wqx# ll
total 12
drwxr-xr-x 3 root root 4096 Aug 24 07:50 ./
drwxr-xr-x 6 root root 4096 Aug 16 09:00 ../
-rw-r--r-- 1 root root 0 Aug 24 07:49 wqx.php