3
votes

I've a Docker composer file similar to:

version: '2'
services:
    db:
        image: mariadb:10.1
        volumes:
            - "./.data/db:/var/lib/mysql"
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: test
            MYSQL_DATABASE: test
            MYSQL_USER: test
            MYSQL_PASSWORD: test
    test:
        depends_on:
            - db
        links:
            - db:db
        build:
            context: .
            args:
                MYSQL_HOST: db
                MYSQL_DATABASE: test
                MYSQL_USER: test
                MYSQL_PASSWORD: test
        ports:
            - "8000:80"
        restart: always

Inside test container Dockerfile

FROM ...

...
ARG MYSQL_HOST 127.0.0.1

RUN set -x; echo $MYSQL_HOST

RUN script ... --param $MYSQL_HOST

However MYSQL_HOST variable (which I would expect to be the internal IP of the other container) is not being translated into the other container name.

How could it be done? Can it be achieved in another way with docker-compose?

1
What value does MYSQL_HOST contain?ppeterka
MYSQL_HOST should refer to the hostname/IP of the other container...Toni Hermoso Pulido
Ok, but what does it contain now, with your current composer file?ppeterka
You can see above: <pre> ... args: MYSQL_HOST: db </pre>Toni Hermoso Pulido

1 Answers

3
votes

I don't think you want a build argument here. Would probably set MYSQL_HOST as an environment variable. Inside of docker-compose, the literal string db is actually resolvable to your db container.

Typically, compose files look like this:

test:
   links:
     db:db
   environment:
     - MYSQL_HOST=db

Then in your test code do something like:

...
String dbHost = System.getEnv("MYSQL_HOST");
...

That way, the MySql host doesn't need to be built into your image removing the need for your image to be rebuilt all the time.