使用MySQL和Django后端运行Docker

y3bcpkx1  于 2023-11-16  发布在  Mysql
关注(0)|答案(1)|浏览(125)

我第一次使用docker,我有点不能正常启动它。这是我的docker-compose.yml文件:

version: "3"
services:
  mysql:
    image: mysql
    container_name: my-mysql
    restart: on-failure
    ports:
      - 3306:3306
    volumes:
      - ./mysql_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ""
      MYSQL_USER: intranet
      MYSQL_PASSWORD: ""
      MYSQL_DATABASE: intranet
      MYSQL_ALLOW_EMPTY_PASSWORD: yes
    healthcheck:
      test:
        [
          "CMD",
          "mysqladmin",
          "ping",
          "-h",
          "localhost",
          "-u",
          "root",
          "-pmy-root-password",
        ]
      interval: 10s
      timeout: 5s
      retries: 5

  backend:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: my-backend
    restart: on-failure
    volumes:
      - .:/app
    environment:
      DATABASE_URL: mysql2://intranet@mysql:3306/intranet
      SECRET_KEY: x
    ports:
      - 8000:8000
    depends_on:
      - mysql
    command: ["./etc/entry", "web-prod"]

字符串
我在MySQL工作台中创建了数据库127.0.0.1:3306,名称为Intranet,代码为:

CREATE USER 'intranet'@'localhost';
CREATE DATABASE intranet;
GRANT ALL PRIVILEGES ON intranet.* TO 'intranet'@'localhost';
FLUSH PRIVILEGES;


我得到的dockerfile是这样的:

FROM python:3.7

RUN wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O /usr/local/bin/cloud_sql_proxy \
    && chmod +x /usr/local/bin/cloud_sql_proxy

RUN pip3 install poetry

ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8

# -- Install Application into container:
WORKDIR /app

COPY pyproject.toml pyproject.toml
COPY poetry.lock poetry.lock

RUN poetry config virtualenvs.create false && poetry install

COPY . /app

ARG GIT_VERSION
ENV GIT_VERSION $GIT_VERSION
ENTRYPOINT ["/app/etc/entry"]
CMD ["web-prod"]


当运行docker-compose up时,我得到了这个错误:django.db.utils.OperationalError: (1045, "Access denied for user 'intranet'@'172.19.0.3' (using password: NO)")尝试搜索这个,但没有任何帮助。我还将提供来自我的项目的自述文件:

需要安装:

  • 诗歌
  • email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)
  • Python 3.7
  • 节点v16

后台

请确保在MySQL中创建了intranet数据库。如果没有,您可能需要使用以下命令:

mysql -u root -p
CREATE USER 'intranet'@'localhost';
CREATE DATABASE intranet;
GRANT ALL PRIVILEGES ON intranet.* TO 'intranet'@'localhost';
FLUSH PRIVILEGES;
exit;


然后:

  1. poetry install
  2. poetry shell
  3. export DATABASE_URL='mysql2://intranet@localhost:3306/intranet'
  4. export SECRET_KEY='x'
  5. ./manage.py migrate
  6. ./manage.py update_perms
  7. ./manage.py createcachetable
  8. ./manage.py runserver
    顺便说一句。我自己写码头撰写文件,这就是为什么我认为这是我的问题。我真的很感激的答案,告诉我一步一步地修复,谢谢
xzv2uavs

xzv2uavs1#

更新:如果你有这个问题,请仔细检查你的项目是否使用postgres而不是mysql进行开发
正确的docker-compose文件:

version: "2.1"
services:
  database:
    image: postgres:11
    container_name: ni-db
    restart: on-failure
    ports:
      - 5436:5432 # to avoid conflicts with other projects
    volumes:
      - ./.testdb:/var/lib/postgresql/data
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
      POSTGRES_DB: backend
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  migrations:
    build: .
    image: ni-backend
    container_name: ni-migrations
    restart: on-failure
    env_file:
      - ./.env
    environment:
      DATABASE_URL: postgresql://postgres@database/backend
      SECRET_KEY: x
    volumes:
      - ./:/app
    command: ["python", "manage.py", "migrate"]
    depends_on:
      database:
        condition: service_healthy

  backend_api:
    image: ni-backend
    container_name: ni-api
    restart: on-failure
    env_file:
      - ./.env
    environment:
      DATABASE_URL: postgresql://postgres@database/backend
      SECRET_KEY: x
    ports:
      - 8000:80
    volumes:
      - ./:/app
    command: ["python", "manage.py", "runserver", "0.0.0.0:80"]
    depends_on:
      migrations:
        condition: service_started

字符串

相关问题