Connect Rails/Unicorn/Nginx container to MySQL con

2019-05-15 23:51发布

问题:

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 IP http://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

  1. db:migrate was having problems running so I ultimately ran it by hand in a docker 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
  2. This linking article helped me understand that my link was not being created so there was no way to communicate properly.
  3. Once I understood how to accurately make the link, I did update my database.yml with the host and port values
  4. Use this command to check the names of your env variables docker run --rm --name <unique-value> --link <db-name> <non-db-image> env.
  5. Use this to see the value of the links in your app container docker inspect -f "{{ .HostConfig.Links }}" <app-name>

回答1:

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'] %>


回答2:

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