1) 制作镜像
# 使用commit制作镜像
1. 拉取基础镜像
docker pull 192.168.192.250:5000/smok/centos7
2. 运行基础镜像
docker container run --name base_centos7 -itd 192.168.192.250:5000/smok/centos7 # 后台运行容器
docker container ls # 查看启动的容器
3. 进入容器安装nginx,vim, net-tools
docker container exec -it base_centos7 bash
[root@51d21c09e1e8 yum.repos.d]# yum -y install nginx
[root@51d21c09e1e8 yum.repos.d]# yum -y install vim
[root@51d21c09e1e8 yum.repos.d]# yum -y install net-tools
[root@51d21c09e1e8 ~]# exit
4. 停容器制作镜像
docker container stop base_centos7
docker container ls -a
docker container commit -m "为基础容器安装了nginx,vim,net-tools" base_centos7 nginx_vim_net-tools:v1
docker image ls
5. 检查新制作的nginx_vim_net-tools:v1镜像
docker container run --name nginx -itd nginx_vim_net-tools:v1
docker container exec -it nginx bash
[root@81e7b9d254ff /]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 使用Dockerfile制作镜像
1. 创建一个制作Dockerfile的目录
mdkir dockerfile_dir
cd dockerfile_dir/
2. 准备Dockerfile中用到的文件
cat > yum-server.repo << EOF
[base]
name=name=CentOS-$releasever - base
baseurl=http://192.168.192.250:18000/base
enabled=1
gpgcheck=0
[extras]
name=name=CentOS-$releasever - extras
baseurl=http://192.168.192.250:18000/extras
enabled=1
gpgcheck=0
[updates]
name=name=CentOS-$releasever - updates
baseurl=http://192.168.192.250:18000/updates
enabled=1
gpgcheck=0
[epel]
name=name=CentOS-$releasever - epel
baseurl=http://192.168.192.250:18000/epel
enabled=1
gpgcheck=0
EOF
3. 配置Dockerfile文件
cat > Dockerfile << EOF
FROM
192.168.192.250:5000/smok/centos7:latest
RUN mv /etc/yum.repos.d/*.repo /tmp
COPY yum-server.repo /etc/yum.repos.d/yum-server.repo
RUN yum -y install nginx
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
EOF
4. 制作镜像
docker image build -t nginx:v11 . # 制作镜像
docker image ls # 查看
5. 检查新制作的nginx_vim_net-tools:v1镜像
docker container run -itd --name nginx nginx:v11
docker container exec -it nginx bash
[root@5af9877a38d1 /]# curl -I http://127.0.0.1
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Wed, 10 Jun 2020 09:30:44 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
2) 镜像的导入和导出
# 使用save,load(推荐使用)
docker image --help # 查看帮助
1. 导出
docker image save nginx:v11 -o nginx_v11.tar
2. 导入
docker image rm nginx:v11
docker image ls
docker image load -i nginx_v11.tar
docker image ls
3. 测试
docker container run --name nginx -itd nginx:v11
docker container exec -it nginx bash
[root@9bdbe302e030 /]# curl -I http://127.0.0.1
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 11 Jun 2020 01:37:02 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
# 使用export,import
1. 导出
export 是导出一个容器归档
docker container run --name nginx -itd nginx:v11
docker container stop nginx
docker container ls -a # 查看准备导出容器的CONTAINER ID或名字
docker container export nginx -o nginx_v11.tar
2. 导入
docker container rm -f nginx
docker image rm -f nginx:v11
docker image ls
docker image import nginx_v11.tar nginx:v11
docker image ls
3. 测试
docker container run --name nginx -itd nginx:v11 bash
docker container exec -it nginx bash
[root@b35d9ee2a7d9 /]# nginx
[root@9bdbe302e030 /]# curl -I http://127.0.0.1
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 11 Jun 2020 01:37:02 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
网友评论