与this线程相关,我正在尝试创建2个容器:1个带有rails应用程序,另一个带有MySQL数据库,但我一直在我的应用程序production.log文件中获取Mysql2::Error (Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
在我点击容器的IP http://192.168.59.103
当我启动rails容器时,我试图链接它们,如果指定了错误的MySQL名称,则会出错。成功链接容器以使整个应用程序在容器中运行时我缺少什么?
Rails容器命令
docker run --name games-app --link test-mysql:mysql -p 8080 -d -e SECRET_KEY_BASE=test sample_rails_games_app
Here are my files:
Dockerfile
# Publish port 8080
EXPOSE 8080
CMD ["bundle", "exec","unicorn", "-p", "8080"]
CMD ["bunde", "exec", "rake", "db:migrate"]
Rails database.yml(开发和测试与生产相同)
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: root
host: localhost
#socket: /tmp/mysql.sock
production:
<<: *default
database: weblog_production
7/31/15编辑
docker日志显示运行的unicorn服务器:
docker logs a13bf7851c6d
I, [2015-07-31T18:10:59.860203 #1] INFO -- : listening on addr=0.0.0.0:8080 fd=9
I, [2015-07-31T18:10:59.860583 #1] INFO -- : worker=0 spawning...
I, [2015-07-31T18:10:59.864143 #1] INFO -- : master process ready
I, [2015-07-31T18:10:59.864859 #7] INFO -- : worker=0 spawned pid=7
I, [2015-07-31T18:10:59.865097 #7] INFO -- : Refreshing Gem list
I, [2015-07-31T18:11:01.796690 #7] INFO -- : worker=0 ready
7/31/15解决方案感谢@Rico
db:migrate
遇到问题,所以我最终在docker run
命令中手动运行它。确保在创建容器后或在创建过程中执行此操作,因为它需要链接到数据库容器docker run --rm --name <unique-value> --link <db-name> <non-db-image> env
的名称。 docker inspect -f "{{ .HostConfig.Links }}" <app-name>
答案 0 :(得分:2)
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'] %>
答案 1 :(得分:1)
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