Docker-容器互联访问之数据容器操作过程

简介:

要通过一个容器A来访问另一个容器B,其中容器B部署数据库,容器A部署应用程序。在Docker生态中我们称之为容器互联。


  下面通过一步一步执行,来实现此需求。

 

  1. 获取postgres镜像


1
2
3
4
5
6
7
ubuntu@ip-10-23-28-180:~$  sudo  docker pull daocloud.io /library/postgres :latest
Pulling repository daocloud.io /library/postgres
a7d662bede59: Download complete 
2c49f83e0b13: Download complete 
4a5e6db8c069: Download complete 
...
Status: Downloaded newer image  for  daocloud.io /library/postgres :latest

 注:a.为了加快下载速度使用daocloud.io提供的服务;b. ... 表示输出内容部分省略


 这里使用了postgres的最新版本,建议在开发,测试,生产过程中最好指定明确的版本,这样就可以在任一时刻清楚此时使用的镜像(image)版本,比如:ubuntu:14.04


2. 查看本地镜像

 

1
2
3
ubuntu@ip-10-23-28-180:~$  sudo  docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
daocloud.io /library/postgres    latest              a7d662bede59        6 days ago          265.3 MB


 镜像库:daocloud.io/library/postgres是来自daocloud registry的,对应docker hub的官方postgres镜像库,tag是latest 。 


 由于没有其它tag的镜像了,下面使用镜像

 daocloud.io/library/postgres

 daocloud.io/postgres,

 daocloud.io/postgres:latest 

 等同daocloud.io/library/postgres:latest镜像。


3.通过镜像daocloud.io/postgres创建一个容器查看环境变量

  

1
2
3
4
5
6
7
8
9
10
ubuntu@ip-10-23-28-180:~$  sudo  docker run --name pg -- rm  daocloud.io /library/postgres  env
HOSTNAME=a741a4625fe8
PG_MAJOR=9.4
PATH= /usr/lib/postgresql/9 .4 /bin : /usr/local/sbin : /usr/local/bin : /usr/sbin : /usr/bin : /sbin : /bin
PWD=/
LANG=en_US.utf8
SHLVL=0
HOME= /root
PG_VERSION=9.4.4-1.pgdg80+1
PGDATA= /var/lib/postgresql/data


 容器名称(--name): pg, 运行完后删除。


4.创建一个容器命名命名为pg,并后台运行

  

1
2
3
4
5
6
ubuntu@ip-10-23-28-180:~$  sudo  docker run --name pg -d  daocloud.io /postgres
3e27983005a9872da5e60fa84aef5a00897585b55754baff26b756eb16ed7674
ubuntu@ip-10-23-28-180:~$  sudo  docker  ps  -a
CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS               NAMES
3e27983005a9        daocloud.io /postgres    " /docker-entrypoint .   13 seconds ago      Up 12 seconds       5432 /tcp             pg                  
ubuntu@ip-10-23-28-180:~$


5.创建一个容器,连接pg容器,运行env命令,查看环境变量信息

 

1
ubuntu@ip-10-23-28-180:~$  sudo  docker run -it --link pg:pglink -- rm   daocloud.io /library/postgres :latest  env

 输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PGLINK_PORT_5432_TCP_ADDR=172.17.0.3
HOSTNAME=c4c66c74ee25
PGLINK_ENV_PGDATA= /var/lib/postgresql/data
PGLINK_ENV_LANG=en_US.utf8
TERM=xterm
PG_MAJOR=9.4
PGLINK_ENV_PG_VERSION=9.4.4-1.pgdg80+1
PGLINK_ENV_PG_MAJOR=9.4
PGLINK_PORT=tcp: //172 .17.0.3:5432
PGLINK_PORT_5432_TCP_PROTO=tcp
PATH= /usr/lib/postgresql/9 .4 /bin : /usr/local/sbin : /usr/local/bin : /usr/sbin : /usr/bin : /sbin : /bin
PGLINK_PORT_5432_TCP_PORT=5432
PWD=/
PGLINK_NAME= /hungry_sammet/pglink
LANG=en_US.utf8
SHLVL=0
HOME= /root
PG_VERSION=9.4.4-1.pgdg80+1
PGLINK_PORT_5432_TCP=tcp: //172 .17.0.3:5432
PGDATA= /var/lib/postgresql/data

 通过输出内容可以得到连接pg容器中的postgres数据库信息


 注:参数 --link CONTAINER_NAME:alias CONTAINER_NAME是要连接的容器名(这个是创建容器时自动生成的或者是通过--name指定的),alias该连接的别名


