Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link doesn't work in Dockerfile for rake #2324

Closed
dazorni opened this issue Nov 4, 2015 · 3 comments
Closed

Link doesn't work in Dockerfile for rake #2324

dazorni opened this issue Nov 4, 2015 · 3 comments

Comments

@dazorni
Copy link

dazorni commented Nov 4, 2015

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?

@dazorni dazorni changed the title Link doesn't work in Dockerfile Link doesn't work in Dockerfile for rake Nov 4, 2015
@dnephin
Copy link

dnephin commented Nov 4, 2015

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.

@dazorni
Copy link
Author

dazorni commented Nov 4, 2015

Okay cool! 👍 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 dazorni closed this as completed Nov 4, 2015
@thinhlvv
Copy link

@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 :) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants