目录
Docker原理
使用场景:
Docker核心概念与安装方式
Docker与虚拟机区别
yum仓库安装docker
安装依赖包
设置阿里云镜像源
安装Docker-ce
启动docker
镜像加速
网络优化
查看docker版本
搜索在线镜像
下载镜像
查看已有镜像
删除镜像
添加镜像标签
存出镜像
载入镜像
容器操作
容器创建
查看容器
启动容器
另一种方法(创建和启动同步)
查看容器
进入容器
安装网络工具
删除容器
批量删除容器
容器导出
容器导入
Docker原理
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。从本质上,容器其实就是一种沙盒技术。就好像把应用隔离在一个盒子内,使其运行。因为有了盒子边界的存在,应用于应用之间不会相互干扰。并且像集装箱一样,拿来就走,随处运行。
原理图:
使用场景:
- 打包应用程序简化部署
- 可脱离底层硬件任意迁移
- 例:服务器从腾讯云迁移到阿里云
Docker核心概念与安装方式
Docker核心概念:
镜像——一个面向Docker容器弓|擎的只读模板
容器——从镜像创建的运行实例
仓库——集中保存镜像的地方
CentOS安装Docker的两种方式:
使用CURL获得Docker的安装脚本进行安装(不推荐)
使用YUM仓库来安装Docker
Docker与虚拟机区别
类型 | Docker | 虚拟机 |
容量 | 占用小 | 占用大 |
启动时间 | 毫秒 | 1~3分钟 |
安全性 | 共享内核,不安全 | 安全 |
yum仓库安装docker
安装依赖包
[root@localhost ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
配置详解:
- yum-utils提供了yum-config-manager
- device mapper存储驱动程序需要device-mapper-persistent-data和lvm2
- Device Mapper是Linux2.6内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构。
设置阿里云镜像源
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# ll /etc/yum.repos.d/
总用量 8
drwxr-xr-x. 2 root root 187 9月 19 10:16 backup
-rw-r--r--. 1 root root 2640 3月 16 2020 docker-ce.repo ##添加的镜像源
-rw-r--r--. 1 root root 226 9月 19 10:19 local.repo
安装Docker-ce
[root@localhost ~]# yum -y install docker-ce
注:错误:软件包:containerd.io-1.3.7-3.1.el7.x86_64 (docker-ce-stable)
需要:container-selinux >= 2:2.74
错误:软件包:3:docker-ce-19.03.13-3.el7.x86_64 (docker-ce-stable)
需要:container-selinux >= 2:2.74
您可以尝试添加 --skip-broken 选项来解决该问题
您可以尝试执行:rpm -Va --nofiles --nodigest
安装时需要将backup里面的yum源移出
启动docker
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2020-11-25 14:13:56 CST; 7s ago
Docs: https://docs.docker.com
Main PID: 16119 (dockerd)
Memory: 33.4M
CGroup: /system.slice/docker.service
└─16119 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
[root@localhost ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:3d:9e:a1:54 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
镜像加速
[root@localhost ~]# cd /etc/docker/
[root@localhost docker]# ll
总用量 4
-rw-------. 1 root root 244 11月 25 14:13 key.json
[root@localhost docker]# tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://wy3fhvf9.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://wy3fhvf9.mirror.aliyuncs.com"]
}
[root@localhost docker]# ls
daemon.json key.json
[root@localhost docker]# systemctl daemon-reload ##重载系统参数
[root@localhost docker]# systemctl restart docker ##重启docker服务
网络优化
[root@localhost docker]# vi /etc/sysctl.conf
net.ipv4.ip_forward=1 开启路由功能
[root@localhost docker]# sysctl -p
net.ipv4.ip_forward = 1
[root@localhost docker]# systemctl restart network
[root@localhost docker]# systemctl restart docker
查看docker版本
[root@localhost 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
搜索在线镜像
[root@localhost 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]
下载镜像
[root@localhost 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@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc9a0695f571 6 hours ago 133MB
删除镜像
[root@localhost docker]# docker images ##查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc9a0695f571 6 hours ago 133MB
tomcat latest e0bd8b34b4ea 6 days ago 649MB
可以选取镜像id或名称删除
[root@localhost docker]# docker rmi tomcat:latest
Untagged: tomcat:latest
添加镜像标签
[root@localhost docker]# docker tag nginx:latest nginx:moon ##添加标签
[root@localhost docker]# docker images ##查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc9a0695f571 6 hours ago 133MB
nginx moon bc9a0695f571 6 hours ago 133MB
注:删除该镜像时由于镜像id一致,只能选择名称+标签删除,否则会报以下错误
存出镜像
[root@localhost docker]# docker save -o /opt/nginx nginx:latest ##存出镜像命名为nginx存在当前目录下
[root@localhost docker]# ls /opt
containerd nginx rh
载入镜像
[root@localhost docker]# docker images ##查询镜像时无镜像文件
REPOSITORY TAG IMAGE ID CREATED SIZE
导入系统存入的镜像
[root@localhost docker]# docker load < /opt/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@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc9a0695f571 7 hours ago 133MB
容器操作
参数详解
Options | Mean |
-i | 以交互模式运行容器,通常与 -t 同时使用; |
-t | 为容器重新分配一个伪输入终端,通常与 -i 同时使用; |
-d | 后台运行容器,并返回容器ID; |
容器创建
[root@localhost docker]# docker create -it nginx:latest /bin/bash
fc2dbc351d97066bec82a231f553aa6ad4acc1d129efb874da258b4ceda392cd
查看容器
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc2dbc351d97 nginx:latest "/docker-entrypoint.…" 27 seconds ago Created mystifying_margulis
启动容器
[root@localhost docker]# docker start fc2dbc351d97
fc2dbc351d97
[root@localhost docker]# docker ps -a ##查看状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc2dbc351d97 nginx:latest "/docker-entrypoint.…" About a minute ago Up 14 seconds 80/tcp mystifying_margulis
另一种方法(创建和启动同步)
[root@localhost docker]# docker run -itd tomcat /bin/bash
3de84dc1289a72a61f615dd437d36875d1434b3c5301deee4d748d09f7474ea2
[root@localhost docker]#
查看容器
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45e832dc52f5 centos:7 "/bin/bash" About a minute ago Up About a minute reverent_diffie
3de84dc1289a tomcat "/bin/bash" 41 minutes ago Up 41 minutes 8080/tcp musing_villani
fc2dbc351d97 nginx:latest "/docker-entrypoint.…" 50 minutes ago Up 49 minutes 80/tcp mystifying_margulis
进入容器
[root@localhost docker]# docker exec -it 45e832dc52f5 /bin/bash
[root@45e832dc52f5 /]#
[root@45e832dc52f5 /]# exit 退出容器
安装网络工具
[root@45e832dc52f5 /]# yum -y install net-tools
删除容器
先停止服务
[root@localhost docker]# docker stop 45e832dc52f5
45e832dc52f5
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45e832dc52f5 centos:7 "/bin/bash" 13 minutes ago Exited (137) 35 seconds ago reverent_diffie
3de84dc1289a tomcat "/bin/bash" 54 minutes ago Up 54 minutes 8080/tcp musing_villani
fc2dbc351d97 nginx:latest "/docker-entrypoint.…" About an hour ago Up About an hour 80/tcp mystifying_margulis
再删除服务
[root@localhost docker]# docker rm 45e832dc52f5
45e832dc52f5
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3de84dc1289a tomcat "/bin/bash" 55 minutes ago Up 55 minutes 8080/tcp musing_villani
fc2dbc351d97 nginx:latest "/docker-entrypoint.…" About an hour ago Up About an hour 80/tcp mystifying_margulis
[root@localhost docker]#
批量删除容器
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea2266cab039 nginx:moon "/bin/bash" 2 seconds ago Up 2 seconds tender_curie
7342ef3e8fc4 nginx:moon "/bin/bash" About a minute ago Up About a minute romantic_mayer
2869c196ba41 nginx:moon "/bin/bash" 4 minutes ago Exited (0) 5 seconds ago mystifying_poincare
63cf5adb7b84 nginx:moon "/bin/bash" 5 minutes ago Exited (0) 4 minutes ago sleepy_cerf
3de84dc1289a tomcat "/bin/bash" 2 hours ago Exited (0) 29 minutes ago musing_villani
[root@localhost opt]# docker ps -a | awk '{print "docker rm "$1}' | bash ##未停止的容器删除不了
Error: No such container: CONTAINER
Error response from daemon: You cannot remove a running container ea2266cab0398cd9706a800c21889bc999eb781bf66c6405ae05f59799111780. Stop the container before attempting removal or force remove
Error response from daemon: You cannot remove a running container 7342ef3e8fc430c61b3b455c0f439471bd4a5b9972fa3d07ba493ed2f7fe8cbc. Stop the container before attempting removal or force remove
2869c196ba41
63cf5adb7b84
3de84dc1289a
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea2266cab039 nginx:moon "/bin/bash" 3 minutes ago Up 3 minutes tender_curie
7342ef3e8fc4 nginx:moon "/bin/bash" 5 minutes ago Up 5 minutes romantic_mayer
注:删除容器的镜像时需要先删除容器
容器导出
[root@localhost docker]# docker export fc2dbc351d97 > /opt/nginx-c
[root@localhost docker]# ll /opt
总用量 266056
drwx--x--x. 4 root root 28 11月 25 14:13 containerd
-rw-------. 1 root root 137096704 11月 25 15:00 nginx
-rw-r--r--. 1 root root 135344128 11月 25 16:19 nginx-c
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
容器导入
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest e0bd8b34b4ea 6 days ago 649MB
centos 7 8652b9f0cb4c 11 days ago 204MB
[root@localhost docker]# cd /opt/
[root@localhost opt]# cat nginx-c | docker import - nginx:moon ##会生成镜像,不会创建容器
sha256:3a23aa75b84b76cd8d03961e69833580d101eceeebceb6ba62ed241b74f39f03
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx moon 3a23aa75b84b 14 seconds ago 131MB
tomcat latest e0bd8b34b4ea 6 days ago 649MB
centos 7 8652b9f0cb4c 11 days ago 204MB