Docker MySQL can't connect to socket
Asked Answered
I

1

6

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.

Inalterable answered 18/11, 2015 at 19:35 Comment(0)
P
6

The problem here is with the links directive in your docker-compose.yml file. You have:

links:
  - database

That's basically saying that the link name:alias is database:database, according to the docker-compose.yml reference.

Also, if you read the linking container docs you can see that the environments exported to the source container are of the format ALIAS_XXX for example ALIAS_PORT_3306_TCP_PORT. So in essence in your database.yml what you want to do is something like this:

default: &default
  adapter: mysql2
  database: dockertest
  host: <%= ENV['DATABASE_PORT_3306_TCP_ADDR'] %>
  port: <%= ENV['DATABASE_PORT_3306_TCP_PORT'] %>
  username: dockertest
  password: dockertest

development:
  <<: *default

production: 
  <<: *default

If you want to use the MYSQL alias your links would have to look something like this in your docker-compose.yml file.

links:
  - database:mysql

The error:

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

is basically coming from your Rails app not being to see what's in your database.yml and defaulting to a local /var/run/mysqld/mysqld.sock connection.

Hope it helps.

Pragmaticism answered 18/11, 2015 at 22:11 Comment(2)
I would like to thank you for your help, especially for the level of detail and good explanation. Now the docker-compose run app rake db:migrate runs successfully, which means that the rake program with production and development environments is able to reach the database container. But from the passenger rails app I'm still getting the Can'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 lookInalterable
Well, I change mysql:latest -> mysql:5.5 and passenger-full -> passenger-ruby21 and everything workedInalterable

© 2022 - 2024 — McMap. All rights reserved.