Ali cloud deployment Docker (7) ---- the container connection

label Ali cloud DockerDocker container connectionDockerDocker deployment
4529 people read comment(0) Collection report
Classification:

Time tries all. okay Should adhere to adhere to.

Today see a thunder younger brother Shi to Ali, bless him, ah, although I have always been people holding called Daniel, maybe I pass the interview not family, hey, the heart is a shame.

This article is my original, reproduced please indicate the source:Http://prog3.com/sbdm/blog/minimicall

We have deployed Docker Service Series in Ali cloud tutorial has been to the seventh section,

Need to review students can look at my blog.

Today, we learn how to connect the docker inside the container. For example, I am a web service, I need to use MySQL services, if they belong to a different container, if the connection. This is the problem that we have to solve in this class.

The first step of the connection is to name our container

Container name

The container is named in the Run option which is --name specific as follows:

Docker run -d -P root@iZ28ikebrg6Z:~# --name web training/webapp Python app.py
Ca9d0b6245e0451e911ac03ef3d2b7748120a55d29c2d8b3cc9d9cd6e4ad0148
PS -l root@iZ28ikebrg6Z:~#
S UID PID PPID F C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 011224112100800 - 5601 pts/1 00:00:00 bash wait
0 R 011907112240800 - 2121 - 00:00:00 PS pts/1
Docker PS -l root@iZ28ikebrg6Z:~#
ID IMAGE COMMAND CREATED STATUS PORTS NAMES CONTAINER
Training/webapp:latest Python "app.py ca9d0b6245e0" 15 ago Up seconds 14 0.0.0.0:49153->5000/tcp web seconds
Root@iZ28ikebrg6Z:~#
We can have a detailed understanding of the bottom layer information of this container:

Docker inspect --help root@iZ28ikebrg6Z:~#

Docker inspect CONTAINER|IMAGE [CONTAINER|IMAGE Usage:...]

Low-level information on a container or image Return

-f, --format= "" the output using the given go template. Format
Docker inspect -f {{.Name}} "root@iZ28ikebrg6Z:~#" CA9
/web
Root@iZ28ikebrg6Z:~#
It should be noted that the name of the container needs to be unique. There is no conflict. If you want to stop the container to give up the name, then you can run the time to add the option --rm


Container connection

After the container is connected, it can exchange data with each other, such as a web container, which can be connected to a container that contains a database, and then the data container provides data storage.

The connection is name:alias --link where name is the container that we want to connect, such as a database container mysql, and alias is the name of the connection.

We first execute the order, and then explain:

Docker stop web root@iZ28ikebrg6Z:~#
Web
Docker images root@iZ28ikebrg6Z:~#
TAG IMAGE ID CREATED VIRTUAL REPOSITORY SIZE
Latest f0bec095f291 sameersbn/redmine 6 ago hours 614.6 MB
Latest bf5c375d9057 sameersbn/gitlab 3 ago days 635.1 MB
Latest 24a6064fa4cd sameersbn/postgresql 11 ago days 142.1 MB
Latest 31fa814ba25a training/webapp 4 ago months 278.8 MB
2.4.2 b95b8046d47c sameersbn/redmine 8 ago months 1.327 GB
Docker run -d --name DB root@iZ28ikebrg6Z:~# sameersbn/postgresql
A9dbca9857fddbe366ce76909943e856eecc12df27886830e43fe5c91a53abc7
Docker PS root@iZ28ikebrg6Z:~#
容器标识图像命令创建状态端口名称
a9dbca9857fd sameersbn / PostgreSQL:最新的“开始”前5秒3秒5432 / TCP分贝
根@ iz28ikebrg6z:~ # Docker RM F网络
Web
根@ iz28ikebrg6z:~ # Docker运行- D - P -名称网络链接数据库:数据库培训/开源的Python app.py
ff990b53706d0a1277c1ba37b274c397ba906ba8ea28e9d9b57e78f84ef12d93
根@ iz28ikebrg6z:~ # Docker PS——NO TRUNC | awk { print $ NF }”
名称
Web
分贝,网络/分贝
根@ iz28ikebrg6z:~ #

我们连接的是sameersbn / PostgreSQL这也是个数据库,是我不小心下载下来的,没有MySQL。拿着用吧,会出错再说。

好,连是连接起来了,可是如何交互数据呢?因为到现在位置我们只是看到多了个-链接选项而已,假如我要使用数据库,我总得增删改查啊。

码头工人提供两种方式,一种是环境变量,一种是改/etc/hosts

环境变量

当一个连接产生之后,码头工人首先会为每个连接产生一个环境变量<别名> _name,例如刚才的连接-链接数据库:数据库、则有

db_name = /网络/数据库
还有,一些源容器暴露的端口。这部分我直接贴官网的说明,或许更能够讲清楚,大家自行理解。


<名称> <口> _port_ _ <协议>将包含一个URL引用到港口。在<名称>是别名的链路参数指定(例如WebDB),<港>是端口号被暴露,和<<协议>>是TCP或UDP。网址的格式将 是:<协议>:// < container_ip_address >:<口>(如TCP / 172.17.0.82:8080)。这个URL将被分成以下3个环境变量的使用:
<名称> <口> _port_ _ <协议> _addr将包含从URL的IP地址(例如webdb_port_8080_tcp_addr = 172.17.0.82)。
<名称> <口> _port_ _ <协议> _port将包含从URL的端口号(如webdb_port_8080_tcp_port = 8080)。
<名称> <口> _port_ _ <协议> _proto将包含从URL的协议(如TCP webdb_port_8080_tcp_proto =)。

设置环境变量的方法为:

$ sudo Docker运行- RM的名字--链接数据库:数据库技术培训/应用环境
。..
db_name = /技术/分贝
db_port = TCP / 172.17.0.5:5432
db_port_5432_tcp = TCP / 172.17.0.5:5432
db_port_5432_tcp_proto = TCP
db_port_5432_tcp_port = 5432
db_port_5432_tcp_addr = 172.17.0.5

还有另一种方法是更改目标容器中的主机文件,在这个文件中加入源容器

/等/主机


具体如下:

$ sudo Docker运行T -我- RM -链接数据库:数据库培训/ webapp /bin/bash
根@ aed84ee21bde:/选择/ webapp #猫/etc/hosts
172.17.0.7 aed84ee21bde
。..
172.17.0.5分贝
可以实验一下是否能够联通

根@ aed84ee21bde:/选择/ webapp # apt-get install -野球拳inetutils平
根@ aed84ee21bde:/选择/ webapp #平分贝
平dB(172.17.0.5):48个字节的数据
56字节从172.17.0.5:icmp_seq = 0 = 64 = 0.267 ms时间TTL
56字节从172.17.0.5:icmp_seq = 1 = 64 = 0.250 ms时间TTL
56字节从172.17.0.5:icmp_seq = 2 = 64 = 0.256 ms时间TTL
就算源容器重启,它也会自动更新

$ sudo泊坞窗重启数据库
根@ aed84ee21bde:/选择/ webapp #猫/etc/hosts
172.17.0.7 aed84ee21bde
。..
172.17.0.9分贝


好。容器的连接就到这里。到实战的时候记得有这么回事就可以。




猜你在找
查看评论
*以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:177354次
    • 积分:三千六百零六
    • 等级:
    • 排名:5115名第
    • 原创:169篇
    • 转载:48篇
    • 译文:0篇
    • 评论:125条
    文章分类
    Latest comments