Dockerfile推送私有仓库的两个案例

一,编写Dockerfile制作Web应用系统nginx镜像,生成镜像nginx:v1.1,并推送其到私有仓库。

具体要求如下:
(1)基于centos基础镜像;
(2)指定作者信息;
(3)安装nginx服务,将提供的dest目录(提供默认主页index.html)传到镜像内,并将dest目录内的前端文件复制到nginx的工作目录;
(4)暴露80端口;
(5)设置服务自启动。
(6)验证镜像。

1.创建一个Dockerfile

#创建一个目录来存放实验需要的东西
[root@example /]# mkdir /docker_test
[root@example /]# mkdir /docker_test/demo01
[root@example /]# cd /docker_test/demo01

#创建dest目录来存放index.html文件
[root@example demo01]# touch ./dest/index.html
[root@example demo01]# echo "welcome to demo01" > dest/index.html
[root@example demo01]# cat dest/index.html
welcome to demo01

#编写Dockerfile
[root@example demo01]# vim Dockerfile
[root@example demo01]# cat Dockerfile 
#centos image
FROM centos

# author info
LABEL maintainer="[email protected]"

#install&& add src files
ADD http://nginx.org/download/nginx-1.24.0.tar.gz /usr/local/src/
ADD dest /usr/share/nginx/html

# expose ports
EXPOSE 80

#start nginx
CMD ["nginx", "-g", "daemon off;"]

#生成镜像
[root@example sbin]# docker build -t nginx:v1.1 .
[+] Building 76.2s (9/9) FINISHED                                      docker:default
 => [internal] load build definition from Dockerfile                             0.0s
 => => transferring dockerfile: 320B                                             0.0s
 => [internal] load .dockerignore                                                0.0s
 => => transferring context: 2B                                                  0.0s
 => [internal] load metadata for docker.io/library/centos:latest                 0.0s
 => http://nginx.org/download/nginx-1.24.0.tar.gz                               76.1s
 => [internal] load build context                                                0.0s
 => => transferring context: 60B                                                 0.0s
 => CACHED [1/3] FROM docker.io/library/centos                                   0.0s
 => [2/3] ADD http://nginx.org/download/nginx-1.24.0.tar.gz /usr/local/src/      0.0s
 => [3/3] ADD dest /usr/share/nginx/html                                         0.0s
 => exporting to image                                                           0.0s
 => => exporting layers                                                          0.0s
 => => writing image sha256:be8c3b5a7a9a32df1466e317cd02face514345f4be5855efa49  0.0s
 => => naming to docker.io/library/nginx:v1.1  

#查看一下生成的镜像                                 
[root@example demo01]# docker images | grep nginx
nginx                           v1.1      be8c3b5a7a9a   25 seconds ago   232MB
goharbor/nginx-photon           v2.8.4    375018db778b   7 days ago       116MB

 2.上传私有仓库

我用的私有仓库是Harbor,所以我在Harbor上创建一个项目来实现这次试验

Dockerfile推送私有仓库的两个案例_第1张图片

 

[root@example demo01]# docker login -u admin -p Harbor12345 192.168.163.143:80
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get "https://192.168.163.143:80/v2/": http: server gave HTTP response to HTTPS client
#这里我出现了报错,因为我之前在配置Harbor时把https注释禁止了,所以我需要再修改下配置文件
[root@example demo01]# vim /etc/docker/daemon.json
[root@example demo01]# cat /etc/docker/daemon.json
{
  "registry-mirrors": ["http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn"],
"insecure-registries":["http://192.168.163.143:80"]
}
[root@example demo01]# systemctl daemon-reload 
[root@example demo01]# systemctl restart docker

#这里是配置了镜像加速和http访问私有仓库
#之后再登录
[root@example demo01]# docker login -u admin -p Harbor12345 192.168.163.143: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@example demo01]# docker tag nginx:v1.1 192.168.163.143:80/demo01/nginx:v1.1
[root@example demo01]# docker push 192.168.163.143:80/demo01/nginx:v1.1
The push refers to repository [192.168.163.143:80/demo01/nginx]
8effb5216144: Pushed 
e1e25aa46a4e: Pushed 
74ddd0ec08fa: Pushed 
v1.1: digest: sha256:8e759f406f6f5e618bdb443862e8cb4454fd579e14985ea5a1173899823af02f size: 947

 

Dockerfile推送私有仓库的两个案例_第2张图片

二,Dockerfile快速搭建自己专属的LAMP环境,生成镜像lamp:v1.1,并推送到私有仓库。

 

具体要求如下:
(1)基于centos:6基础镜像;
(2)指定作者信息;
(3)安装httpd、mysql、mysql-server、php、php-mysql、php-gd;
(4)暴露80和3306端口;
(5)设置服务自启动。
(6)验证镜像。

#老样子,创建一个目录来存放
[root@example docker_test]# mkdir demo02
[root@example docker_test]# cd  demo02

#编写DOKERFILE
[root@example demo02]# vim Dockerfile
[root@example demo02]# cat Dockerfile
[root@example demo02]# cat Dockerfile
#来自centos6 
FROM centos:6
#作者信息
MAINTAINER "LL "
#下载一系列 
RUN yum install -y httpd mysql mysql-server php php-mysql php-gd
#释放端口
EXPOSE 80/tcp
EXPOSE 3306/tcp
#设置启动
CMD ["/bin/bash","-c", "service httpd start && service mysqld start && tail -f /dev/null"]

#生成镜像
[root@example demo02]# docker build -t lamp:v1.1 .
#运行镜像,查看功能
[root@example demo02]# docker run -it --rm lamp:v1.1 /bin/bash                
[root@66bddcc38330 /]#                                                        
[root@66bddcc38330 /]# php -v
PHP 5.4.16 (cli) (built: Apr  1 2020 04:07:17) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
[root@66bddcc38330 /]# exit
exit

#上传到私有仓库
[root@example demo02]# docker login -u admin -p Harbor12345 192.168.163.143:80WARNING! 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@example demo02]# docker tag lamp:v1.1 192.168.163.143:80/demo02/lamp:v1.1
[root@example demo02]# docker push 192.168.163.143:80/demo02/lamp:v1.1
The push refers to repository [192.168.163.143:80/demo02/lamp]
71836e35c11f: Pushed 
174f56854903: Pushed 
v1.1: digest: sha256:4dcda26c0d57bc6f7de11ddf983be80dc03162ed57da82b03a9c0d30d2d1ff5b size: 742







Dockerfile推送私有仓库的两个案例_第3张图片

 

你可能感兴趣的:(docker)