6.创建一个容器,连接容器pg,执行psql连接postgres数据库,要用到5中输出的环境变量


1
2
3
4
5
ubuntu@ip-10-23-28-180:~$  sudo  docker run -it --link pg:pglink --name pgclient  daocloud.io /library/postgres :latest sh -c  'exec psql -h "$PGLINK_PORT_5432_TCP_ADDR" -p "$PGLINK_PORT_5432_TCP_PORT" -U postgres'
psql (9.4.4)
Type  "help"  for  help.
 
postgres= # \q

 

 注:5中创建postgres数据库容器没有指定密码和用户名,6中使用默认用户名postgres


7.查看容器

 

1
2
3
4
ubuntu@ip-10-23-28-180:~$  sudo  docker  ps  -a
CONTAINER ID        IMAGE                                 COMMAND                CREATED             STATUS                          PORTS               NAMES
10ff07bac1f3        daocloud.io /library/postgres :latest   " /docker-entrypoint .   2 minutes ago       Exited (0) About a minute ago                       pgclient            
3e27983005a9        daocloud.io /postgres                   " /docker-entrypoint .   15 minutes ago      Up 15 minutes                   5432 /tcp             pg


 现在有两个容器,pg容器还在运行中;pgclient容器处理退出状态,可以启动。


8. 下面重新操作4,5,6个步骤,为postgres容器指定用户名和密码,客户端容器要通过认证才能连接到数据库


 

1
2
ubuntu@ip-10-23-28-180:~$  sudo  docker run --name pg2 -d -e POSTGRES_PASSWORD=123 -e POSTGRES_USER=admin  daocloud.io /postgres
1373bae75b802587bbbe4a71e37ec5d69a1a8d28e715c95574e154c10bddce5d


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ubuntu@ip-10-23-28-180:~$  sudo  docker run -it --link pg2:pglink --name pgclient2  daocloud.io /library/postgres :latest  env
PGLINK_PORT_5432_TCP_ADDR=172.17.0.9
HOSTNAME=9dc0b31dee7d
PGLINK_ENV_PGDATA= /var/lib/postgresql/data
PGLINK_ENV_LANG=en_US.utf8
TERM=xterm
PG_MAJOR=9.4
PGLINK_ENV_PG_VERSION=9.4.4-1.pgdg80+1
PGLINK_ENV_PG_MAJOR=9.4
PGLINK_PORT=tcp: //172 .17.0.9:5432
PGLINK_PORT_5432_TCP_PROTO=tcp
PATH= /usr/lib/postgresql/9 .4 /bin : /usr/local/sbin : /usr/local/bin : /usr/sbin : /usr/bin : /sbin : /bin
PGLINK_PORT_5432_TCP_PORT=5432
PWD=/
PGLINK_NAME= /pgclient2/pglink
LANG=en_US.utf8
PGLINK_ENV_POSTGRES_USER=admin
SHLVL=0
HOME= /root
PGLINK_ENV_POSTGRES_PASSWORD=123
PG_VERSION=9.4.4-1.pgdg80+1
PGLINK_PORT_5432_TCP=tcp: //172 .17.0.9:5432
PGDATA= /var/lib/postgresql/data


 注意:PGLINK_ENV_POSTGRES_USER, PGLINK_ENV_POSTGRES_PASSWORD


 下面是正确的认证方式:

