I have a Dockerfile
and docker-compose.yml
like in the tutorial except I'm starting with an existing app.
My docker-compose.yml
looks like:
db: image: postgres ports: - "5432" web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/myapp ports: - "3030:3030" links: - db
and Dockerfile
:
FROM ruby:2.1.4 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs RUN mkdir /myapp WORKDIR /myapp ADD Gemfile /myapp/Gemfile RUN bundle install ADD . /myapp # RUN bundle exec rake db:create # RUN bundle exec rake db:migrate # RUN bundle exec rake db:seed
and database.yml
development: adapter: postgresql encoding: utf8 database: myapp_development host: db pool: 5 username: postgres password:
As you can see, I commented RUN bundle exec rake db:create
because I was receiving an error:
could not translate host name "db" to address: Name or service not known /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `initialize' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `new' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `connect' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:324:in `initialize' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout' /usr/local/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection' /usr/local/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/railties/databases.rake:144:in `rescue in create_database' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/railties/databases.rake:85:in `create_database' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/railties/databases.rake:62:in `block (3 levels) in <top (required)>' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/railties/databases.rake:62:in `each' /usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/railties/databases.rake:62:in `block (2 levels) in <top (required)>' /usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:240:in `call' /usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute' /usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:235:in `each' /usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute' /usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain' /usr/local/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize' /usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain' /usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke' /usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task' /usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level' /usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:106:in `each' /usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level' /usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads' /usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level' /usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run' /usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling' /usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:75:in `run' /usr/local/bundle/gems/rake-10.4.2/bin/rake:33:in `<top (required)>' /usr/local/bundle/bin/rake:16:in `load' /usr/local/bundle/bin/rake:16:in `<main>' Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8", "database"=>"myapp_development", "host"=>"db", "pool"=>5, "username"=>"postgres", "password"=>nil}
Instead I had to docker-compose build
then docker-compose run web rake db:create
etc.
Why can't I have the db-creation/migration in the Dockerfile. It would be so much clearner. Can I achieve that?
A Dockerfile is a simple text file that contains the commands a user could call to assemble an image whereas Docker Compose is a tool for defining and running multi-container Docker applications. Docker Compose define the services that make up your app in docker-compose.
Multiple containers can mount the same volume. Running docker-compose up will create a volume named <project_name>_html_files if it doesn't already exist . Then run docker volume ls to list the two volumes created, starting with the project name.
In your terminal, change your directory to the docker-rails directory and run the following command: >_ docker build -t rubyapp . To create a container from this image, run the command docker run -p 3000:3000 rubyapp . This creates a container and binds port 3000 of your host computer to port 3000 of the container.
When your web
image is built (following the instructions of the Dockerfile
), it doesn't have a connection to a db
container.
The webserver and database images are independents and the containers are linked when you launch them (following the definitions of the docker-compose.yml
file).
You cannot link to a container during the image build because it would break the principle that an image build must be reproducible. Similarly, you cannot mount a volume from the host machine during an image build neither.
The docker-compose run web rake db:create
command you used is a correct way to initalize the database.
Alternatively, you could launch the containers normaly with docker-compose
, and then use the docker exec
command to execute rake db:create
in the web
container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With