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
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>