Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker MySQL can't connect to socket

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.

like image 465
mariowise Avatar asked Nov 18 '15 19:11

mariowise


1 Answers

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.

like image 124
Rico Avatar answered Sep 20 '22 11:09

Rico