Compose: Link doesn't work in Dockerfile for rake

Created on 4 Nov 2015  路  3Comments  路  Source: docker/compose

Currently i'm using docker compose to connect my web app with the postgre database. My config looks like this:

# Dockerfile

FROM rails:onbuild
RUN rake db:create db:migrate db:seed
# config/database.yml

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
    adapter: postgresql
    encoding: utf8
    database: rezeptr_prod
    pool: 5
    username: postgres
    password:
    host: db

development:
  <<: *default
  database: rezeptr_dev

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: rezeptr_test
# docker-compose.yml

db:
  image: postgres
web:
  build: .
  volumes:
    - .:/usr/src/app
  ports:
    - "3000:3000"
  links:
    - db:db

If i run docker-compose up i get this error

could not translate host name "db" to address: Name or service not known

If i don't run rake db:create ... in the Dockerfile and run it like this docker-compose run web rake db:create it works fine and i don't get any errors. Can you help me?

kinquestion

Most helpful comment

Yes, links are not available during the build phase. There are a few ways to get your fixtures installed during build, but none of them are really straightforward:

  • you can create a new Dockerfile for the database, add all the project source, and ruby dependencies, and run the rake db:create as part of the image build
  • you can run the rake db:create as part of the entrypoint script of a database image (something I'm not a fan of, and don't really recommend)
  • you can exclude it from the build, and make it a manual step in setting up the environment (also not a fan, but it's the easiest option, and what we use in the example docs http://docs.docker.com/compose/rails/)
  • make a multi-step build that starts a temporary environment, which you can use to create a sql dump of the data, and include that in the database image. There isn't great tooling for that yet, but I'm working on something in https://github.com/dnephin/buildpipe. There are no docs for it yet, so you probably don't want to use it.

All 3 comments

Yes, links are not available during the build phase. There are a few ways to get your fixtures installed during build, but none of them are really straightforward:

  • you can create a new Dockerfile for the database, add all the project source, and ruby dependencies, and run the rake db:create as part of the image build
  • you can run the rake db:create as part of the entrypoint script of a database image (something I'm not a fan of, and don't really recommend)
  • you can exclude it from the build, and make it a manual step in setting up the environment (also not a fan, but it's the easiest option, and what we use in the example docs http://docs.docker.com/compose/rails/)
  • make a multi-step build that starts a temporary environment, which you can use to create a sql dump of the data, and include that in the database image. There isn't great tooling for that yet, but I'm working on something in https://github.com/dnephin/buildpipe. There are no docs for it yet, so you probably don't want to use it.

Okay cool! :+1: First, thank you for your long description! I will run it as a manual step, this should be enough for now. Later i could think about further steps!

@dazorni Could you please update the steps to solve. I got into this and try to discover. I know it's difficult to reproduce, but really appreciate if you can show :) .

Was this page helpful?
0 / 5 - 0 ratings