文章目录
安装依赖包
[root@localhost ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
为什么要这些依赖包呢?
yum-utils 是yum配置管理工具,提供yum-config-manager这条指令
device mapper存储驱动程序需要device-mapper-persistent-data和 lvm2,它是内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构,简单讲是为了让docker-ce能够平稳运行
- 设置阿里云镜像源
这里就用到了yum-config-manager工具,从官网添加镜像源
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装Docker-ce
[root@localhost yum.repos.d]# yum -y install docker-ce
因为我在做的时候,把光盘带的源放进backup里了,所以出现下面的报错
################################################################
报错
错误:软件包: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
解决: 把原来yum.repo里面 backup 里面的源拿出来,要用到线网源里更新的版本
###############################################################
关闭防火墙,核心防护
[root@localhost yum.repos.d]# systemctl stop firewalld
[root@localhost yum.repos.d]# setenforce 0
这些都是临时关闭,关机重启就不起效了
开启docker服务
[root@localhost yum.repos.d]# systemctl start docker.service
[root@localhost yum.repos.d]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
镜像加速
每个人在阿里云官网上都能找到自己的镜像加速器,请自行获取
操作方法如下:
登录以后 ,点右上角的控制台
这里能看到一个容器镜像服务
这边就是你个人的镜像加速器,如果是root登录就不用sudo
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["####加速网址######"]
}
EOF ##写入镜像加速
systemctl daemon-reload ##重载daemon
systemctl restart docker ##重启docker
镜像查看,容器查看
[root@localhost docker]# docker images ##查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost docker]# docker ps -a ##查看容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
网络加速
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@localhost ~]# sysctl -p
[root@localhost ~]# systemctl restart docker
搜索镜像
docker search 镜像名
搜索出来的镜像按热度降序排序 ,被下载最高的排在上面
[root@localhost ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 13981 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1906 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 791 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 128
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 109
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 103 [OK]
下载镜像
docker pull 镜像名
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
bb79b6b2107f: Pull complete #####可以看到它是分层下载的
5a9f1c0027a7: Pull complete
b5c20b2b484f: Pull complete
166a2418f7e8: Pull complete
1966ea362d23: Pull complete
Digest: sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 3 days ago 133MB
查看docker版本信息
[root@localhost ~]# 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
docker下载文件信息位置
[root@localhost ~]# cd /var/lib/docker
[root@localhost docker]# ls
builder containers network plugins swarm trust
buildkit image overlay2 runtimes tmp volumes
[root@localhost docker]# cd image/
[root@localhost image]# ls
overlay2
[root@localhost image]# cd overlay2/
[root@localhost overlay2]# ls ##可以看到这个.json文件
distribution imagedb layerdb repositories.json
获取镜像信息
docker inspect 镜像ID
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 3 days ago 133MB
[root@localhost overlay2]# docker inspect c39a868aad02
添加新标签
docker tag 需要添加标签的镜像标签 新标签
[root@localhost overlay2]# docker tag nginx:latest nginx:new
[root@localhost overlay2]# docker images ##可以看到标签不一样,ID是一样的
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 3 days ago 133MB
nginx new c39a868aad02 3 days ago 133MB
[root@localhost overlay2]# docker tag nginx:latest ng:web
[root@localhost overlay2]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ng web c39a868aad02 3 days ago 133MB
nginx latest c39a868aad02 3 days ago 133MB
nginx new c39a868aad02 3 days ago 133MB
镜像删除
多标签时只是把别名删掉了,镜像还在
docker rmi 标签/id
[root@localhost overlay2]# docker rmi ng:web
Untagged: ng:web
[root@localhost overlay2]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 3 days ago 133MB
nginx new c39a868aad02 3 days ago 133MB
[root@localhost overlay2]# docker rmi nginx:new
Untagged: nginx:new
删除 只剩一个时,删除标签或id都删掉了 ,但是存在多个标签时,不能直接删掉id,并且必须把镜像生成的容器删了,才能删镜像
[root@localhost overlay2]# docker rmi c39a868aad02
Untagged: nginx:latest
Untagged: nginx@sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Deleted: sha256:c39a868aad02a383c7e490e0fc4a5b0217f667f2de764bc2755e315a5adf64a1
Deleted: sha256:1af47386c12cb24b362bd785e71058ab675fbdcbda8e7a7ba0dd10c3cff75a2a
Deleted: sha256:25ee02f9e42f8cdca7ca8bc522cb69ac4a86a55401818a13c7a01e52a81894fe
Deleted: sha256:b1d2bc3292aa3d64794f99d2885d35b454993b4af24ad350a969d27925db7ebb
Deleted: sha256:4216e20d59f9fc90bccdd14493452532806d4803a25514366de2a9c5560624d0
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c
这样才是删除了镜像
镜像存出
存出镜像命名为nginx存在当前目录下 必须下载镜像才能存
docker save -o 存储路径 镜像
[root@localhost overlay2]# docker save -o /opt/nginx nginx:latest
镜像载入
docker load < 镜像文件
先把原镜像删了
[root@localhost overlay2]# docker rmi nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Deleted: sha256:c39a868aad02a383c7e490e0fc4a5b0217f667f2de764bc2755e315a5adf64a1
Deleted: sha256:1af47386c12cb24b362bd785e71058ab675fbdcbda8e7a7ba0dd10c3cff75a2a
Deleted: sha256:25ee02f9e42f8cdca7ca8bc522cb69ac4a86a55401818a13c7a01e52a81894fe
Deleted: sha256:b1d2bc3292aa3d64794f99d2885d35b454993b4af24ad350a969d27925db7ebb
Deleted: sha256:4216e20d59f9fc90bccdd14493452532806d4803a25514366de2a9c5560624d0
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c
再载入
[root@localhost overlay2]# docker load < /opt/nginx
d0fe97fa8b8c: Loading layer 72.49MB/72.49MB
2baf69a23d7a: Loading layer 64.54MB/64.54MB
2f57e21e4365: Loading layer 3.072kB/3.072kB
aee208b6ccfb: Loading layer 4.096kB/4.096kB
7b5417cae114: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
上传镜像
改标签
docker tag nginx:latest abner/nginx:web //用户名/仓库名:镜像名
登陆
docker login
Username: docker
password:
Email: xxx@xxx.com
上传
docker push abner/nginx:web
容器的制作
docker create [选项] 镜像运行的程序
-i 让容器的标准输入保持打开
-t 让Docker分配一个伪终端
查看容器运行状态
docker ps [选项]
-a 列出所有的容器,包括未运行的容器
[root@localhost opt]# docker create -it nginx:latest /bin/bash
477ec91aa35d8be7cdffc372ee11c5e4b933c5f2eba1d57935d1af427503dc5a
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
477ec91aa35d nginx:latest "/docker-entrypoint.…" 13 seconds ago Created great_pascal
此时状态为created,不能进入容器
启动容器
[root@localhost opt]# docker start 477ec91aa35d
477ec91aa35d
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
477ec91aa35d nginx:latest "/docker-entrypoint.…" 16 minutes ago Up 2 seconds 80/tcp great_pascal
容器一定要是up状态,才能进入
[root@localhost opt]# docker exec -it 477ec91aa35d /bin/bash
root@477ec91aa35d:/# 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@477ec91aa35d:/# exit
exit
容器平稳运行三步骤:下载镜像、创建容器、启动容器
三合一:
[root@localhost ~]# docker run paigeeworld/centos7 /usr/bin/bash -c ls /
Unable to find image 'paigeeworld/centos7:latest' locally
latest: Pulling from paigeeworld/centos7
Image docker.io/paigeeworld/centos7:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
a3ed95caeb02: Pull complete
7235103339cf: Pull complete
c962e85f182c: Pull complete
e945c72281a4: Pull complete
bd558d66c6e0: Pull complete
b6d31d07a319: Pull complete
Digest: sha256:356b32d72680f0b9e0489af16961b2c3de74e18cbd391ffcf5ab085997fedf2b
Status: Downloaded newer image for paigeeworld/centos7:latest
bin
boot
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@localhost ~]# docker images ##可以看到镜像下载了
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 3 days ago 133MB
paigeeworld/centos7 latest 4cbe7aa905e7 6 years ago 382MB
[root@localhost ~]# docker ps -a ###可以看到执行命令后容器关闭了
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b15ab7b44e1f paigeeworld/centos7 "/usr/bin/bash -c ls…" 45 seconds ago Exited (0) 44 seconds ago infallible_euclid
477ec91aa35d nginx:latest "/docker-entrypoint.…" 36 minutes ago Exited (0) 10 minutes ago great_pascal
持续在后台运行
[root@localhost ~]# docker run paigeeworld/centos7 /usr/bin/bash -c "while true;do echo hello;done"
[root@localhost ~]# docker stop d26af2696bd2
d26af2696bd2
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d26af2696bd2 paigeeworld/centos7 "/usr/bin/bash -c 'w…" 54 seconds ago Exited (137) 3 seconds ago loving_lehmann
b15ab7b44e1f paigeeworld/centos7 "/usr/bin/bash -c ls…" 21 minutes ago Exited (0) 21 minutes ago infallible_euclid
477ec91aa35d nginx:latest "/docker-entrypoint.…" 57 minutes ago Exited (0) 31 minutes ago great_pascal
执行完成结束,返回状态码0。如果未完成被强行终止,或者未执行成功,返回状态码非0
容器导出
[root@localhost ~]# docker export b15ab7b44e1f >/opt/nginx_c
[root@localhost ~]# ls /opt
containerd nginx nginx_c rh
容器批量删除
批量删除容器 只会删除关闭状态的容器
[root@localhost ~]# docker ps -a | awk '{print "docker rm "$1}' |bash
Error: No such container: CONTAINER 这一行是提示行,不符合所以报错
d26af2696bd2
b15ab7b44e1f
477ec91aa35d
开着的不会删掉
[root@localhost opt]# docker ps -a | awk '{print "docker rm "$1}'|bash
Error: No such container: CONTAINER
d30cfeeeeab9
Error response from daemon: You cannot remove a running container fd562f234cca299b2b544f53e899a85f8260cdc28ea3e6b235968adb89033f2f. Stop the container before attempting removal or force remove
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd562f234cca nginx:latest "/docker-entrypoint.…" 37 minutes ago Up About a minute 80/tcp cranky_agnesi
注意: rm后面有个空格然后跟$1,空格必须在“”内
容器导入(会生成镜像,而不会创建容器)
[root@localhost ~]# cat /opt/nginx_c | docker import - nginx:web
sha256:ba2ca205a1ea76c0a10826bfeb586f971c6daf6dc610fcca3be9eb2ece1825bc
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx web ba2ca205a1ea 20 seconds ago 0B
nginx latest c39a868aad02 3 days ago 133MB
paigeeworld/centos7 latest 4cbe7aa905e7 6 years ago 382MB
进程放在后台运行
[root@localhost opt]# docker run -itd centos:7 /bin/bash
Unable to find image 'centos:7' locally
7: Pulling from library/centos
75f829a71a1c: Pull complete
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:7
19ed00c77db9b8a2b0bde9042b8200e3e9f5cbd67f1dca6b01f7b5ac0dd83bb4
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19ed00c77db9 centos:7 "/bin/bash" 11 seconds ago Up 10 seconds quizzical_northcutt
fd562f234cca nginx:latest "/docker-entrypoint.…" 39 minutes ago Up 4 minutes 80/tcp cranky_agnesi