概述
一、将所需的环境架构上传至容器,打包镜像,往仓库传,需方直接下拉
现目前运维使用最多的服务
Dockerfile,Docker构件镜像三种方式中使用最广泛的一种
-
服务彼此间相互独立——服务间的解耦
解耦:建立连接,便于管理,防止数据过多积累在一个文件中
-
服务可以灵活迁移——Docker引擎——Docker-ce
Docker概述
是一种轻量级的“虚拟机”
在Linux容器里运行应用的开源工具
Docker与虚拟机的区别
虚拟机 | 容器 | |
---|---|---|
启动 | 1-3分钟 | 毫秒 |
容量 | 占用大 | 占用小 |
安全性 | 安全 | 不安全(共享内核资源) |
隔离级别 | 操作系统级 | 进程级 |
系统资源 | 5~15% | 0~5% |
镜像储存 | GB-TB | KB-MB |
集群规模 | 上百 | 上万 |
高可用策略 | 备份、容灾、迁移 | 弹性、负载、动态 |
Docker的使用场景
- 打包应用程序简单化部署
- 可脱离底层硬件任意迁移
Docker核心概念
-
镜像
一个面向Docker容器引擎的只读模板
-
容器
从镜像创建的运行实例
-
仓库
集中保存镜像的地方
安装Docker
安装最新版本Docker依赖版本环境
[root@server1 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
已加载插件:fastestmirror, langpacks
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): extras/7/x86_64/primary_db | 222 kB 00:00:00
(2/4): base/7/x86_64/group_gz | 153 kB 00:00:00
(3/4): base/7/x86_64/primary_db | 6.1 MB 00:00:00
(4/4): updates/7/x86_64/primary_db | 3.7 MB 00:00:02
#yum-utils提供了 yum-config-manager
#device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2
#Device Mapper 是 Linux2.6 内核中支持逻辑卷管理的通用设备映射机制
他为实验用于储存资源管理的块设备驱动提供了一共高度模块化的内核架构
设置阿里云镜像源
[root@server1 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加载插件:fastestmirror, langpacks
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
安装Docker-CE
[root@server1 ~]# yum -y install docker-ce
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* extras: mirrors.ustc.edu.cn
* updates: mirrors.ustc.edu.cn
正在解决依赖关系
查看docker版本
[root@server1 docker]# docker version
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:03:45 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.13
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:21 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.3.7
GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
查看公有仓库
[root@server1 docker]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14063 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1912 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 795 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 131
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 115
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 105 [OK]
bitnami/nginx Bitnami nginx Docker Image 90 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 80 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 72 [OK]
搜索镜像
[root@server1 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
获取镜像
[root@server1 docker]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
852e50cd189d: Pull complete
571d7e852307: Pull complete
addb10abd9cb: Pull complete
d20aa7ccdb77: Pull complete
8b03f1e11359: Pull complete
Digest: sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
查看镜像
[root@server1 docker]# docker images nginx #查看镜像信息
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc9a0695f571 6 hours ago 133MB
[root@server1 docker]# docker inspect nginx #查看镜像详细信息
[
{
"Id": "sha256:bc9a0695f5712dcaaa09a5adc415a3936ccba13fc2587dfd76b1b8aeea3f221c",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3"
],
"Parent": "",
"Comment": "",
"Created": "2020-11-25T00:30:19.011398516Z",
"Container": "279e6916c4aaaf5d61e468508abd96933f4e48194bd979dc692e0196cde2d59d",
添加新标签
[root@server1 docker]# docker tag nginx:latest nginx:web #修改标签为nginx:web
[root@server1 docker]# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc9a0695f571 6 hours ago 133MB
nginx web bc9a0695f571 6 hours ago 133MB
删除镜像
[root@server1 docker]# docker rmi nginx(或者镜像ID)
Untagged: nginx:latest
[root@server1 docker]# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx web bc9a0695f571 6 hours ago 133MB
添加新标签之后无法用镜像ID删除
[root@server1 docker]# docker tag nginx:latest nginx:web
[root@server1 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc9a0695f571 6 hours ago 133MB
nginx web bc9a0695f571 6 hours ago 133MB
[root@server1 docker]# docker rmi bc9a0695f571
Error response from daemon: conflict: unable to delete bc9a0695f571 (must be forced) - image is referenced in multiple repositories
使用名称删除,无法删除镜像,只能删除名称
如果使用多重重命名镜像,则需删除名称直至最后一个镜像
保存镜像
[root@server1 opt]# docker save -o nginx nginx:latest
[root@server1 opt]# ll
总用量 133884
drwx--x--x. 4 root root 28 11月 25 14:18 containerd
-rw-------. 1 root root 137096704 11月 25 14:52 nginx
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
载入镜像
一、将镜像保存到本地,删除docker中的镜像用于验证
[root@server1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@server1 opt]# docker load < nginx
f5600c6330da: Loading layer 72.52MB/72.52MB
7ccabd267c9f: Loading layer 64.54MB/64.54MB
850c2400ea4d: Loading layer 3.072kB/3.072kB
f790aed835ee: Loading layer 4.096kB/4.096kB
7e914612e366: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
[root@server1 opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc9a0695f571 6 hours ago 133MB
查看容器
[root@server1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27766c25cee5 nginx:latest "/docker-entrypoint.…" 7 seconds ago Created agitated_shockley
- -a 列出所有容器,包括未运行的容器
容器创建
[root@server1 ~]# docker create -it nginx:latest /bin/bash
27766c25cee55f51e1f42853f4a1d6bc0c752ea42798aa5335ea1bd20f9cff40
[root@server1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27766c25cee5 nginx:latest "/docker-entrypoint.…" 7 seconds ago Created agitated_shockley
- -i 让容器的标准输入保持打开
- -t 让docker分配一个伪终端
启动容器/停止容器
[root@server1 ~]# docker start 27766c25cee5
27766c25cee5
[root@server1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27766c25cee5 nginx:latest "/docker-entrypoint.…" About a minute ago Up 3 seconds 80/tcp agitated_shockley
[root@server1 ~]# docker stop 27766c25cee5
27766c25cee5
[root@server1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27766c25cee5 nginx:latest "/docker-entrypoint.…" 2 minutes ago Exited (137) 12 seconds ago agitated_shockley
正常关闭容器返回值为0
非正常关闭容器返回值为非0值
容器创建加启动命令
[root@server1 ~]# docker run -itd centos:7 /bin/bash
Unable to find image 'centos:7' locally
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
f89676a10c55276470ea5a643252293aeb6402af501a1a3d8ffa3cf4f4108c10
[root@server1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f89676a10c55 centos:7 "/bin/bash" 5 seconds ago Up 4 seconds stupefied_kilby
27766c25cee5 nginx:latest "/docker-entrypoint.…" 6 minutes ago Exited (137) 3 minutes ago agitated_shockley
如果没有该镜像,则会自动下载
进入容器
[root@server1 ~]# docker exec -it f89676a10c55 /bin/bash
[root@f89676a10c55 /]# yum -y install net-tools
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* extras: mirrors.163.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--> Finished Dependency Resolution
[root@f89676a10c55 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 #容器IP和本机docker0IP一致
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 4887 bytes 11200117 (10.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2916 bytes 160794 (157.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 co
让容器执行一次性命令
[root@server1 ~]# docker run nginx:latest /bin/bash -c ls
bin
boot
dev
docker-entrypoint.d
docker-entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@server1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03a3c18db923 nginx:latest "/docker-entrypoint.…" 37 seconds ago Exited (0) 36 seconds ago great_dubinsky
10a85cad04be nginx:latest "/docker-entrypoint.…" About a minute ago Exited (127) About a minute ago keen_boyd
733b6168c968 centos:7 "-c ls" 4 minutes ago Created upbeat_dirac
6b5a5ec5448e nginx:latest "/docker-entrypoint.…" 5 minutes ago Exited (127) 5 minutes ago vigorous_matsumoto
3b3aac174fcf nginx:latest "/docker-entrypoint.…" 6 minutes ago Exited (127) 6 minutes ago practical_goldstine
f89676a10c55 centos:7 "/bin/bash" 15 minutes ago Up 15 minutes stupefied_kilby
27766c25cee5 nginx:latest "/docker-entrypoint.…" 21 minutes ago Up 5 minutes 80/tcp agitated_shockley
运算完成之后容器关闭,自动释放资源
容器导出/导入
[root@server1 ~]# docker export f89676a10c55 > /opt/nginx_c
[root@server1 ~]# cd /opt
[root@server1 opt]# ll
总用量 402284
drwx--x--x. 4 root root 28 11月 25 14:18 containerd
-rw-------. 1 root root 137096704 11月 25 14:52 nginx
-rw-r--r--. 1 root root 274841600 11月 25 15:22 nginx_c
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[root@server1 ~]# cd /opt
[root@server1 opt]# cat nginx_c | docker import - nginx:web ###会生成镜像,而不会创建容器
sha256:a2a3ec303c94856805338f879c25ffbac5a994f68aadf1512c82f9dcc9ae94f0
[root@server1 opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx web a2a3ec303c94 6 seconds ago 267MB
<none> <none> ed426aa87a0a 18 seconds ago 0B
nginx latest bc9a0695f571 7 hours ago 133MB
centos 7 8652b9f0cb4c 11 days ago 204MB
删除容器/批量删除容器
[root@server1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03a3c18db923 nginx:latest "/docker-entrypoint.…" 4 minutes ago Exited (0) 4 minutes ago great_dubinsky
[root@server1 ~]# docker rm 03a3c18db923
03a3c18db923
[root@server1 ~]# docker ps -a | awk '{print "docker rm "$1}'| bash
Error: No such container: CONTAINER
10a85cad04be
733b6168c968
6b5a5ec5448e
3b3aac174fcf
Error response from daemon: You cannot remove a running container f89676a10c55276470ea5a643252293aeb6402af501a1a3d8ffa3cf4f4108c10. Stop the container before attempting removal or force remove
Error response from daemon: You cannot remove a running container 27766c25cee55f51e1f42853f4a1d6bc0c752ea42798aa5335ea1bd20f9cff40. Stop the container before attempting removal or force remove
[root@server1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f89676a10c55 centos:7 "/bin/bash" 36 minutes ago Up 36 minutes stupefied_kilby
27766c25cee5 nginx:latest "/docker-entrypoint.…" 42 minutes ago Up 26 minutes 80/tcp agitated_shockley