4
respostas

error ao executar docker-compose run web python manage.py makemigrations

Tentei executar o comando abaixo:

docker-compose run web python manage.py makemigrations

E deu os seguintes erros:

Starting alura_devops-master_db_1 ... done System check identified some issues:

WARNINGS: core.Todo.created_at: (fields.W161) Fixed default value provided. HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use django.utils.timezone.now Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection self.connect() File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect self.connection = self.get_new_connection(conn_params) File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection return Database.connect(*conn_params) File "/usr/local/lib/python3.6/site-packages/MySQLdb/init.py", line 84, in Connect return Connection(args, *kwargs) File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 164, in init super(Connection, self).init(args, **kwargs2) MySQLdb._exceptions.OperationalError: (2003, 'Can't connect to MySQL server on 'db' (111 "Connection refused")')

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "manage.py", line 15, in execute_from_command_line(sys.argv) File "/usr/local/lib/python3.6/site-packages/django/core/management/init.py", line 381, in execute_from_command_line utility.execute() File "/usr/local/lib/python3.6/site-packages/django/core/management/init.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv self.execute(args, **cmd_options) File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute output = self.handle(args, options) File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped res = handle_func(args, *kwargs) File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 103, in handle loader.check_consistent_history(connection) File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 281, in check_consistent_history applied = recorder.applied_migrations() File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations if self.has_table(): File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 44, in has_table return self.Migration.meta.dbtable in self.connection.introspection.table_names(self.connection.cursor()) File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor return self.cursor() File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor self.ensureconnection() File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection self.connect() File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection self.connect() File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect self.connection = self.get_new_connection(conn_params) File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection return Database.connect(conn_params) File "/usr/local/lib/python3.6/site-packages/MySQLdb/init.py", line 84, in Connect return Connection(args, **kwargs) File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 164, in init super(Connection, self).init(args, **kwargs2) django.db.utils.OperationalError: (2003, 'Can't connect to MySQL server on 'db' (111 "Connection refused")')

O que deve ser feito para que funcione.

4 respostas

Olá Sebastião, tudo bem? O erro na verdade é apenas a conexão com o MySQL:

'Can't connect to MySQL server on 'db' (111 "Connection refused")

A conexão foi recusada pelo MySQL, então precisamos verificar isso. Vamos começar pelo código do arquivo docker-compose.yml, você utilizou esse código:

version: '3'


services:
  db:
    image: mysql:5.7
    ports:
      - '3309:3306'
    environment:
       MYSQL_DATABASE: 'todo_dev'
       MYSQL_USER: 'devops_dev'
       MYSQL_PASSWORD: 'mestre'
       MYSQL_ROOT_PASSWORD: 'senha'
  web:
    image: jnlucas/minha-imagem:latest
    volumes:
      - ./env:/usr/src/app/to_do/.env
    ports:
      - "8009:8000"
    depends_on:
      - db

Bom, o MySQL pode está falando por questão da porta, então você pode alterar a linha:

- '3309:3306'

Deixando da seguinte forma:

- '3306:3306'

E então você excluir os containers e cria novamente com os comandos:

docker-compose down
docker-compose up

E fala pra gente se funcionou!

Ainda continua o erro, segue abaixo:

docker-compose run web python manage.py makemigrations Starting alura_devops-master_db_1 ... done System check identified some issues:

WARNINGS: core.Todo.created_at: (fields.W161) Fixed default value provided. HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use django.utils.timezone.now Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection self.connect() File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect self.connection = self.get_new_connection(conn_params) File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection return Database.connect(*conn_params) File "/usr/local/lib/python3.6/site-packages/MySQLdb/init.py", line 84, in Connect return Connection(args, *kwargs) File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 164, in init super(Connection, self).init(args, **kwargs2) MySQLdb._exceptions.OperationalError: (2003, 'Can't connect to MySQL server on 'db' (111 "Connection refused")')

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "manage.py", line 15, in execute_from_command_line(sys.argv) File "/usr/local/lib/python3.6/site-packages/django/core/management/init.py", line 381, in execute_from_command_line utility.execute() File "/usr/local/lib/python3.6/site-packages/django/core/management/init.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv self.execute(args, **cmd_options) File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute output = self.handle(args, options) File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped res = handle_func(args, *kwargs) File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 103, in handle loader.check_consistent_history(connection) File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 281, in check_consistent_history applied = recorder.applied_migrations() File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations if self.has_table(): File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 44, in has_table return self.Migration.meta.dbtable in self.connection.introspection.table_names(self.connection.cursor()) File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor return self.cursor() File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor self.ensureconnection() File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection self.connect() File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection self.connect() File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect self.connection = self.get_new_connection(conn_params) File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection return Database.connect(conn_params) File "/usr/local/lib/python3.6/site-packages/MySQLdb/init.py", line 84, in Connect return Connection(args, **kwargs) File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 164, in init super(Connection, self).init(args, **kwargs2) django.db.utils.OperationalError: (2003, 'Can't connect to MySQL server on 'db' (111 "Connection refused")')

Segue meu docker-compose:

version: '3'

services: db: image: mysql:5.7 ports:

  - '3306:3306'
environment:
   MYSQL_DATABASE: 'todo_dev'
   MYSQL_USER: 'devops_dev'
   MYSQL_PASSWORD: 'mestre'
   MYSQL_ROOT_PASSWORD: 'senha'

web: image: jnlucas/minha-imagem:latest volumes:

  - ./env:/usr/src/app/to_do/.env
ports:
  - "8009:8000"
depends_on:
  - db

Sebastião, você poderia também colocar a porta padrão do MySQL no arquivo env na raiz do projeto, ficando assim:

DB_PORT = "3306"

Isso, porque o MySQL sempre por padrão já realizar o EXPOSE da porta e por algum motivo a aplicação não está conseguindo encontrar o MySQL em outra porta diferente da porta padrão "3306".

Tenta fazer isso e executar novamente e fala pra gente se funcionou!