Docker创建镜像和私有仓库建立

Docker镜像的分层

在这里插入图片描述

Docker镜像的创建

Docker镜像

  • 应用发布的标准格式
  • 支撑一个Docker容器的运行

Docker镜像的创建的方法

  • 基于已有镜像创建
  • 基于本地模板创建
  • 基于Dockerfile创建

案例:构建各类Docker镜像服务

案例环境

主机操作系统主机名/ip地址
服务器Centos 7.3 x86-64Localhost/192.168.254.137

需求描述

  • 基于容器(现有镜像)创建镜像服务
  • 基于模板创建镜像服务
  • 基于Dockerfile 创建常用基础服务

基于已有镜像创建

将容器里面运行的程序及运行环境打包生成新的镜像

Docker 操作指令
在这里插入图片描述

[root@localhost ~]# docker pull registry             # 私有仓库建立

//显示下载完成的信息
Using default tag: latest
latest: Pulling from library/registry
cbdbe7a5bc2a: Pull complete 
47112e65547d: Pull complete 
46bcb632e506: Pull complete 
c1cc712bcecd: Pull complete 
3db6272dcbfa: Pull complete 
Digest: sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest

//Dockerfile文件
[root@localhost ~]# mkdir apache                             
[root@localhost ~]# cd apache/

[root@localhost apache]# vim Dockerfile
FROM centos:7                             //基于的基础镜像
MAINTAINER The porject           //维护镜像的用户信息
RUN yum -y update                      
RUN yum -y install httpd                 //镜像操作指令安装apache软件
EXPOSE 80                         //开启80端口
ADD index.html /var/www/html/index.html         //复制网站首页文件
ADD run.sh /run.sh                                   //将执行脚本复制到镜像中
RUN chmod 755 /run.sh
CMD ["/run.sh"]                             //启动容器时执行脚本