1
2
3
4
5
6
ubuntu@ip-10-23-28-180:~$  sudo  docker run -it --link pg2:pglink --name pgclient2  daocloud.io /library/postgres :latest sh -c  'exec psql -h "$PGLINK_PORT_5432_TCP_ADDR" -p "$PGLINK_PORT_5432_TCP_PORT" -U "$PGLINK_ENV_POSTGRES_USER" '
Password  for  user admin: 
psql (9.4.4)
Type  "help"  for  help.
 
admin= #


 下面是错误的认证方式:

1
2
3
ubuntu@ip-10-23-28-180:~$  sudo  docker run -it --link pg2:pglink --name pgclient2  daocloud.io /library/postgres :latest sh -c  'exec psql -h "$PGLINK_PORT_5432_TCP_ADDR" -p "$PGLINK_PORT_5432_TCP_PORT" -U "$PGLINK_ENV_POSTGRES_USER" -P "$PGLINK_ENV_POSTGRES_PASSWORD"'
\pset: unknown option: 123
psql: could not  set  printing parameter  "123"


9.使用busybox进行创建容器,并连接容器pg

 

 下载busybox镜像:

 

1
2
3
4
5
6
ubuntu@ip-10-23-28-180:~$  sudo  docker pull busybox
Pulling repository busybox
8c2e06607696: Download complete 
cf2616975b4a: Download complete 
6ce2e90b0bc7: Download complete 
Status: Downloaded newer image  for  busybox:latest

 

 创建容器,通过交互式方式ping pg


1
2
3
4
5
6
7
8
9
10
11
12
ubuntu@ip-10-23-28-180:~$  sudo  docker run -it --name bb --link pg:bblink  busybox
# ping -c 5 pg
PING pg (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3:  seq =0 ttl=64  time =0.132 ms
64 bytes from 172.17.0.3:  seq =1 ttl=64  time =0.050 ms
64 bytes from 172.17.0.3:  seq =2 ttl=64  time =0.079 ms
64 bytes from 172.17.0.3:  seq =3 ttl=64  time =0.053 ms
64 bytes from 172.17.0.3:  seq =4 ttl=64  time =0.082 ms
 
--- pg  ping  statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min /avg/max  = 0.050 /0 .079 /0 .132 ms


 容器bb和容器pg可以通信,并且可以直接使用容器名来作为hostname。 多个容器可以同时连接到一个容器上。



本文转自 secondriver 51CTO博客,原文链接:http://blog.51cto.com/aiilive/1690841,如需转载请自行联系原作者

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
2月前
|
NoSQL 算法 Redis
【Docker】(3)学习Docker中 镜像与容器数据卷、映射关系!手把手带你安装 MySql主从同步 和 Redis三主三从集群!并且进行主从切换与扩容操作,还有分析 哈希分区 等知识点!
Union文件系统(UnionFS)是一种**分层、轻量级并且高性能的文件系统**,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem) Union 文件系统是 Docker 镜像的基础。 镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
526 6
|
2月前
|
监控 Linux 调度
【赵渝强老师】Docker容器的资源管理机制
本文介绍了Linux CGroup技术及其在Docker资源管理中的应用。通过实例演示了如何利用CGroup限制应用程序的CPU、内存和I/O带宽使用,实现系统资源的精细化控制,帮助理解Docker底层资源限制机制。
261 6
|
网络协议 Java Maven
docker将数据从宿主机挂载到容器的方式(二)
docker将数据从宿主机挂载到容器的方式(二)
625 0
docker将数据从宿主机挂载到容器的方式(二)
|
存储 Docker 容器
docker将数据从宿主机挂载到容器的方式(一)
docker将数据从宿主机挂载到容器的方式(一)
523 0
docker将数据从宿主机挂载到容器的方式(一)
|
3月前
|
缓存 前端开发 Docker
Docker Layer Caching:加速你的容器构建
Docker Layer Caching:加速你的容器构建