使用Dockerfile创建应用镜像及私有仓库

 

目录

 

   1.Dockerfile介绍   

 2.Dockerfile应用

1.创建目录

2.跳转目录

3.编辑启动脚本

4.编辑index.html文件

5.编辑 Dockerfile

6.创建镜像

7.查看镜像

8.创建容器测试

9.挂载其他项目测试

 3.私有仓库

1.安装仓库镜像---仓库注册器

 2.创建目录

 3.启动容器、映射端口、挂载目录

 4.访问仓库

 5.配置pull和push,修改daemon.json文件

 6.重启docker服务

 7.启动registry容器

8.查看镜像,打上标签


   1.Dockerfile介绍   

        Dockerfile 是一种用于定义和构建 docker 镜像的文本文件。它包含一系列的指令和参数,用于描述镜像的构建过程,包括基础映像、软件包安装、文件拷贝、环境变量设置等。

        通过编写 dockerfile,可以将应用程序、环境和依赖项打包成一个独立的容器镜像,使其可以在不同的环境和平台上运行,实现应用程序的可移植性和可扩展性。

        在Docker file中定义所需要执行的指令,使用docker build创建镜像,过程中会按照dockerfile所定义的内容进行打开临时性容器,把docker file中命令全部执行完成,就得到了⼀个容器应用镜像,每一行命令都会出现容器,都会使用docker commit进行提交一个临时性的镜像。

注意执行的命令越多,镜像体积越大,所以需要优化镜像。

Docker file关键字

1.FORM 指定基础镜像为该镜像的最后修改版本

2.FROM 指定基础镜像为该镜像的一个tag版本

3.MAINTAINER  指定镜像创建者,企业内部不用指定,对外发布也可以不指定

3.RUN 运行命令,安装软件

4.CMD 设置container启动时进行的操作,如果容器镜像中有这个命名,启动容器时,不要手动让容器执行其他命令

5.ENTRYPORINT(入口点)cmd每次只能执行一个指令,entrypoint可以多行执行。

6.USER设置容器进程的一些用户

7.EXPOSE 暴露端口 指定容器需要映射到宿主机的端口

8.ENV   设置环境变量 -e

9.ADD 从宿主机src复制文件到容器的dest路径

10.volumn 指定挂载点

11.WROKDIR 切换目录

12.ONBUILD在子镜像中执行指令

 2.Dockerfile应用

通过dockerfile创建⼀个在启动容器时,就可以启动httpd服务的镜像

1.创建目录

[root@docker0 ~]# mkdir httpd0

2.跳转目录

[root@docker0 ~]# cd httpd0/

3.编辑启动脚本

