镜像准备
系统使用一个centos7.9的基础镜像
基础环境
# 查看centos7.9系统,具体可以使用的版本号
[root@docker ~]# curl -s https://hub.docker.com/v2/repositories/library/centos/tags/ | jq -r '.results[].name' | grep "7.9"
centos7.9.2009
7.9.2009
拉取镜像
[root@docker ~]# docker pull centos:7.9.2009
7.9.2009: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7.9.2009
docker.io/library/centos:7.9.2009
# 查看centos相关的镜像
[root@docker ~]# docker images centos*
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.9.2009 eeb6ee3f44bd 2 years ago 204MB
基础包准备
# 进入容器
[root@docker ~]# docker run -it centos:7.9.2009 /bin/bash
[root@0220f7a961f7 /]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
# 更新yum源,epel源
[root@0220f7a961f7 /]# rm -rf /etc/yum.repos.d/*
[root@b46417512e5e /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2523 100 2523 0 0 6362 0 --:--:-- --:--:-- --:--:-- 6355
[root@b46417512e5e /]# curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 664 100 664 0 0 2282 0 --:--:-- --:--:-- --:--:-- 2281
[root@b46417512e5e /]# ls /etc/yum.repos.d/
CentOS-Base.repo epel.repo
# 安装基础包
[root@b46417512e5e /]# yum install vim net-tools nginx -y
注意⚠️
基础镜像没有wget命令,这是使用了curl命令,而不是使用yum安装wget。yum安装完必备的软件包也没有使用yum makecache生成缓存,而且最后还要清空缓存。原因?
这是为了镜像的精简化,尽可能缩小docker镜像的体积。
启动Nginx进程
# 启动Nginx并查看进程
[root@58dd740e4b7d /]# nginx
[root@58dd740e4b7d /]# ps -ef | grep nginx
root 119 1 0 09:06 ? 00:00:00 nginx: master process nginx
nginx 120 119 0 09:06 ? 00:00:00 nginx: worker process
root 122 1 0 09:06 pts/0 00:00:00 grep --color=auto nginx
# 修改index.html文件,写点内容
[root@58dd740e4b7d ~]# cd /usr/share/nginx/html
[root@58dd740e4b7d html]# rm -rf ./*
[root@58dd740e4b7d html]# echo "docker nginx success" > index.html
# 宿主机访问测试
[root@docker ~]# curl -I 172.17.0.2
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Fri, 07 Jun 2024 09:10:28 GMT
Content-Type: text/html
Content-Length: 21
Last-Modified: Fri, 07 Jun 2024 09:08:57 GMT
Connection: keep-alive
ETag: "6662ce29-15"
Accept-Ranges: bytes
[root@docker ~]# curl 172.17.0.2
docker nginx success
推送镜像
登陆DockerHub
[root@docker ~]# docker login
Log in with your Docker ID or email address to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com/ to create one.
You can log in with your password or a Personal Access Token (PAT). Using a limited-scope PAT grants better security and is required for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/
Username: liangbizhi
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
推送
# 提交容器记录
# commit命令:从容器创建一个新的镜像
[root@docker ~]# docker commit 58dd740e4b7d liangbizhi/240607-nettools-vim-nginx-centos7
sha256:a7580a07dab4e1958734dcba7feb212ae6ecc39174dd1691234b459cdc67f036
# 查看镜像
[root@docker ~]# docker images liangbizhi/*
REPOSITORY TAG IMAGE ID CREATED SIZE
liangbizhi/240607-nettools-vim-nginx-centos7 latest a7580a07dab4 About a minute ago 587MB
# 推送镜像至DockerHub
[root@docker ~]# docker push liangbizhi/240607-nettools-vim-nginx-centos7
使用自定义镜像运行Nginx容器
[root@docker ~]# docker run -d -p 80:80 liangbizhi/240607-nettools-vim-nginx-centos7 nginx -g "daemon off;"
本地导出导入镜像
还有这样一种本地导出镜像的方式,可以将自定义的镜像导出为tar,然后发送至任意一台机器上,加载启动该镜像。
# 源机器导出镜像
[root@docker ~]# docker save liangbizhi/240607-nettools-vim-nginx-centos7 > /opt/240607-nettools-vim-nginx-centos7.tar
[root@docker ~]# ls /opt/240607-nettools-vim-nginx-centos7.tar
/opt/240607-nettools-vim-nginx-centos7.tar
# 发送到目标机器的opt目录下
[root@docker ~]# scp /opt/240607-nettools-vim-nginx-centos7.tar root@目标机器IP:/opt/
# 目标机器加载该镜像
docker load < /opt/240607-nettools-vim-nginx-centos7.tar