docker registry
启动容器时,docker daemon会试图从本地获取相关的镜像,本地镜像不存在时,其将从Registry中下载该镜像并保存到本地。
Registry用于保存docker镜像,包括镜像的层次结构和元数据。用户可以自建Registry,亦可使用官方的Docker Hub。
docker registry的分类:
- Sponsor Registry:第三方的Registry,供客户和Docker社区使用
- Mirror Registry:第三方的Registry,只让客户使用
- Vendor Registry:由发布docker镜像的供应商提供的registry
- Private Registry:通过设有防火墙和额外的安全层的私有实体提供的registry
docker registry的组成:
- Repository
- 由某特定的docker镜像的所有迭代版本组成的镜像仓库
- 一个Registry中可以存在多个Repository
- Repository可分为“顶层仓库”和“用户仓库”
- 用户仓库名称格式为“用户名/仓库名”
- 每个仓库可包含多个Tag(标签),每个标签对应一个镜像
- Index
- 维护用户帐户、镜像的检验以及公共命名空间的信息
- 相当于为Registry提供了一个完成用户认证等功能的检索接口
Docker Registry中的镜像通常由开发人员制作,而后推送至“公共”或“私有”Registry上保存,供其他人员使用,例如“部署”到生产环境。
docker镜像的制作
多数情况下,我们做镜像是基于别人已存在的某个基础镜像来实现的,我们把它称为base image。比如一个纯净版的最小化的centos、ubuntu或debian。
那么这个最小化的centos镜像从何而来呢?其实这个基础镜像一般是由Docker Hub的相关维护人员,也就是Docker官方手动制作的。这个基础镜像的制作对于Docker官方的专业人员来说是非常容易的,但对于终端用户来说就不是那么容易制作的了。
Docker Hub
Docker Hub is a cloud-based registry service which allows you to link to code repositories, build your images and test them, stores manually pushed images, and links to Docker Cloud so you can deploy images to your hosts.
It provides a centralized resource for container image discovery, distribution and change management, user and team collaboration, and workflow automation throughout the development pipeline.
Docker Hub provides the following major features:
- Image Repositories
- Find and pull images from community and official libraries, and manage, push to, and pull from private images libraries to which you have access.
- Automated Builds
- Automatically create new images when you make changes to a source code repository.
- Webhooks
- A feature of Automated Builds, Webhooks let you trigger actions after a successful push to a
repository.
- A feature of Automated Builds, Webhooks let you trigger actions after a successful push to a
- Organizations
- Create work groups to manage access to image repositories.
- GitHub and Bitbucket Integration
- Add the Hub and your Docker Images to your current workflows.
docker镜像的获取
To get Docker images from a remote registry(such as your own Docker registry)and add them to your local system, use the docker pull command:
# docker pull <registry>[:<port>]/[<namespace>/]<name>:<tag>
The is a host that provides the docker-distribution service on TCP (default:5000)
Together, and identify a particular image controlled by at the registry
- Some registries also support raw ;for those, is optional
- When it is included, however, the additional level of hierarchy that provides is usefull to distinguish between images with the same
The additional level of hierarchy of
Namespace | Examples(/) |
---|---|
organization | redhat/kubernetes, google/kubernetes |
login(username) | Alice/application, bob/application |
role | devel/database, test/database, prod/database |
镜像的生成
镜像的生成途径:
- Dockerfile
- 基于容器制作
- Docker Hub automated builds
基于容器制作镜像
Create a new image from container’s changes
Usage:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Options | Default | Description |
---|---|---|
—author, -a | Author (e.g., “John Hannibal Smith hannibal@a-team.com”) | |
-c, --change list | Apply Dockerfile instruction to the created image | |
-m, --message string | Commit message | |
-p, --pause | true | Pause container during commit |
拉去镜像
[root@docker ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
映射端口
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d17c0969592a centos "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp, :::80->80/tcp httpd
创建容器
[root@docker ~]# docker run -tid --name httpd -p 80:80 centos /bin/bash
[root@docker ~]# docker exec -it httpd /bin/bash
把软件包和阿里云yum源cp到容器
docker cp /etc/yum.repos.d/CentOS-Base.repo d17c0969592a:/etc/yum.repos.d/
docker cp /etc/yum.repos.d/epel* d17c0969592a:/etc/yum.repos.d/
docker cp /etc/yum.repos.d/epel-modular.repo d17c0969592a:/etc/yum.repos.d/
docker cp /etc/yum.repos.d/epel.repo d17c0969592a:/etc/yum.repos.d/
docker cp /etc/yum.repos.d/epeltestingmodular.repod17c0969592a:/etc/yum.repos.d/
docker cp /etc/yum.repos.d/epel-testing.repo d17c0969592a:/etc/yum.repos.d/
[root@d17c0969592a yum.repos.d]# ls
CentOS-Base.repo epel-modular.repo epel-testing-modular.repo epel-testing.repo epel.repo
[root@d17c0969592a yum.repos.d]#
[root@docker ~]# docker cp /usr/src/apr-1.6.5.tar.bz2 d17c0969592a:/
[root@docker ~]# docker cp /usr/src/apr-util-1.6.1.tar.bz2 d17c0969592a:/
[root@docker ~]# docker cp /usr/src/httpd-2.4.54.tar.bz2 d17c0969592a:/
[root@docker ~]#
所有配置
[root@d17c0969592a /]# ls
apr-1.6.5.tar.bz2 bin etc httpd-2.4.54.tar.bz2 lib64 media opt root sbin sys usr apr-util-1.6.1.tar.bz2
[root@d17c0969592a ~]# useradd -M -r -s /sbin/nologin/ apache
[root@d17c0969592a ~]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)
[root@d17c0969592a ~]# yum groups mark install "Development Tools"
[root@d17c0969592a ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ --allowerasing
[root@d17c0969592a ~]# yum install -y bash-completion
[root@d17c0969592a ~]# source /etc/profile.d/bash_completion.sh
[root@d17c0969592a src]# tar xf apr-1.6.5.tar.bz2
[root@d17c0969592a src]# ls
apr-1.6.5 apr-1.6.5.tar.bz2 apr-util-1.6.1.tar.bz2 debug httpd-2.4.54.tar.bz2 kernels
[root@d17c0969592a src]#
[root@d17c0969592a apr-1.6.5]# vi configure
# $RM "$cfgfile"
[root@d17c0969592a apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@d17c0969592a apr-1.6.5]# make && make install
[root@d17c0969592a src]# tar xf apr-util-1.6.1.tar.bz2
[root@d17c0969592a src]# cd apr-util-1.6.1/
[root@d17c0969592a apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@d17c0969592a apr-util-1.6.1]# make && make install
[root@d17c0969592a httpd-2.4.54]#
./configure --prefix=/usr/local/apache \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
[root@d17c0969592a httpd-2.4.54]# make && make install
测试
第一种方法绝对路径
[root@docker ~]# docker commit -c 'CMD ["/usr/local/apache/bin/apachectl","-D","FOREGROUND"]' -p d17c0969592a centos-uu
sha256:af26c6352bf02ff0a696c54bda75e677be1cb0c78f61bd8bea75638a0040587e
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-uu latest af26c6352bf0 8 seconds ago 749MB
centos latest 5d0da3dc9764 10 months ago 231MB
[root@docker ~]# docker run -d -it --name haha centos-uu
febbae72ef19c36be613cc62c8b86f4ca0e2386ea893e399e3fbec8ec3d720df
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
febbae72ef19 centos-uu "/usr/local/apache/b…" 8 seconds ago Up 7 seconds 80/tcp haha
d17c0969592a centos "/bin/bash" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp, :::80->80/tcp httpd
[root@docker ~]# docker inspect haha
"Cmd": [
"/usr/local/apache/bin/apachectl",
"-D",
"FOREGROUND"
],
"Image": "centos-uu",
"Volumes": null,
[root@docker ~]# curl 172.17.0.3
<html><body><h1>It works!</h1></body></html>
[root@docker ~]#
第二方法脚本
[root@febbae72ef19 /]# vi haha.sh
[root@febbae72ef19 /]# chmod +x haha.sh
[root@febbae72ef19 /]# cat haha.sh
#!/bin/bash
/usr/local/apache/bin/apachectl start
sleep 5d
[root@docker ~]# docker commit -c 'CMD ["./haha.sh"]' -p haha centos:b5
sha256:6402bc9f799450052a2c1fde2ad37ee37a7a060d85c4be2dc622d194d21eeea6
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos b5 6402bc9f7994 About a minute ago 749MB
centos-uu latest af26c6352bf0 2 hours ago 749MB
centos latest 5d0da3dc9764 10 months ago 231MB
[root@docker ~]#
[root@docker ~]# docker run -d -it --name hh centos:b5
ab336e978f6ee87f5d2d1cc69038bd9a5d918be4c01ba5ed149d6fb64d2c4ea6
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab336e978f6e centos:b5 "./haha.sh" 7 seconds ago Up 5 seconds 80/tcp hh
[root@docker ~]# docker inspect hh
"Cmd": [
"./haha.sh"
],
"Image": "centos:b5",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "ea606d89da2c810bf936c56968635bde59074ffbc1202f9ed1533fb2219104d0",
"EndpointID": "22c6a8d663ffbb7f61d3d1a1d334b2d98a563e2d22eb35b66d51c6a249f48c60",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
[root@docker ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@docker ~]#
上传镜像
[root@docker ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: hzsjxx
Password:
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@docker ~]# docker tag centos-uu:latest hzsjxx/httpd:fuwu
[root@docker ~]# docker push hzsjxx/httpd:fuwu
The push refers to repository [docker.io/hzsjxx/httpd]
5a6929b0094d: Layer already exists
74ddd0ec08fa: Layer already exists
fuwu: digest: sha256:e8d6b7086f6d3fb2bfebc23c7afd3e46b4a4e6d9248edce5c342f6261986faa3 size: 742
[root@docker ~]#