Dockerfile django & mysql












0















I'm trying to create a docker environment for my django project



my dockerfile :



 FROM python:3
ENV PYTHONUNBUFFERED=1
RUN apt-get install default-libmysqlclient-dev
RUN mkdir /config
ADD /config/requirements.txt /config/
RUN pip install -r /config/requirements.txt
RUN mkdir /src
WORKDIR /src


my docker-compose :



 version: '3'

services:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_DATABASE: ProjetDjango
container_name: mysql01
restart: always
nginx:
image: nginx:1.13-alpine
container_name: nginx01
ports:
- "8000:8000"
volumes:
- ./project:/src
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- web
web:
build: .
container_name: django01
command: bash -c "python3 manage.py makemigrations && python3 manage.py migrate && python3 manage.py collectstatic --noinput && gunicorn hello_django.wsgi -b 0.0.0.0:8000"
depends_on:
- db
volumes:
- ./project:/src
expose:
- "8000"
restart: always


my settings.py :



DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'ProjetDjango',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}


I've got this error while running : docker-compose up



django.db.utils.OperationalError: (2006, 'Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")')
django01 exited with code 1



I've been working on this issue for 2 days now and didn't find a response that solve it!



Thank you for your help










share|improve this question





























    0















    I'm trying to create a docker environment for my django project



    my dockerfile :



     FROM python:3
    ENV PYTHONUNBUFFERED=1
    RUN apt-get install default-libmysqlclient-dev
    RUN mkdir /config
    ADD /config/requirements.txt /config/
    RUN pip install -r /config/requirements.txt
    RUN mkdir /src
    WORKDIR /src


    my docker-compose :



     version: '3'

    services:
    db:
    image: mysql
    environment:
    MYSQL_ROOT_PASSWORD: root
    MYSQL_USER: root
    MYSQL_PASSWORD: root
    MYSQL_DATABASE: ProjetDjango
    container_name: mysql01
    restart: always
    nginx:
    image: nginx:1.13-alpine
    container_name: nginx01
    ports:
    - "8000:8000"
    volumes:
    - ./project:/src
    - ./config/nginx:/etc/nginx/conf.d
    depends_on:
    - web
    web:
    build: .
    container_name: django01
    command: bash -c "python3 manage.py makemigrations && python3 manage.py migrate && python3 manage.py collectstatic --noinput && gunicorn hello_django.wsgi -b 0.0.0.0:8000"
    depends_on:
    - db
    volumes:
    - ./project:/src
    expose:
    - "8000"
    restart: always


    my settings.py :



    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'ProjetDjango',
    'USER': 'root',
    'PASSWORD': 'root',
    'HOST': '127.0.0.1',
    'PORT': '3306',
    }
    }


    I've got this error while running : docker-compose up



    django.db.utils.OperationalError: (2006, 'Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")')
    django01 exited with code 1



    I've been working on this issue for 2 days now and didn't find a response that solve it!



    Thank you for your help










    share|improve this question



























      0












      0








      0








      I'm trying to create a docker environment for my django project



      my dockerfile :



       FROM python:3
      ENV PYTHONUNBUFFERED=1
      RUN apt-get install default-libmysqlclient-dev
      RUN mkdir /config
      ADD /config/requirements.txt /config/
      RUN pip install -r /config/requirements.txt
      RUN mkdir /src
      WORKDIR /src


      my docker-compose :



       version: '3'

      services:
      db:
      image: mysql
      environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_DATABASE: ProjetDjango
      container_name: mysql01
      restart: always
      nginx:
      image: nginx:1.13-alpine
      container_name: nginx01
      ports:
      - "8000:8000"
      volumes:
      - ./project:/src
      - ./config/nginx:/etc/nginx/conf.d
      depends_on:
      - web
      web:
      build: .
      container_name: django01
      command: bash -c "python3 manage.py makemigrations && python3 manage.py migrate && python3 manage.py collectstatic --noinput && gunicorn hello_django.wsgi -b 0.0.0.0:8000"
      depends_on:
      - db
      volumes:
      - ./project:/src
      expose:
      - "8000"
      restart: always


      my settings.py :



      DATABASES = {
      'default': {
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'ProjetDjango',
      'USER': 'root',
      'PASSWORD': 'root',
      'HOST': '127.0.0.1',
      'PORT': '3306',
      }
      }


      I've got this error while running : docker-compose up



      django.db.utils.OperationalError: (2006, 'Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")')
      django01 exited with code 1



      I've been working on this issue for 2 days now and didn't find a response that solve it!



      Thank you for your help










      share|improve this question
















      I'm trying to create a docker environment for my django project



      my dockerfile :



       FROM python:3
      ENV PYTHONUNBUFFERED=1
      RUN apt-get install default-libmysqlclient-dev
      RUN mkdir /config
      ADD /config/requirements.txt /config/
      RUN pip install -r /config/requirements.txt
      RUN mkdir /src
      WORKDIR /src


      my docker-compose :



       version: '3'

      services:
      db:
      image: mysql
      environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_DATABASE: ProjetDjango
      container_name: mysql01
      restart: always
      nginx:
      image: nginx:1.13-alpine
      container_name: nginx01
      ports:
      - "8000:8000"
      volumes:
      - ./project:/src
      - ./config/nginx:/etc/nginx/conf.d
      depends_on:
      - web
      web:
      build: .
      container_name: django01
      command: bash -c "python3 manage.py makemigrations && python3 manage.py migrate && python3 manage.py collectstatic --noinput && gunicorn hello_django.wsgi -b 0.0.0.0:8000"
      depends_on:
      - db
      volumes:
      - ./project:/src
      expose:
      - "8000"
      restart: always


      my settings.py :



      DATABASES = {
      'default': {
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'ProjetDjango',
      'USER': 'root',
      'PASSWORD': 'root',
      'HOST': '127.0.0.1',
      'PORT': '3306',
      }
      }


      I've got this error while running : docker-compose up



      django.db.utils.OperationalError: (2006, 'Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")')
      django01 exited with code 1



      I've been working on this issue for 2 days now and didn't find a response that solve it!



      Thank you for your help







      mysql django docker






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 19 at 14:28









      Khashayar Ghamati

      10018




      10018










      asked Jan 19 at 13:51









      Zineb LazrakZineb Lazrak

      31




      31
























          1 Answer
          1






          active

          oldest

          votes


















          2














          When you link your db service to your web service in docker so you must use your db service name instead of your database server ip for connecting database :



          db:
          image: mysql
          restart: unless-stopped
          container_name: db_of_my_project

          web:
          build: .
          container_name: django01
          command: ...
          links:
          - db
          volumes:
          - ./project:/src
          expose:
          - "8000"
          restart: always


          and your database configuration:



          DATABASES = {
          'default': {
          'ENGINE': 'django.db.backends.mysql',
          'NAME': 'ProjetDjango',
          'USER': 'root',
          'PASSWORD': 'root',
          'HOST': 'db', # database container name
          'PORT': '',
          }
          }





          share|improve this answer





















          • 1





            (This answer will work equally well without the links: section.)

            – David Maze
            Jan 19 at 17:44











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267781%2fdockerfile-django-mysql%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          When you link your db service to your web service in docker so you must use your db service name instead of your database server ip for connecting database :



          db:
          image: mysql
          restart: unless-stopped
          container_name: db_of_my_project

          web:
          build: .
          container_name: django01
          command: ...
          links:
          - db
          volumes:
          - ./project:/src
          expose:
          - "8000"
          restart: always


          and your database configuration:



          DATABASES = {
          'default': {
          'ENGINE': 'django.db.backends.mysql',
          'NAME': 'ProjetDjango',
          'USER': 'root',
          'PASSWORD': 'root',
          'HOST': 'db', # database container name
          'PORT': '',
          }
          }





          share|improve this answer





















          • 1





            (This answer will work equally well without the links: section.)

            – David Maze
            Jan 19 at 17:44
















          2














          When you link your db service to your web service in docker so you must use your db service name instead of your database server ip for connecting database :



          db:
          image: mysql
          restart: unless-stopped
          container_name: db_of_my_project

          web:
          build: .
          container_name: django01
          command: ...
          links:
          - db
          volumes:
          - ./project:/src
          expose:
          - "8000"
          restart: always


          and your database configuration:



          DATABASES = {
          'default': {
          'ENGINE': 'django.db.backends.mysql',
          'NAME': 'ProjetDjango',
          'USER': 'root',
          'PASSWORD': 'root',
          'HOST': 'db', # database container name
          'PORT': '',
          }
          }





          share|improve this answer





















          • 1





            (This answer will work equally well without the links: section.)

            – David Maze
            Jan 19 at 17:44














          2












          2








          2







          When you link your db service to your web service in docker so you must use your db service name instead of your database server ip for connecting database :



          db:
          image: mysql
          restart: unless-stopped
          container_name: db_of_my_project

          web:
          build: .
          container_name: django01
          command: ...
          links:
          - db
          volumes:
          - ./project:/src
          expose:
          - "8000"
          restart: always


          and your database configuration:



          DATABASES = {
          'default': {
          'ENGINE': 'django.db.backends.mysql',
          'NAME': 'ProjetDjango',
          'USER': 'root',
          'PASSWORD': 'root',
          'HOST': 'db', # database container name
          'PORT': '',
          }
          }





          share|improve this answer















          When you link your db service to your web service in docker so you must use your db service name instead of your database server ip for connecting database :



          db:
          image: mysql
          restart: unless-stopped
          container_name: db_of_my_project

          web:
          build: .
          container_name: django01
          command: ...
          links:
          - db
          volumes:
          - ./project:/src
          expose:
          - "8000"
          restart: always


          and your database configuration:



          DATABASES = {
          'default': {
          'ENGINE': 'django.db.backends.mysql',
          'NAME': 'ProjetDjango',
          'USER': 'root',
          'PASSWORD': 'root',
          'HOST': 'db', # database container name
          'PORT': '',
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 27 at 8:03

























          answered Jan 19 at 14:04









          Khashayar GhamatiKhashayar Ghamati

          10018




          10018








          • 1





            (This answer will work equally well without the links: section.)

            – David Maze
            Jan 19 at 17:44














          • 1





            (This answer will work equally well without the links: section.)

            – David Maze
            Jan 19 at 17:44








          1




          1





          (This answer will work equally well without the links: section.)

          – David Maze
          Jan 19 at 17:44





          (This answer will work equally well without the links: section.)

          – David Maze
          Jan 19 at 17:44


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267781%2fdockerfile-django-mysql%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Callistus III

          Ostreoida

          Plistias Cous