将Rails / Unicorn / Nginx容器连接到MySQL容器 [英] Connect Rails/Unicorn/Nginx container to MySQL container
问题描述
与此线程相关,我试图创建2个容器:1与rails应用程序,另一个与MySQL数据库,但我不断得到 Mysql2 ::错误(无法通过套接字'/ var / run / mysqld连接到本地MySQL服务器/mysqld.sock'
在我的应用程序production.log文件中,我打了容器的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
/ pre>
这是我的文件:
Dockerfile
code>#发布端口8080
EXPOSE 8080
CMD [bundle,exec,unicorn,-p,8080]
CMD [bunde,exec,rake,db:migrate]
strong> Rails database.yml(开发和测试与生产相同)
默认值:&默认
适配器:mysql2
编码:utf8
池:5
用户名:root
密码:root
主机:localhost
#socket:/ tmp /mysql.sock
生产:
<:*默认
数据库:weblog_production
7/31/15编辑
Docker日志显示运行的独角兽服务器: p>
docker log a13bf7851c6d
I,[2015-07-31T18:10:59.860203#1] INFO - :listen addr = 0.0.0.0:8080 fd = 9
I,[2015-07-31T18:10:59.860583#1] INFO - :worker = 0产卵...
I,[2015-07 -31T18:10:59.864143#1] INFO - :master process ready
I,[2015-07-31T18:10:59.86485 9#7] INFO - :worker = 0产生pid = 7
I,[2015-07-31T18:10:59.865097#7] INFO - :刷新宝石列表
I,[2015- 07-31T18:11:01.796690#7] INFO - :worker = 0 ready
7/31/15解决方案感谢@Rico
db:migrate
运行中出现问题,所以我最终在$ code> docker run 命令中手动运行它。确保在容器已创建之后或在创建过程中执行此操作,因为它需要链接到DB容器
- 此链接文章帮助我了解到我的链接没有被创建,所以没有办法正确的沟通。
- 一旦我明白如何准确地创建链接,我更新了我的database.yml与主机和端口值
- 使用此命令检查env变量的名称
docker run --rm --name< unique-value> --link< db-name> <非DB-图像> ENV
。
- 使用它来查看应用程序容器中的链接的值
docker inspect -f{{.HostConfig.Links}}< app-name>
解决方案其实你的
unicorn -p 8080
CMD
替代bundle exec rake db:migrate
,因为它不不要返回。
你应该首先运行你的
db:migrate
,你应该使用RUN
命令为CMD
是Docker中的主要命令。
但是另一个问题是您的
database.yml
文件。您将数据库指向与应用程序相同的容器上运行的数据库服务器。您应该在将源容器(应用程序)链接到目标容器(db服务器容器)之后创建的env变量中填充database.yml
的值。 env变量在源容器中创建。
更多信息: https://docs.docker.com/userguide/dockerlinks/
所以例如:
$ 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
您的
数据库.yml
应该如下所示:默认值:& default
适配器: mysql2
encoding:utf8
pool:5
数据库:<%= ENV ['DB_NAME']%>
用户名:root
密码:root
主机:<%= ENV ['DB_PORT_5432_TCP_ADDR']%>
port:<%= ENV ['DB_PORT_5432_TCP_PORT']%>
Related to this thread, I am trying to create 2 containers: 1 with a rails app, and the other with a MySQL database but I keep getting the
Mysql2::Error (Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
in my apps production.log file after I hit the container's IPhttp://192.168.59.103
When I start the rails container, I am attempting to link them and do get an error if I specify an incorrect MySQL name. What am I missing to successfully link the containers so the full app runs in containers?
Rails container command
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 (dev and test are the same as production)
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 Edit
The docker log shows the unicorn server running:
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 Solution Thanks to @Rico
db:migrate
was having problems running so I ultimately ran it by hand in adocker run
command. Make sure you do this after the container is already created, or during the creation process as it needs the linking to the DB container- This linking article helped me understand that my link was not being created so there was no way to communicate properly.
- Once I understood how to accurately make the link, I did update my database.yml with the host and port values
- Use this command to check the names of your env variables
docker run --rm --name <unique-value> --link <db-name> <non-db-image> env
.- Use this to see the value of the links in your app container
docker inspect -f "{{ .HostConfig.Links }}" <app-name>
解决方案Actually your
bundle exec unicorn -p 8080
CMD
is superseding thebundle exec rake db:migrate
as it doesn't return.You should run your
db:migrate
first and you should run it with theRUN
command asCMD
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 yourdatabase.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'] %>
这篇关于将Rails / Unicorn / Nginx容器连接到MySQL容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!