3

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?

4
  • What value does MYSQL_HOST contain? Commented Sep 3, 2016 at 18:55
  • MYSQL_HOST should refer to the hostname/IP of the other container... Commented Sep 3, 2016 at 18:56
  • Ok, but what does it contain now, with your current composer file? Commented Sep 3, 2016 at 18:57
  • You can see above: <pre> ... args: MYSQL_HOST: db </pre> Commented Sep 3, 2016 at 19:01

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the suggestion. Sadly that code is in already existing application and it's actually used in the building process for generating a configuration file to be read by the running application (this: mediawiki.org/wiki/Manual:Install.php). It seems it must be with build args, because envs are ignored in the build process.
It sounds like your build depends on external network access to another container. By design, images should be able to be built on any docker hosts without external dependencies, and docker doesn't really support the method you require to gather dependencies. At best, you could have a pre-build step that gathers settings from external sources and injects those directly into the image build as args.
Thanks. You're right. I actually can do this with a Bash script command such as: "docker inspect --format '{{ .NetworkSettings.IPAddress }}' db` I will comment in docker-compose issues if this may be a possibility in the future...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.