Connect Rails/Unicorn/Nginx container to MySQL container

霸气de小男生 提交于 2019-12-05 21:48:30

Actually your bundle exec unicorn -p 8080 CMD is superseding the bundle exec rake db:migrate as it doesn't return.

You should run your db:migrate first and you should run it with the RUN command as CMD is the primary command in docker.

But the other problem is with your database.yml file. You are pointing your db to a db server that runs on the same container as in the application. You should populate the values of your database.yml from the env variables created after you link your source container (application) to destination container (db server container). The env variables are created in the source container.

More info here: https://docs.docker.com/userguide/dockerlinks/

So for example:

$ docker run --rm --name web2 --link db:db training/webapp env
. . .
DB_NAME=/web2/db
DB_PORT=tcp://172.17.0.5:5432
DB_PORT_5432_TCP=tcp://172.17.0.5:5432
DB_PORT_5432_TCP_PROTO=tcp
DB_PORT_5432_TCP_PORT=5432
DB_PORT_5432_TCP_ADDR=172.17.0.5

Your database.yml should look something like this:

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  database: <%= ENV['DB_NAME'] %>
  username: root
  password: root
  host: <%= ENV['DB_PORT_5432_TCP_ADDR'] %>
  port: <%= ENV['DB_PORT_5432_TCP_PORT'] %>

You can't have 2 CMD commands in your Dockerfile, in fact only the last one is kept. The CMD command executed is `

CMD ["bunde", "exec", "rake", "db:migrate"]`

the other, the

CMD ["bundle", "exec","unicorn", "-p", "8080"]

is superseded.

See Supervisor

https://docs.docker.com/articles/using_supervisord/

if you want to run more than one rocess in your container, or run 2 differnet containers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!