说明:我将用到的私有仓库是Harbor,安装教程参考我的这一篇文章:
安装搭建私有仓库Harbor_Word_Smith_的博客-CSDN博客
一、案例1
1、要求
编写Dockerfile制作Web应用系统nginx镜像,生成镜像nginx:v1.1,并推送其到私有仓库。具体要求如下:
(1)基于centos基础镜像
(2)指定作者信息
(3)安装nginx服务,将提供的dest目录(提供默认主页index.html)传到镜像内,并将dest目录内的前端文件复制到nginx的工作目录
(4)暴露80端口
(5)设置服务自启动
(6)验证镜像
2、实现步骤
(1)编写Dockerfile文件
#创建index.html前端文件
[root@node2 demo01]# cat index.html
welcome to demo01
#创建demo01目录用来创建Dockerfile
[root@node2 ~]# mkdir -p docker_demo/demo01
[root@node2 ~]# cd docker_demo/demo01/
[root@node2 demo01]# vim Dockerfile
#写入以下内容
FROM centos
MAINTAINER "benjm <benjm@163.com>"
ADD http://nginx.org/download/nginx-1.24.0.tar.gz /usr/local/src/
COPY index.html /usr/share/nginx/
EXPOSE 80
CMD ["/usr/sbin/nginx","-g","daemon off;"]
Dockerfile内容如下
(2)构建镜像
[root@node2 demo01]# docker build -t nginx:v1.1 ./
查看
(3) 推送到私有仓库
在harbor上创建项目demo01
#首先登录到私有仓库
[root@node2 demo01]# docker login -u admin -p Harbor12345 192.168.198.200:80
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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
#给镜像打上合适的标签
[root@node2 demo01]# docker tag nginx:v1.1 192.168.198.200:80/demo01/nginx:v1.1
[root@node2 demo01]# docker images | grep demo01
192.168.198.200:80/demo01/nginx v1.1 b4887857b526 8 minutes ago 231MB
#推送镜像到私有仓库
[root@node2 demo01]# docker push 192.168.198.200:80/demo01/nginx:v1.1
The push refers to repository [192.168.198.200:80/demo01/nginx]
02dfab95c1be: Pushed
74ddd0ec08fa: Pushed
v1.1: digest: sha256:888ed81c26b452ff94686c7709f1a6b668aeb2f2f7f80e4225eb83257428a8a7 size: 736
[root@node2 demo01]#
然后就能在harbor上看到推送的镜像了
二、案例2
1、要求
编写Dockerfile快速搭建自己专属的LAMP环境,生成镜像lamp:v1.1,并推送到私有仓库。具体要求如下:
(1)基于centos:6基础镜像
(2)指定作者信息
(3)安装httpd、mysql、mysql-server、php、php-mysql、php-gd
(4)暴露80和3306端口
(5)设置服务自启动
(6)验证镜像
2、实现步骤
(1)编写Dockerfile文件
[root@node2 ~]# mkdir -p docker_demo/demo02
[root@node2 ~]# cd docker_demo/demo02
[root@node2 demo02]# vim Dockerfile
#写入以下内容
FROM centos:6
MAINTAINER "benjm <benjm@163.com>"
ENV WEB_DOC_ROOT="/data/web/html"
RUN yum install httpd mysql mysql-server php php-mysql php-gd -y
EXPOSE 80/tcp
EXPOSE 3306/tcp
CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
Dockerfile内容如下
(2)构建镜像
[root@node2 demo02]# docker build -t lamp:v1.1 ./
查看
(3)推送到私有仓库
在harbot上创建项目demo02
#给镜像打上合适的标签
[root@node2 demo02]# docker tag lamp:v1.1 192.168.198.200:80/demo02/lamp:v1.1
#推送镜像到私有仓库
[root@node2 demo02]# docker push 192.168.198.200:80/demo02/lamp:v1.1
The push refers to repository [192.168.198.200:80/demo02/lamp]
74ddd0ec08fa: Mounted from demo01/nginx
v1.1: digest: sha256:396c671a6ab30d329ec504736d1a5d37025360e98c68896c70f9ddfadc845eba size: 529
[root@node2 demo02]#
然后就能在harbor上看到推送的镜像了