Elliot Larson February 2016

Docker compose linking appears to not work

I'm using Docker Compose to run an Elixir/Phoenix app in development. The setup is pretty standard, with a postgres container and a web container.

However, I'm having a hard time getting the web container to talk to the database container.

Here is my web container Dockerfile:

FROM ubuntu:14.04
MAINTAINER me@example.com

RUN locale-gen en_US.UTF-8  
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8 

ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update
RUN apt-get install -y wget
RUN apt-get install -y curl
RUN apt-get install -y inotify-tools
RUN apt-get install -y postgresql-client
RUN wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb \
    && dpkg -i erlang-solutions_1.0_all.deb
RUN curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
RUN apt-get install -y nodejs
RUN apt-get update
RUN apt-get install -y esl-erlang 
RUN apt-get install -y elixir
RUN mix local.rebar
RUN mix local.hex --force

ADD . src/blog/
WORKDIR src/blog/

RUN mix deps.get
RUN mix deps.compile

Here's my docker-compose.yml:

db:
  image: postgres
web:
  build: .
  command: mix phoenix.server
  volumes:
    - .:/src/blog
  ports:
    - "4000:4000"
  links:
    - db

When I run docker-compose up, things appear to work okay. However, when I try to run (to create the database):

$ docker run blogphoenix_web mix ecto.create

I get the following error:

** (Mix) The database for Blog.Repo couldn't be created, reason given: psql: could not translate host name "db" to address: Name or service not known

Then, if I inspect the hosts file of the web container with:

$ docker run blogphoenix_web cat /etc/hosts

... I get this output:

127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loop        

Answers


TopperH February 2016

You need to run it using docker-compose:

docker-compose run web mix ecto.create

Docker compose creates linked containers, but the images themselves are not linked. This means that blogphoenix_web is not linked to blogphoenix_db, but when you will run

docker-compose up

the newly created containers "blogphoenix_web_1" and "blogphoenix_db_1" will be linked together.

Post Status

Asked in February 2016
Viewed 2,335 times
Voted 8
Answered 1 times

Search




Leave an answer