[root@docker0 httpd0]# vim abc.sh
[root@docker0 httpd0]# cat abc.sh
#!/bin/bash
rm -rf /run/httpd/*
exec /sbin/httpd -D FOREGROUND

4.编辑index.html文件

[root@docker0 httpd0]# echo "asdfgasdfg" > index.html
[root@docker0 httpd0]# ls
abc.sh  index.html

5.编辑 Dockerfile

[root@docker0 httpd0]# vim Dockerfile
FROM    centos:latest    

MAINTAINER      "centos httpd server"

RUN     rm -rf /etc/yum.repos.d/*

RUN     curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

RUN     yum clean all && yum makecache

RUN     yum install -y epel-release

RUN     yum -y install httpd

ADD     abc.sh /abc.sh

ADD     index.html /var/www/html/index.html

RUN     chmod -v -x /abc.sh

CMD     ["/bin/bash","/abc.sh"]

6.创建镜像

[root@docker0 httpd0]# docker build -t centos:httpd .

使用Dockerfile创建应用镜像及私有仓库_第1张图片

7.查看镜像

docker image ls

8.创建容器测试

[root@docker0 httpd0]# docker run -d --name c0 centos:httpd
0243403fbf233185d856278917f1129280e3c970491aeab7ddc35f570017d26b
[root@docker0 httpd0]# docker ps --all
CONTAINER ID   IMAGE          COMMAND               CREATED          STATUS          PORTS     NAMES
0243403fbf23   centos:httpd   "/bin/bash /abc.sh"   12 seconds ago   Up 11 seconds             c0
[root@docker0 httpd0]# curl 172.17.0.2
asdfgasdfg

9.挂载其他项目测试

[root@docker0 httpd0]# docker run -d -v /opt/:/var/www/html --name c1 -p80 centos:httpd
933b572d1f829e4623a2163a9b85b66c57f7183151b81c515c286673e44aaccf
[root@docker0 httpd0]# ls /opt/
assets  containerd  favicon.ico  index.html
[root@docker0 httpd0]# docker ps --all
CONTAINER ID   IMAGE          COMMAND               CREATED          STATUS          PORTS                                     NAMES
933b572d1f82   centos:httpd   "/bin/bash /abc.sh"   24 seconds ago   Up 23 seconds   0.0.0.0:32768->80/tcp, :::32768->80/tcp   c1
0243403fbf23   centos:httpd   "/bin/bash /abc.sh"   3 minutes ago    Up 3 minutes                                              c0

浏览器访问:

使用Dockerfile创建应用镜像及私有仓库_第2张图片

练习:配置nginx应用镜像 

[root@docker0 ~]# mkdir nginx
[root@docker0 ~]# cd nginx/
[root@docker0 nginx]# vim Dockerfile 
FROM centos:yum

MAINTAINER	"NGINX"

RUN	yum -y install nginx

RUN	echo "daemon off;" >> /etc/nginx/nginx.conf

WORKDIR /

CMD /usr/sbin/nginx

[root@docker0 nginx]# docker build -t centos:nginx .
[+] Building 166.9s (7/7) FINISHED                  docker:default
 => [internal] load build definition from Dockerfile          0.0s
 => => transferring dockerfile: 182B                          0.0s
 => [internal] load metadata for docker.io/library/centos:yu  0.0s
 => [internal] load .dockerignore                             0.0s
 => => transferring context: 2B                               0.0s
 => [1/4] FROM docker.io/library/centos:yum                   0.0s
 => [2/4] RUN YUM -y install nginx                          166.1s
 => [3/4] RUN ECHO "daemon off;" >> /etc/nginx/nginx.conf     0.2s 
 => exporting to image                                        0.5s 
 => => exporting layers                                       0.5s 
 => => writing image sha256:54d2fca7546bc6a54c7ef5eace3529cb  0.0s 
 => => naming to docker.io/library/centos:nginx               0.0s 
[root@docker0 nginx]# docker run -d --name nginx centos:nginx
d30397d951288976e202d6707b54759e1aab74aa579a52b089f5c6769583bd2b
[root@docker0 nginx]# docker ps --all
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS     NAMES
d30397d95128   centos:nginx   "/bin/sh -c /usr/sbi…"   47 seconds ago   Up 46 seconds             nginx
[root@docker0 nginx]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
centos       nginx     54d2fca7546b   20 minutes ago   403MB
centos       yum       662ca15b0cde   26 minutes ago   260MB
centos       httpd     1f7c1fd8d415   2 hours ago      338MB
centos       latest    5d0da3dc9764   2 years ago      231MB

 3.私有仓库

1.安装仓库镜像---仓库注册器

[root@docker0 nginx]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
930bdd4d222e: Pull complete 
a15309931e05: Pull complete 
6263fb9c821f: Pull complete 
86c1d3af3872: Pull complete 
a37b1bf6a96f: Pull complete 
Digest: sha256:12120425f07de11a1b899e418d4b0ea174c8d4d572d45bdb640f93bc7ca06a3d
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@docker0 nginx]# 
[root@docker0 nginx]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
centos       nginx     54d2fca7546b   33 minutes ago   403MB
centos       yum       662ca15b0cde   39 minutes ago   260MB
centos       httpd     1f7c1fd8d415   2 hours ago      338MB
registry     latest    cfb4d9904335   10 months ago    25.4MB
centos       latest    5d0da3dc9764   2 years ago      231MB

 2.创建目录

[root@docker0 nginx]# mkdir /regist

 3.启动容器、映射端口、挂载目录

[root@docker0 nginx]# docker run -d --name r1 -v /regist/:/var/lib/registry -p5000:5000 registry:latest
762990d22569e028f8115504f71c7d80b7e8c5da2ab0cd9823185c0a5e1eb208
[root@docker0 nginx]# docker ps --all
CONTAINER ID   IMAGE             COMMAND                   CREATED         STATUS         PORTS                                       NAMES
762990d22569   registry:latest   "/entrypoint.sh /etc…"   9 seconds ago   Up 8 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   r1

 4.访问仓库

[root@docker0 nginx]# curl localhost
curl: (7) Failed connect to localhost:80; 拒绝连接
[root@docker0 nginx]# curl localhost:5000/v2/_catalog
{"repositories":[]}

使用Dockerfile创建应用镜像及私有仓库_第3张图片

 5.配置pull和push,修改daemon.json文件

{
…
"insecure-registries":[
                "http://192.168.1.50:5000"
        ]
}

 6.重启docker服务

[root@docker0 ~]# systemctl restart docker

 7.启动registry容器

[root@docker0 ~]# docker ps --all
CONTAINER ID   IMAGE             COMMAND                   CREATED         STATUS                      PORTS     NAMES
f17b085f6f38   registry:latest   "/entrypoint.sh /etc…"   5 minutes ago   Exited (2) 18 seconds ago             r1
[root@docker0 ~]# docker start r1
r1
[root@docker0 ~]# curl localhost:5000/v2/_catalog
{"repositories":[]}

8.查看镜像,打上标签

[root@docker0 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED             SIZE
centos       nginx     54d2fca7546b   55 minutes ago      403MB
centos       yum       662ca15b0cde   About an hour ago   260MB
centos       httpd     1f7c1fd8d415   2 hours ago         338MB
registry     latest    cfb4d9904335   10 months ago       25.4MB
centos       latest    5d0da3dc9764   2 years ago         231MB
[root@docker0 ~]# docker tag --help

Usage:  docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Aliases:
  docker image tag, docker tag

[root@docker0 ~]# docker tag centos:yum 192.168.1.50:5000/centos:yum
[root@docker0 ~]# docker images
REPOSITORY                 TAG       IMAGE ID       CREATED             SIZE
centos                     nginx     54d2fca7546b   58 minutes ago      403MB
192.168.1.50:5000/centos   yum       662ca15b0cde   About an hour ago   260MB
centos                     yum       662ca15b0cde   About an hour ago   260MB
centos                     httpd     1f7c1fd8d415   2 hours ago         338MB
registry                   latest    cfb4d9904335   10 months ago       25.4MB
centos                     latest    5d0da3dc9764   2 years ago         231MB

[root@docker0 ~]# docker push 192.168.1.50:5000/centos:yum
The push refers to repository [192.168.1.50:5000/centos]
0750ba8bd49a: Pushed 
yum: digest: sha256:9073337da64bd552f4272a7bb1beb7c4126d3477b65f593d3d47a9fce888be67 size: 529
[root@docker0 ~]# curl localhost:5000/v2/_catalog
{"repositories":["centos"]}

你可能感兴趣的:(云计算学习,eureka,云原生,docker,服务器,linux,运维)