1

I was trying to run a Django project with Docker + Nginx + MySQL, by following this Link After successfull build, by running

docker-compose up --build

Here is my setting.py Database connection

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'smartCut',
        'USER': 'antu',
        'PASSWORD': 'secure',
        'HOST': 'mysql',
        'PORT': 3306,
    }
}

If I use HOST: 'mysql'

(1045, "Access denied for user 'antu'@'172.25.0.3' (using password: YES)")

Error But If I use If I use HOST: 'localhost'

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

Also, this project cannot find the static file in the admin panel.

Whats I'm doing wrong?

Update:

$ docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS                               NAMES
07a5a13e15a1        projectone_web      "nginx -g 'daemon of…"   16 seconds ago      Up 8 seconds              0.0.0.0:8888->80/tcp                web
2431afe3eed2        projectone_python   "uwsgi --ini /uwsgi.…"   21 seconds ago      Up 17 seconds                                                 python
83572753b4ae        projectone_mysql    "docker-entrypoint.s…"   56 seconds ago      Up 52 seconds (healthy)   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

docker-compose.yml

version: "2.4"
services:
  web:
    container_name: web
    build: ./docker/nginx
    ports:
      - 8888:80
    volumes:
      - ./app:/var/www/html
    working_dir: /etc/nginx
    links:
      - python
  python:
    container_name: python
    build: ./docker/python
    volumes:
      - ./app:/var/www/html
    working_dir: /var/www/html
    depends_on:
      mysql:
        condition: service_healthy
  mysql:
    build: docker/mysql
    container_name: mysql
    ports:
      - 3306:3306
    volumes:
      - data-volume:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=secure
      - MYSQL_DATABASE=smartCut
    healthcheck:
      test: "exit 0"

volumes:
  data-volume:

$ docker logs 83572753b4ae

2019-07-21T10:00:06.327977Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-07-21T10:00:06.328151Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 1
2019-07-21T10:00:06.332333Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2019-07-21T10:00:12.001312Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-07-21T10:00:12.150341Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2019-07-21T10:00:12.290955Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.16'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2019-07-21T10:00:12.817464Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
6
  • This works for me, I can connect to MySQL with this docker compose. Have you changed anything in the code from this repo? Commented Jul 21, 2019 at 10:05
  • I try to run two projects with I run one project in 3306 MySQL port and other is 3301 port but both are not working Commented Jul 21, 2019 at 10:07
  • have you changed app/config/settings.py file and the database configuration? Show this file. Commented Jul 21, 2019 at 10:07
  • You need to include the actual error message you're getting as text in the question; don't paste a screen shot of a browser window. Showing the code or configuration where you're telling the application how to connect to the database would be helpful too. Commented Jul 21, 2019 at 10:18
  • 1
    USER should be root in your database settings. And also leave mysql as host. Commented Jul 21, 2019 at 10:25

1 Answer 1

2

So as explained you should use mysql as host and root as USER in your database config :

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

mysql will be resolved to mysql container IP because container from compose will be connected to the same network.

Also you have set up root password for your mysql container by using environment variable - MYSQL_ROOT_PASSWORD=secure so root have to be used as user.

If you want to create new user you could use MYSQL_USER, MYSQL_PASSWORD environment variables - reference can be found in mysql docker hub. Modify your docker-compose accordingly :

version: "2.4"
services:
  web:
    container_name: web
    build: ./docker/nginx
    ports:
      - 8888:80
    volumes:
      - ./app:/var/www/html
    working_dir: /etc/nginx
    links:
      - python
  python:
    container_name: python
    build: ./docker/python
    volumes:
      - ./app:/var/www/html
    working_dir: /var/www/html
    depends_on:
      mysql:
        condition: service_healthy
  mysql:
    build: docker/mysql
    container_name: mysql
    ports:
      - 3306:3306
    volumes:
      - data-volume:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=secure #root password
      - MYSQL_DATABASE=smartCut #this database will be created for MYSQL_USER
      - MYSQL_USER=antu 
      - MYSQL_PASSWORD=secure #your user password
    healthcheck:
      test: "exit 0"

volumes:
  data-volume:
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.