[root@localhost ~]# vim run.sh
#!/bin/bash
rm -rf /run/httpd/*
exec /usr/sbin/apachectl -D FOREGROUND

[root@localhost ~]# echo "KY06-WEB" > index.html      //往首页文件输入数据
[root@localhost ~]# cat index.html 
KY06-WEB

[root@localhost ~]# docker build -t httpd:centos .           //生成镜像(别忘了末尾有" .")

[root@localhost ~]# docker run -d -p 1216:80 httpd:centos

测试

      http://192.168.254.137:1216/

在这里插入图片描述

私有仓库建立

[root@localhost ~]# docker pull registry

//下载信息
Using default tag: latest
latest: Pulling from library/registry
Digest: sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d
Status: Image is up to date for registry:latest
docker.io/library/registry:latest

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
http                centos              be56647852dd        4 hours ago         476MB
nginx               web                 c22d015f3ba2        19 hours ago        131MB
centos              7                   8652b9f0cb4c        12 days ago         204MB
registry            latest              2d4f4b5309b1        5 months ago        26.2MB

[root@localhost ~]# vim /etc/docker/daemon.json 
{
  "insecure-registries":["192.168.254.137:5000"],    //添加 (私有仓库端口号为5000)
  "registry-mirrors": ["https://yfseh35c.mirror.aliyuncs.com"]
}

[root@localhost ~]# systemctl restart docker.service 
[root@localhost ~]# docker create -it registry /bin/bash
cb1ed1bbe8ba03650cde4ce003aa92a14159eef1dc1850647f02000d718ef7c1
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
cb1ed1bbe8ba        registry            "/entrypoint.sh /bin…"   6 seconds ago       Created                                         jovial_fermi
51cc378e432e        registry            "/entrypoint.sh /etc…"   4 hours ago         Exited (2) 21 seconds ago                       affectionate_poitras
023364721bc7        registry            "/entrypoint.sh /bin…"   4 hours ago         Exited (127) 4 hours ago                        upbeat_banzai
b7f0e837232d        http:centos         "/run.sh"                4 hours ago         Exited (137) 4 hours ago                        festive_swartz
7776dc45d232        centos:7            "/bin/bash"              19 hours ago        Exited (137) 4 hours ago        

//宿主机的/data/registry自动创建挂载容器中的/tmp/registry
[root@localhost ~]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry 

[root@localhost ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
http                centos              be56647852dd        5 hours ago         476MB
nginx               web                 c22d015f3ba2        20 hours ago        131MB
centos              7                   8652b9f0cb4c        12 days ago         204MB
registry            latest              2d4f4b5309b1        5 months ago        26.2MB

[root@localhost ~]# docker tag http:centos 192.168.254.137:5000/httpd   //更改标记

[root@localhost ~]# docker images 
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
192.168.254.137:5000/httpd   latest              be56647852dd        5 hours ago         476MB
http                         centos              be56647852dd        5 hours ago         476MB
nginx                        web                 c22d015f3ba2        20 hours ago        131MB
centos                       7                   8652b9f0cb4c        12 days ago         204MB
registry                     latest              2d4f4b5309b1        5 months ago        26.2MB

[root@localhost ~]# docker push 192.168.254.137:5000/httpd     //上传
The push refers to repository [192.168.254.137:5000/httpd]
9dc08a637e93: Pushed 
365caa5264de: Pushed 
d65dd1be6f78: Pushed 
1f050344193d: Pushed 
8fcf7e82e7ef: Pushed 
174f56854903: Pushed 
latest: digest: sha256:6d515bcd4e56f9a88f5abe51169d304c96b61380c4c61d433868c3b61a9737f5 size: 1574

//先删除apache镜像
[root@localhost ~]# curl -XGET http://192.168.254.137:5000/v2/_catalog //获取私有仓库列表
{"repositories":["httpd"]}
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
71fd3f188ac7        registry            "/entrypoint.sh /etc…"   14 minutes ago      Exited (2) 3 minutes ago                          sad_jones
cb1ed1bbe8ba        registry            "/entrypoint.sh /bin…"   18 minutes ago      Exited (127) 16 minutes ago                       jovial_fermi
51cc378e432e        registry            "/entrypoint.sh /etc…"   4 hours ago         Exited (2) 19 minutes ago                         affectionate_poitras
023364721bc7        registry            "/entrypoint.sh /bin…"   4 hours ago         Exited (127) 4 hours ago                          upbeat_banzai
b7f0e837232d        http:centos         "/run.sh"                5 hours ago         Exited (137) 4 hours ago                          festive_swartz
7776dc45d232        centos:7            "/bin/bash"              19 hours ago        Exited (137) 4 hours ago                          optimistic_khorana
[root@localhost ~]# docker rm b7f0e837232d 
b7f0e837232d
[root@localhost ~]# docker rmi http:centos 
Untagged: http:centos
Deleted: sha256:be56647852dd193122f8c4a62b863686bad316ec444e409c9fafe03d08ffb772
Deleted: sha256:d87d00224629dc8aec77235827b8ccbe2e1882c6600716d5a4d304e9e831d47c
Deleted: sha256:759392b7659223c1006d46cbd12fb79683f5960237d5fc1b6da26be51e2b55b6
Deleted: sha256:a84e46f8b4bd46cf3e017eea87db4290c2a90ed63bd33071dc3a07fe2db83b23
Deleted: sha256:f45e4d41d85f922292da1a89d801f90630250f9712e95f7cbe1e1d4ba3cca8e8
Deleted: sha256:d44c78fd104414ef57125599ea67964506af11353bec5b79df1179cbdc0c5550
Deleted: sha256:61f7a4e2b63f6ca184949d05d85c3903580a8c24bd086189cde01ef6a45c4b6e
Deleted: sha256:417836d6ca5cff34d7f8631b92322b2862fb9be763ee68b0d3a7cbce1fd6f05b
Deleted: sha256:7dfb313508b1aef2643156d6dc6d370b093debf331d82a621d464d717b81bffb
Deleted: sha256:5715ebdfea5382cde2f9a84c14aabe81575282b58b5f5093e1c00e9e2da6242c
Deleted: sha256:458c2cdfb0828049b96a694e87d50f18e5f6eb9c1591e3bd36ed4c09bb3dc68a
Deleted: sha256:2611782a57a0e2accf537cf35ed7a6a6de9652ceed69c1b66209eb73eaa1b57d
Deleted: sha256:a163b6ecf94f7d572f8e4cb2680bcb3cd7d42bb126e8659f3c892b380b66c233
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               web                 c22d015f3ba2        20 hours ago        131MB
centos              7                   8652b9f0cb4c        12 days ago         204MB
registry            latest              2d4f4b5309b1        5 months ago        26.2MB


[root@localhost ~]# docker pull  192.168.254.137:5000/httpd       //私有仓库下载
Using default tag: latest
latest: Pulling from httpd
2d473b07cdd5: Already exists 
ffb4f13e466c: Pull complete 
7fc1195e558a: Pull complete 
0a0e380463bb: Pull complete 
229ca7e47e5e: Pull complete 
7bedf88621d3: Pull complete 
Digest: sha256:6d515bcd4e56f9a88f5abe51169d304c96b61380c4c61d433868c3b61a9737f5
Status: Downloaded newer image for 192.168.254.137:5000/httpd:latest
192.168.254.137:5000/httpd:latest

Docker 数据卷

docker pull centos      

[root@localhost ~]# docker run -v /var/www:/data1 --name web1 -it centos:7 /bin/bash
[root@a119f2d8add6 /]# cd data1/
[root@a119f2d8add6 data1]# ls
[root@a119f2d8add6 data1]# echo "this is test" > test.txt
[root@a119f2d8add6 data1]# ls
test.txt
[root@a119f2d8add6 data1]# cat test.txt 
this is test

返回宿主机查看在这里插入图片描述

数据卷容器

[root@localhost www]# docker run --name web100 -v /data1 -v /data2 -it centos:7 /bin/bash  //数据卷容器
[root@1d079e8f89e8 /]# ls 
anaconda-post.log  bin  data1  data2  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv 
[root@1d079e8f89e8 /]# ls data1
[root@1d079e8f89e8 /]# ls data2                          //目录下没有文件

更换终端登录

新容器挂载数据卷容器web100 并在data1、data2中创建文件

在这里插入图片描述
检验
在这里插入图片描述

端口映射

[root@localhost ~]# docker run -d -P 192.168.254.137:5000/httpd   // -P为随机端口映射

在这里插入图片描述

[root@localhost ~]# docker run -d -p 7777:80 httpd:latest

在这里插入图片描述

容器互联

[root@localhost ~]# docker run -itd -P --name web10 centos:7 /bin/bash 
[root@localhost ~]# docker run -itd -P --name web11 --link web10:web10 centos:7 /bin/bash 
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
61af28876da9        centos:7            "/bin/bash"         6 seconds ago       Up 5 seconds                            web11
8d1d18208175        centos:7            "/bin/bash"         39 seconds ago      Up 38 seconds                           web10
[root@localhost ~]# docker exec -it 61af28876da9 /bin/bash
[root@61af28876da9 /]# ping web10
PING web10 (172.17.0.2) 56(84) bytes of data.
64 bytes from web10 (172.17.0.2): icmp_seq=1 ttl=64 time=0.046 ms
64 bytes from web10 (172.17.0.2): icmp_seq=2 ttl=64 time=0.054 ms
64 bytes from web10 (172.17.0.2): icmp_seq=3 ttl=64 time=0.035 ms
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker镜像管理和私有仓库建构是Docker的重要组成部分。镜像管理是指如何管理和维护Docker镜像,包括创建、删除、更新和查看镜像等操作。私有仓库建构是指如何建立自己的Docker仓库,用于存放和分享自己的Docker镜像。 以下是关于Docker容器的镜像管理和私有仓库建构的一些基本知识点: 1. 镜像管理 Docker镜像是一个可执行的软件包,包含了运行某个应用程序所需的所有文件和依赖项。Docker镜像可以基于已有的镜像创建,也可以从Dockerfile构建。 基于已有的镜像创建新的镜像: ``` docker commit [CONTAINER_ID] [NEW_IMAGE_NAME] ``` 从Dockerfile构建新的镜像: ``` docker build -t [IMAGE_NAME] [DOCKERFILE_PATH] ``` 查看本地所有镜像: ``` docker images ``` 删除本地指定的镜像: ``` docker rmi [IMAGE_NAME] ``` 2. 私有仓库建构 Docker Hub是一个公共的Docker仓库,但是有时候我们需要建立自己的私有仓库,用于存放和分享自己的Docker镜像Docker官方提供了Docker Registry来搭建私有仓库,也可以使用第三方工具如Nexus、GitLab等来搭建私有仓库。 使用Docker Registry搭建私有仓库: ``` docker run -d -p 5000:5000 --name registry registry:2 ``` 在Dockerfile中指定私有仓库地址: ``` FROM [PRIVATE_REGISTRY]:[PORT]/[IMAGE_NAME]:[TAG] ``` 将本地镜像推送到私有仓库: ``` docker tag [LOCAL_IMAGE_NAME] [PRIVATE_REGISTRY]:[PORT]/[IMAGE_NAME]:[TAG] docker push [PRIVATE_REGISTRY]:[PORT]/[IMAGE_NAME]:[TAG] ``` 从私有仓库拉取镜像: ``` docker pull [PRIVATE_REGISTRY]:[PORT]/[IMAGE_NAME]:[TAG] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值