-1

I am attempting to dockerize a Django project that features a MySQL database and nginx. When I run the docker-compose up --build command, I receive the error message django.db.utils.OperationalError: (2002, "Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2)").

Here's my docker-compose.yml file:

version: '3.7'

services:
  app:
    build: ./app
    env_file:
      - .env
    container_name: app
    restart: always
    expose:
        - 8000
    command: bash -c  "python3 manage.py collectstatic --noinput && python3 manage.py migrate --noinput --fake-initial && \
              gunicorn -b 0.0.0.0:8000 veblog.wsgi"
    environment:
      - MYSQL_DATABASE=${NAME}
      - MYSQL_USER=${USER_NAME}
      - MYSQL_PASSWORD=${PASSWORD}
      - MYSQL_HOST=sqldb
    mem_limit: 1g
    depends_on:
      - sqldb
      # - nginx
    volumes:
      - ./volumes/app:/app
      - type: bind
        source: ./volumes/media
        target: /app/media
      - type: bind
        source:  ./volumes/static
        target:  /app/static
    
  
  nginx:
      build: ./nginx
      container_name: nginx
      restart: always
      ports:
        - 8000:80
  
     
  sqldb:
    image:  mysql:latest
    container_name: sqldb
    restart:  always
    depends_on:
      - nginx
    expose:
      - 3306
    environment:
      - MYSQL_DATABASE=${NAME}
      - MYSQL_USER=${USER_NAME}
      - MYSQL_PASSWORD=${PASSWORD}
      - MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD} 
      - MYSQL_INITDB_SKIP_TZINFO=true  
      - MYSQL_INIT_COMMAND=SET GLOBAL host_cache_size=0;
    volumes:
      - type:  bind
        source:  ./volumes/dbdata
        target:  /var/lib/mysql
      - /usr/share/zoneinfo:/usr/share/zoneinfo:ro

This is my Dockerfile in the app directory:

FROM python:3
ENV PYTHONDONTWRITEBYCODE 1
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /app
WORKDIR /app
COPY . .
RUN apt update
RUN apt install gcc python3-dev musl-dev -y
RUN pip install --upgrade pip
RUN pip install mysqlclient
COPY ./requirement.txt .
RUN pip install -r requirement.txt
ADD . /app/
RUN pip install gunicorn

Here's the Dockerfile for nginx:

FROM nginx:alpine
RUN rm /etc/nginx/conf.d/default.conf
ADD default.conf /etc/nginx/conf.d/default.conf

And here's my default.conf file:

upstream app {
    server app:8000;
}

server {
    listen 80;
    server_name localhost;
    location / {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
    location /media/ {
        alias /app/media;
    }
    location /static/ {
        alias /app/static;
    }
}

This is my settings.py file:

'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': config('NAME'),
        'USER': config('USER_NAME'),
        'PASSWORD': config('PASSWORD'),
        'HOST': 'localhost',  # host.docker.internal
        'PORT': '3306',

MySQL and Apache2 are started and active. I can connect to my database in the terminal. When I set HOST to 127.0.0.1, I get the following error: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)"). When I set HOST to sqldb, I get the following error: (2002, "Can't connect to MySQL server on 'sqldb' (115)"). /var/run/mysqld/mysqld.sock and /etc/mysql/my.cnf are empty. I have tried the following commands: sudo chmod -R 755 /var/run/mysqld, ln -s /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock, SQL uninstall and install, set IP Docker to bind-address. I use command 'docker build' separately instead of docker-compose up --build.

I tried the solutions listed in this link: ERROR 2002 (HY000): Can't connect to MySQL server on '192.168.1.15' (115), but they didn't work. Specifically, I ran the command mysql --help | grep -A 1 "Default options" Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf. I then set the bind-address to 0.0.0.0 in /etc/my.cnf, /etc/mysql/my.cnf, and ~/.my.cnf, but when I logged into mysql and ran SHOW VARIABLES LIKE 'bind-address', I received the error 'Empty set (0.01 sec)'. I also gave access to my firewall. Can anyone guide me on this?

1 Answers1

0

The error message you're encountering indicates that the Django application is unable to connect to the MySQL database. There are a few potential issues that could be causing this problem.

  1. Verify MySQL container name: In your Django settings, you're using the hostname "sqldb" to connect to the MySQL container. Make sure that the container name defined in your docker-compose.yml file matches the name used in the Django settings. In your case, it should be sqldb.

  2. Check MySQL host configuration: In your Django settings, you have set the MySQL host to 'localhost'. However, when running in Docker containers, 'localhost' typically refers to the container itself. Instead, you should use the hostname of the MySQL service defined in your docker-compose.yml file, which is 'sqldb'. Update your Django settings as follows:

    'HOST': 'sqldb',
    
  3. Verify MySQL port configuration: In your Django settings, you have set the MySQL port to '3306', which is the default MySQL port. However, when using Docker Compose, the services within the same network can communicate with each other using the container names. Therefore, you don't need to expose the MySQL container's port or specify it in the Django settings. Remove the expose section for the sqldb service in your docker-compose.yml file:

    expose:
      - 3306
    

    Also, update your Django settings:

    'PORT': '',
    

    With these changes, Django will be able to connect to the MySQL container using the default MySQL port.

  4. Verify MySQL credentials: Double-check that the values for NAME, USER_NAME, PASSWORD, and ROOT_PASSWORD in your .env file match the corresponding environment variables used in the docker-compose.yml file and Django settings.

After making these changes, try running docker-compose up --build again and see if the issue is resolved.

mrhcmp
  • 1