Docker 卷和 MySQL 数据访问

问题描述 投票:0回答:1

我目前正在使用 MySql 数据库在 Flask 中编写一个自动签入系统。我希望代码无限期地运行,因此我在 Docker 上部署了应用程序和数据库。这样我就可以在我的主计算机上继续迭代,同时在我的旧计算机上运行较新的版本。

但是,我的问题是,两台计算机之间的 MySql 数据库不同,并且在我的主计算机上本地运行的 MySql 数据库也与我在主计算机上运行 docker 时的数据库不同。我希望能够在开发应用程序时查询信息并在命令行中使用 MySql 数据库。我的主计算机在 MacOS 上本地运行 MySQL。

目前我已将数据库设置为作为 Docker 卷运行,但我不确定这是最佳选择。如果有人对跨设备在 docker 中最好地持久运行数据库的方法有见解(或者至少如何使我的其他计算机和 docker 卷查询并将信息添加到我的计算机上本地运行的数据库),我将不胜感激!

这是我的 compose.yaml 文件:

services:
  server:
    build:
      context: .
    ports:
      - 8000:8000
    environment:
      - SQL_PASS=/run/secrets/db-password
      - MYSQL_ROOT_PASSWORD=/run/secrets/db-password
      - TWILIO_ACCOUNT_SID=/run/secrets/twilio-sid
      - TWILIO_AUTH_TOKEN=/run/secrets/twilio-token
    depends_on:
      db:
        condition: service_healthy
    secrets:
      - db-password
      - twilio-sid
      - twilio-token

  db:
    # We use a mariadb image which supports both amd64 & arm64 architecture
    #image: mariadb:10-focal
    # If you really want to use MySQL, uncomment the following line
    image: mysql:8
    restart: always
    healthcheck:
      test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 --password="$$(cat /run/secrets/db-password)" --silent']
      interval: 3s
      retries: 5
      start_period: 30s
    secrets:
      - db-password
    volumes:
      - /usr/local/mysql/data/callassure:/db-data
      - type: bind
        source: ./initial-data
        target: /docker-entrypoint-initdb.d/
    environment:
      - MYSQL_DATABASE=callassure
      - MYSQL_USER=mysql
      - MYSQL_ROOT_PASSWORD=/run/secrets/db-password
      - MYSQL_PASSWORD=/run/secrets/db-password
    expose:
      - 3306
      - 33060

volumes:
  db-data:

secrets:
  db-password:
    file: db/password.txt
  twilio-sid:
    file: db/sid.txt
  twilio-token:
    file: db/auth.txt

和我的 Dockerfile:

# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG PYTHON_VERSION=3.9.6
FROM python:${PYTHON_VERSION}-slim as base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Install required system packages
RUN apt-get update && apt-get install -y \
    gcc \
    default-libmysqlclient-dev \
    build-essential \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
    --mount=type=bind,source=requirements.txt,target=requirements.txt \
    python -m pip install -r requirements.txt

# Switch to the non-privileged user to run the application.
USER appuser

# Copy the source code into the container.
COPY . .

# Expose the port that the application listens on.
EXPOSE 8000

# Run the application.
CMD ["flask", "run", "-h", "0.0.0.0", "-p", "8000"]
mysql docker macos docker-volume
1个回答
0
投票

假设两台计算机在同一网络上运行,您的设置似乎已准备好实现您想要的效果。

如果我正确理解您的请求,您希望您的自动化系统+所有开发工作都指向在您的旧计算机上运行的同一个 MySQL 数据库。如果是这样,您只需在从新计算机连接到旧计算机的 IP 地址时匹配

host
参数即可。

您必须更改服务器以不假设 MySQL 数据库位于 127.0.0.1 IP 上。例如添加ENV环境变量:

services:
  server:
    build:
      context: .
    ports:
      - 8000:8000
    environment:
      - SQL_HOST=${SQL_HOST}
      - SQL_PASS=/run/secrets/db-password
      - MYSQL_ROOT_PASSWORD=/run/secrets/db-password
      - TWILIO_ACCOUNT_SID=/run/secrets/twilio-sid
      - TWILIO_AUTH_TOKEN=/run/secrets/twilio-token
    depends_on:
      db:
        condition: service_healthy
    secrets:
      - db-password
      - twilio-sid
      - twilio-token

您现在可以在新旧计算机上使用不同的 SQL_HOST 环境变量值创建不同的 .env 文件,然后可以在 Flask 应用程序中使用该文件。

© www.soinside.com 2019 - 2024. All rights reserved.