Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

440
Visualizações
Best practice for rails docker-compose db:create db:migrate

I have simple rails application with docker-compose.yml file. It consists from two containers - db container with PostgreSQL and web container with rails app.

In dockerfile for web part I have such lines in CMD

CMD RAILS_ENV=production rake db:create db:migrate && \
    bundle exec rails s -p 3000 -b '0.0.0.0' --environment=production

So in line rake db:create db:migrate I create db if it is a first run of db container, and run migrate.

But if it is only update of web part - I need only to run db:migrate, and db:create (as it should) give me error

ERROR:  database "myapp_production" already exists
STATEMENT:  CREATE DATABASE "myapp_production" ENCODING = 'unicode'

Everything working fine, but I think there is a better way.
What is a best way to handle this situation?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I have the same development stack and here is that I'm doing.

Here is a Dockerfile for postgres which I'm extend:

FROM postgres:9.4

ADD db/init.sql /docker-entrypoint-initdb.d/

EXPOSE 5432
CMD ["postgres"]

From the docker postgres documentation:

If you would like to do additional initialization in an image derived from this one, add one or more *.sql or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files and source any *.sh scripts found in that directory to do further initialization before starting the service.

My init.sql:

CREATE USER database_user;
CREATE DATABASE database_production;
GRANT ALL PRIVILEGES ON DATABASE database_production TO database_user;

After that my RUN command in the web container points to the run.sh script:

#!/usr/bin/env bash

echo "Bundling gems"
bundle install --jobs 8 --retry 3

echo "Clearing logs"
bin/rake log:clear

echo "Run migrations"
bundle exec rake db:migrate

echo "Seed database"
bundle exec rake db:seed

echo "Removing contents of tmp dirs"
bin/rake tmp:clear

echo "Starting app server ..."
bundle exec rails s -p 3000 -b '0.0.0.0'

That's it. My database created in the db container, and web app only does migration.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda