I'm learning Docker and I've a problem trying to connect a Rails app on the passenger-full
container and a mysql
container. Both are linked in a compose file
app:
build: ./rails
ports:
- "80:80"
links:
- database
volumes:
- ./rails:/home/app/webapp
database:
image: mysql
environment:
- MYSQL_DATABASE="dockertest"
- MYSQL_USER="dockertest"
- MYSQL_PASSWORD="dockertest"
- MYSQL_ROOT_PASSWORD="root"
So I added the apt-get install
at the top of my Dockerfile like this
FROM phusion/passenger-full
RUN apt-get update && apt-get install libmysqlclient-dev mysql-client -y
# Set correct environment variables.
ENV HOME /root
# Use baseimage-docker's init process.
CMD ["/sbin/my_init"]
RUN rm -f /etc/service/nginx/down
RUN rm /etc/nginx/sites-enabled/default
ADD webapp.conf /etc/nginx/sites-enabled/webapp.conf
RUN mkdir /home/app/webapp
WORKDIR /home/app/webapp
ADD . /home/app/webapp
RUN cd /home/app/webapp && bundle install
RUN touch /home/app/webapp/tmp/restart.txt
# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
Also this is my database.yml
in the Rails app.
default: &default
adapter: mysql2
database: dockertest
host: <%= ENV['MYSQL_PORT_3306_TCP_ADDR'] %>
port: <%= ENV['MYSQL_PORT_3306_TCP_PORT'] %>
username: dockertest
password: dockertest
development:
<<: *default
production:
<<: *default
The problem is that I cant stop receiving the error
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
The webconf file is
# /etc/nginx/sites-enabled/webapp.conf:
server {
listen 80;
server_name localhost;
root /home/app/webapp/public;
passenger_enabled on;
passenger_user app;
passenger_ruby /usr/bin/ruby2.2;
}
Is that the right way to do this? As you can see I'm pretty new to docker.
docker-compose run app rake db:migrate
runs successfully, which means that therake
program withproduction
anddevelopment
environments is able to reach the database container. But from the passenger rails app I'm still getting theCan't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
error. I added the/etc/nginx/sites-enabled/webapp.conf
file for more details, if you have time to take a look – Inalterable