Connecting Maria and Application containers using a hostname

System: Debian Stretch, Linux gekkoslovakia 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1+deb9u1 (2018-05-07) x86_64 GNU/Linux
Docker: Docker version 18.03.1-ce, build 9ee9f40

I need to connect two Docker containers: mariadb and our application. Currently my database does not allow a user to access it using a domain name while a hardcoded IP works.

I create the two containers like so:


FROM alpine:latest
RUN apk update && apk upgrade && apk add shadow mariadb-client nodejs
RUN useradd --create-home -s /bin/sh ouruser
RUN mkdir -p /home/ouruser/application/private/app_server
COPY private/app_server /home/ouruser/application/private/app_server/
COPY private/private_config.json /home/ouruser/application/private/
RUN chown -R ouruser:ouruser /home/ouruser
RUN chmod -R 750 /home/ouruser
USER ouruser
CMD ["/usr/bin/node", "/home/ouruser/application/private/app_server/main.js"]


FROM mariadb:latest
ADD sql/ /docker-entrypoint-initdb.d
CMD ["mysqld"]

In the sql dir I have files to create the database and create users to access it. The user file:

USE test_db;

CREATE USER 'user_test'@'app_server' IDENTIFIED BY 'xxx';
CREATE USER 'user_test_read'@'app_server' IDENTIFIED BY 'xxx';

GRANT ALL PRIVILEGES ON test_db.* TO 'user_test'@'app_server';
GRANT SELECT, SHOW VIEW ON test_db.* TO 'user_test_read'@'app_server';


Here I use the hostname “app_server” as the user’s domain, which I understand comes from Docker custom bridge DNS as per

In my application I have configured the database to connect to the hostname “maria”. Here’s how I start the containers:

# Always cleared for now, not needed when everything works
docker stop maria && docker rm maria
docker stop ws && docker rm ws
docker volume rm maria-vol
docker network rm my_network

docker network create my_network
docker volume create maria-vol

# in maria container dir
docker build --rm -t maria .

# in app container dir
docker build --rm -t ws .

docker run --name maria --hostname maria --network my_network --publish 3306:3306 --mount source=maria-vol,target=/var/lib/mysql maria
docker run --network my_network --hostname app_server --name ws ws

In my maria db container I get:

2018-06-11  7:41:24 8 [Warning] Access denied for user 'user_test'@'' (using password: YES)

If I manually specify IPs for each container and set up hardcoded IPs in all my files, everything works. However, I’d prefer dynamic IPs with hostnames as it’s possible a particular IP is in use. This is also necessary for more complex container interaction later.