在Docker Compose文件中使用networking

我试图了解容器如何与Docker Compose进行对话。

我在没有域名的虚拟机上工作,所以我使用IP地址:端口。

我想要一个简单的WordPress应用程序使用标准的图像: wordpress:php5.6-fpm-alpinemariadbnginx 。 接下来,我想要一个带有自己docker-compose.yml的phpMyAdmin容器,链接到MariaDB容器。

短篇故事

我设法让它与links工作。

然后我试图设置,但我得到一个错误,说他们不在同一个networking上。 我知道我可能已经走了--link在命令行链接,但这并不好玩。

所以我想这就是networks指令是什么,我重写了我docker-compose.yml

这里是我的Wordpress应用程序的工作docker-compose.yml (只有相关的东西)

 services: blog: (skip) networks: - maria_net - app_net db: (skip) networks: - maria_net web: (skip) networks: - app_net networks: app_net: maria_net: 

该应用程序公开端口8080,nginx正在侦听。

我需要告诉nginx将PHP请求代理到php-fpm正在监听端口9000的blog容器。

nginx的conf文件被压入容器的/etc/nginx/conf.d

它能做什么 :

  • 设置指向blog容器的端口9000( php-fpm )的上游,
  • 为/ var / www / html(这是与blog容器共享的卷)上的静态文件设置root
  • 代理请求上的PHP文件

样本nginx/conf.d/default.conf

 upstream backend { server blog:9000; } server { listen 80; root /var/www/html; index index.php; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; (...more fastcgi_param...) fastcgi_pass backend; } } 

通过这个设置,Docker负责在同一个networking上使用他们的服务名称来显示彼此的容器,当我在端口8080上打开虚拟机的IP时,我就可以很好的安装Wordpress了。

phpMyAdmin的

我现在尝试将一个phpMyAdmin容器连接到MariaDB。

docker-compose.yml

 version: '3' services: phpmyadmin: image: phpmyadmin/phpmyadmin networks: - maria_net environment: - PMA_ARBITRARY=1 restart: always ports: - 8081:80 volumes: - /sessions environment: - PMA_HOST='db' - PMA_USER=root - PMA_PASSWORD=azerty networks: maria_net: 

phpMyAdmin实际上在@ 8081监听,但它说它不能连接到主机db

这是很正常的,因为我发现:

 user@host $ docker network ls NETWORK ID NAME DRIVER SCOPE 3fbb92bac6dc blog_app_net bridge local fa0ef0ee1a64 blog_maria_net bridge local 3b4cd3956119 bridge bridge local 820bc4c6c844 host host local 17b010e55091 none null local 79bcc3149270 phpmyadmin_maria_net bridge local 

phpMyAdmin和MariaDB每个都有他们的mariadb_net

我又一次发现了RTFM,发现在Docker Compose中,你必须指定你想要连接到一个external的networking(networking实际的名称由docker network ls ):

 networks: maria_net: external: name: blog_maria_net 

现在phpmyadmin和mariadb容器在同一个networking上:

 $ docker network inspect blog_maria_net [ { "Name": "blog_maria_net", "Id": "fa0ef0ee1a6479fba0ed60dc01480a5f2200ff1ab37de511f9a2e55caa238bb1", "Created": "2017-07-02T13:01:08.473871818+02:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.19.0.0/16", "Gateway": "172.19.0.1" } ] }, "Internal": false, "Attachable": true, "Ingress": false, "Containers": { "1b0241ed5609f541e60f23717d1cfd55ded4a272536ad417c33e57c573ffec72": { "Name": "blog_blog_1", "EndpointID": "9e4fd814799cce653299f0751af51482e9f40e15d73bc213af9f120bd7d0f143", "MacAddress": "02:42:ac:13:00:03", "IPv4Address": "172.19.0.3/16", "IPv6Address": "" }, "af6a790c667ba6576ebd056f8166a46d8da7cd1d2704a91e45b93d6d7c945e28": { "Name": "blog_db_1", "EndpointID": "a9e5d3489f8b156385d1c81c34b6b712708715803080f8b75651ba1bc8fdd039", "MacAddress": "02:42:ac:13:00:02", "IPv4Address": "172.19.0.2/16", "IPv6Address": "" }, "ccaf49d12d928a455c50be33e607e731bb1045f2afe9003c4d22a914af604923": { "Name": "phpmyadmin_phpmyadmin_1", "EndpointID": "8cfc3efa3654040fdaaa4ffa16643cfab838602d9eead3d14d40c0dd8fd45fe8", "MacAddress": "02:42:ac:13:00:04", "IPv4Address": "172.19.0.4/16", "IPv6Address": "" } }, "Options": {}, "Labels": { "com.docker.compose.network": "maria_net", "com.docker.compose.project": "blog" } } ] 

而mariadb容器是别名db根据其服务名称:

 $ docker inspect blog_db_1 (...) "Networks": { "blog_maria_net": { "IPAMConfig": null, "Links": null, "Aliases": [ "af6a790c667b", "db" ], "NetworkID": "fa0ef0ee1a6479fba0ed60dc01480a5f2200ff1ab37de511f9a2e55caa238bb1", "EndpointID": "a9e5d3489f8b156385d1c81c34b6b712708715803080f8b75651ba1bc8fdd039", "Gateway": "172.19.0.1", "IPAddress": "172.19.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:13:00:02" } } 

但是phpMyAdmin仍然无法连接到主机数据库:

 MySQL a répondu : Documentation #2005 - Unknown MySQL server host ''db'' (-2) 

双引号可能是问题…

其实这是引语:我设置数据库的引号的环境variables,由phpmyadmin转义,所以而不是连接到主机db它试图连接到'db'

 - PMA_HOST='db' 

它应该是 :

 - PMA_HOST=db 

这是一个相当蹩脚的问题,但是关于Composenetworking的整个事情并不是很清楚,我希望最终得到一个有效的conf可以帮助其他人。