0

I am trying to configure Django and MySql application with Docker containers.

For Django I am using python:3.7-slim image and for MySql mysql:5.6.

When I run docker-compose up it returns an error stated below -

ERROR: for app_mysql_db_1  Cannot start service mysql_db: driver failed programming external connectivity on endpoint app_mysql_db_1 (c647d4793a198af2c09cc52d08191fb2cd984025ad0a61434ad1577d9dcccebe): Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use 

I run command docker ps -a to check docker status and found that mysql container was created but the python container status was exited.

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
7d91795e0bae        mysql:5.6           "docker-entrypoint.s…"   15 seconds ago      Created                                        app_mysql_db_1
fa0419ad0f21        e0bf94710555        "/bin/sh -c 'adduser…"   2 minutes ago       Exited (1) 2 minutes ago                       pedantic_faraday

can someone rewrite or suggest the modification for the configurations.

Dockerfile

FROM python:3.7-slim

ENV PYTHONUNBUFFERED 1

RUN apt-get update
RUN apt-get install python3-dev default-libmysqlclient-dev gcc  -y
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

RUN mkdir /app

WORKDIR /app
COPY . /app

docker-compose.yaml

version: "3"

services:
  eitan-application:
    restart: always
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./eitan:/app
    command: >
      sh -c "python3 manage.py runserver 0.0.0.0:8000
      && python3 manage.py makemigrations
      && python3 manage.py migrate"
    depends_on:
      - mysql_db
  mysql_db:
    image: mysql:5.6
    command: mysqld --default-authentication-plugin=mysql_native_password
    volumes:
      - "./mysql:/var/lib/mysql"
    ports:
      - "3306:3306"
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=root
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my-app-db',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'mysql_db',
        'PORT': 3307,
    }
}

1 Answer 1

1

bind: address already in use suggests that you have some local database running. If you don't need to access database outside of docker-compose network don't expose port 3306. So I'd try to test it without

...
    ports:
      - "3306:3306"
...

Also in settings.py you connect to port mysql_db:3307 so change it to default port 3306.

Even if you expose database port to some other port on localhost then settings.py connects using mysql_db network, so you shouldn't change this port in django settings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.