I'm trying to understand how the containers talk to each other with Docker Compose.
I'm working in a VM with no domain name, so I use IP address:port.
I want a simple WordPress app using the standard images : wordpress:php5.6-fpm-alpine
, mariadb
, nginx
. Next to that, I want a phpMyAdmin container with its own docker-compose.yml
, linking to the MariaDB container.
short story
I managed to get it to work with links
.
Then I tried to setup, but I got an error saying they are not on the same network. I know I could probably have gone with --link
in command-line, but that's no fun.
So I figured that's what the networks
directives are for, and I rewrote my docker-compose.yml
.
Here's the working docker-compose.yml
for my WordPress app (only the relevant stuff)
services:
blog:
(skip)
networks:
- maria_net
- app_net
db:
(skip)
networks:
- maria_net
web:
(skip)
networks:
- app_net
networks:
app_net:
maria_net:
The app exposes port 8080, where nginx is listening.
I need to tell nginx to proxy php requests to the blog
container where php-fpm
is listening on port 9000.
The conf files for nginx are pushed in the container's /etc/nginx/conf.d
.
What it does :
- sets an upstream pointing to port 9000 of the
blog
container (php-fpm
), - sets
root
for static files on /var/www/html, which is a volume shared with theblog
container - proxies requests on php files to the upstream
sample 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;
}
}
With this setup, Docker takes care of making containers on the same net visible to each other under their service name, and when I hit my VM's IP on port 8080, I get to the WordPress install just fine.
phpmyadmin
I now try to connect a phpMyAdmin container to 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 actually listens on @8081, but it says it can't connect to host db
.
Which is quite normal, as I found out :
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 and MariaDB each have their mariadb_net
…
I RTFM again, and found that from within Docker Compose, you have to specify that you want to connect to an existing network with external
(the network's actual name as listed by docker network ls
):
networks:
maria_net:
external:
name: blog_maria_net
Now the phpmyadmin and mariadb containers are on the same network :
$ 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"
}
}
]
And the mariadb container is aliased db
as per its service name :
$ 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"
}
}
But phpMyAdmin still can't connect to host db :
MySQL a répondu : Documentation
#2005 - Unknown MySQL server host ''db'' (-2)
The double quote might be the problem…
Best Answer
Actually it was the quote : I set the environment variable for the db with quotes, that are escaped by phpmyadmin, so instead of connecting to host
db
it tries to connect to'db'
:It should be :
That was a pretty lame problem, but the whole thing about networks in Compose isn't too clear, and I hope that finally getting a working conf will help others.