postgresql - Docker compose linking appears to not work -
i'm using docker compose run elixir/phoenix app in development. setup pretty standard, postgres container , web container.
however, i'm having hard time getting web container talk database container.
here 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 docker-compose.yml:
db: image: postgres web: build: . command: mix phoenix.server volumes: - .:/src/blog ports: - "4000:4000" links: - db when run docker-compose up, things appear work okay. however, when try run (to create database):
$ docker run blogphoenix_web mix ecto.create i following error:
** (mix) database blog.repo couldn't created, reason given: psql: not translate host name "db" address: name or service not known
then, if inspect hosts file of web container with:
$ docker run blogphoenix_web cat /etc/hosts ... output:
127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.4 a86e4f02ea56 isn't docker compose supposed create hostname entry db container?
here relevant version numbers docker tooling:
$ docker-machine --version #=> docker-machine version 0.6.0, build e27fb87 $ docker-compose --version #=> docker-compose version 1.6.0, build unknown $ docker --version #=> docker version 1.10.0, build 590d510 edit
okay, noticed may else reading this. command, docker run blogphoenix_web cat /etc/hosts enters new container, whereas command docker exec 845f9d69cb1e cat /etc/hosts enters running container. 845f9d69cb1e container id running version of blogphoenix_web image.
$ docker ps container id image command created status ports names 845f9d69cb1e blogphoenix_web "mix phoenix.server" hour ago 2 minutes 0.0.0.0:4000->4000/tcp blogphoenix_web_1 21a6f48dfc3b postgres "/docker-entrypoint.s" hour ago 2 minutes 5432/tcp blogphoenix_db_1 running exec command expected output hosts file, showing appropriate hostname linkage db container:
$ docker exec 845f9d69cb1e cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.2 db_1 21a6f48dfc3b blogphoenix_db_1 172.17.0.2 blogphoenix_db_1 21a6f48dfc3b 172.17.0.2 db 21a6f48dfc3b blogphoenix_db_1 172.17.0.3 845f9d69cb1e in other words, when ran docker run blogphoenix_web mix ecto.create command, executing mix ecto.create in new container based on blogphoenix_web image. new container wasn't started docker-compose , did not have appropriate host file linkage db container setup.
you need run using docker-compose:
docker-compose run web mix ecto.create docker compose creates linked containers, images not linked. means blogphoenix_web not linked blogphoenix_db, when run
docker-compose the newly created containers "blogphoenix_web_1" , "blogphoenix_db_1" linked together.
Comments
Post a Comment