Docker other two topics: DockerHub and container network

No public concern, we can reply, "blog Park" No background in public, freely available knowledge of Java / information interview must-see.

Hello, Hello, everyone, I am a son You, as a small series this week, yesterday told you about myself, and today let us continue to learn a little rain south (southern fact, a lot of rain recently, ha ha) series of Docker it. The following is the text.

By the previous study, we believe that the basic operation of Docker has basically mastered, we will come and we talk about Docker's two other topics: DockerHub and container network!

This article is the fifth in the series, read the previous article contribute to a better understanding of the article:


  1. Docker entry and installation [Docker series -1]

  2. Docker containers Basic Operation [Docker series-2]

  3. Docker containers Advanced Operations [Docker Series -3]

  4. Basic operation Docker mirror [Docker series -4]


DockerHub

DockerHub code hosting services like GitHub offers, Docker Hub provides a mirror image hosting services, Docker Hub address https://hub.docker.com/.

Use Docker Hub readers can search, create, share and manage images. Mirroring divided into two categories on the Docker Hub, the official one is a mirror, for example, we previously used nginx, mysql, etc., there is a class of ordinary users mirror, mirror the average user uploaded by the user. For domestic users, if they feel Docker Hub access speed is too slow, you can use the mirror some domestic companies, such as Netease:

  • https://c.163yun.com/hub#/m/home/

As used herein, the official Docker Hub to demonstrate, interested readers can try Netease mirror sites.

First of all readers open Docker Hub, register an account, this is relatively simple, I will not go into details.

After account registration is successful, the command-line client can login account we have just registered, as follows:

See Login Succeeded represents a successful login!

After a successful login, then you can use the push command uploaded our homemade mirroring. Note homemade mirror to be able to upload, naming must meet the specifications that format, where namespace must be a user name, before we created Dockerfile text, for example, to rebuild a local mirror here and upload to Docker Hub, as follows:namespace/name

After the first call docker build command to rebuild a local mirror, successfully constructed by docker images you can see the local command have a wongsung / nginx called mirroring, followed by the command docker push mirroring uploaded to the server. After a successful upload, the user logs Docker Hub, you can see just the image has been uploaded successfully, the following:

See this represents the image has been uploaded successfully, the next, you can download others I just uploaded the image with the following command:

  1. docker pull wongsung/nginx

After pull down, it can be created directly based on the image of the container. Specific process of creating this reader can refer to our previous series.

Build Automation

Building automation is to use a connector comprising Docker Hub Dockerfile file GitHub BitBucket warehouse or repository, Docker Hub is automatically constructed mirror, constructed in this way out of the image will be marked as Automated Build, also known as a trusted Construction (Trusted build out build), a mirror image of this building, others are free to view content when using Dockerfile, mirroring know is how come, at the same time, due to the build process is automated, it is possible to ensure that the warehouse is mirrored Newest. Specific construction steps are as follows:

  • Add warehouse

First, log on to the Docker Hub, click Create the upper right corner, then select Create Automated Build, as shown below:

The new entrants to the page, select Link Account button, then select the connection GitHub, select the page in the connection, we choose the first connection, as follows:

After selection, according to the guidance log GitHub, complete the authorization operation, the page after the authorization is completed as follows:

  • Construction of Mirror

授权完成后,再次点击右上角的 Create 按钮,选择 Create Automated Build ,在打开的页面中选择 GitHub ,如下两张图:


这里展示了刚刚关联的 GitHub 上的仓库,只有一个 docker ,然后点击进去,如下:

填入镜像的名字以及描述,然后点击 Create 按钮,创建结果如下:

如此之后,我们的镜像就算构建成功了,一旦 GitHub 仓库中的 Dockerfile 文件有更新, Docker Hub 上的镜像构建就会被自动触发,不用人工干预,从而保证镜像始终都是最新的。

接下来,用户可以通过如下命令获取镜像:

  1. docker pull wongsung/nginx2

获取到镜像之后,再运行即可。

有没有觉得很神奇!镜像更新只要更新自己的 GitHub 即可。镜像就会自动更新!事实上,我们使用的大部分 镜像都是这样生成的。

构建自己的 DockerHub

前面我们使用的 Docker Hub 是由 Docker 官方提供的,我们也可以搭建自己的 Docker Hub ,搭建方式也很容器,因为 Docker 官方已经将 Docker 注册服务器做成镜像了,我们直接 pull 下来运行即可,没有没很酷!。具体步骤如下:

  • 拉取镜像

运行如下命令拉取registry官方镜像:

  1. docker pull registry

 

  • 运行

接下来运行如下命令将registry运行起来,如下:

  1. docker run -itd --name registry -p 5000:5000 2e2f252f3c88

运行成功后,我们就可以将自己的镜像提交到registry上了,如下:

这里需要注意的是,本地镜像的命名按照 registryHost:registryPort/imageName:tag 的格式命名。

容器运行在宿主机上,如果外网能够访问容器,才能够使用它提供的服务。本文就来了解下容器中的网络知识。

暴露网络端口

在前面的文章中,我们已经有用过暴露网络端口相关的命令了,即 -p 参数,实际上,Docker 中涉及暴露网络端口的参数有两个,分别是 -p-P 。下面分别来介绍。

  • -P

使用 -P,Docker 会在宿主机上随机为应用分配一个未被使用的端口,并将其映射到容器开放的端口,以 Nginx 为例,如下:

可以看到,Docker 为应用分配了一个随机端口 32768 ,使用该端口即可访问容器中的 nginx(http://lcalhost:32768)。

  • -p

-p 参数则有几种不同的用法:

  • hostPort:containerPort

这种用法是将宿主机端口和容器端口绑定起来,如下用法:

如上命令表示将宿主机的80端口映射到容器的80上,第一个 80 是宿主机的 80 ,第二个 80 是容器的 80 。

  • ip:hostPort:containerPort

这种是将指定的 ip 地址的端口和容器的端口进行映射。如下:

将 192.168.0.195 地址的80端口映射到容器的80端口上。

  • ip::containerPort

这种是将指定 ip 地址的随机端口映射到容器的开放端口上,如下:

 

总结

本文主要向大家介绍了 DockerHub 和容器网络,DockerHub 是我们容器的集散中心,网络则使我们的容器有办法对外提供服务。

参考资料:

[1] 曾金龙,肖新华,刘清.Docker开发实践[M].北京:人民邮电出版社,2015.

Java 极客技术公众号,是由一群热爱 Java 开发的技术人组建成立,专注分享原创、高质量的 Java 文章。如果您觉得我们的文章还不错,请帮忙赞赏、在看、转发支持,鼓励我们分享出更好的文章。

 

推荐阅读

面试系列

 

面试你应该知道的 MySQL 的锁

聊聊面试中的 Java 线程池

一个经典面试题:如何保证缓存与数据库的双写一致性?

今天来聊Java ClassLoader

面试中的最常被问到的两种锁

面试必备的分布式事物方案

 

Docker 系列

Docker 入门及安装[Docker 系列-1]

Docker 容器基本操作[Docker 系列-2]

Docker 容器高级操作[Docker 系列-3]

Docker 镜像基本操作[Docker 系列-4]

 

Guess you like

Origin www.cnblogs.com/justdojava/p/11271349.html