如何构建Django应用程序的Docker映像(没有Docker组成)并使用现有的MySQL容器

发布于 2025-01-23 14:13:02 字数 1753 浏览 2 评论 0原文

我在Django中有一个使用MySQL数据库的“销售点”应用程序。
我遵循了本Docker指南: python入门指南

为了设置mysql容器我创建了几卷及其网络:

docker volume create mysql
docker volume create mysql_config

docker network create mysqlnet

我的dockerfile看起来像这样:
(我不想使用docker-compose,但因为我想学习基础)

dockerfile

# syntax=docker/dockerfile:1

FROM python:3.8-slim-buster

RUN apt update
RUN apt upgrade -y
RUN apt dist-upgrade
RUN apt-get install procps -y
RUN apt install curl -y
RUN apt install net-tools -y

WORKDIR /home/pos

COPY requirements.txt ./.

RUN pip3 install -r requirements.txt

COPY ./src ./src

CMD ["python", "./src/manage.py", "runserver", "0.0.0.0:8000"]

而在django项目中,我的数据库设置和要求看起来像:

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'point-of-sales',
        'USER': 'root',
        'PASSWORD': 'r00t',
        'HOST': 'localhost',
        'PORT': '3307'
    }
}

要求.txt

# Django
django>=3.1,<3.2

# DRF
djangorestframework>=3.12,<3.13

# Databases
mysqlclient

我想要的是构建我可以运行并将其附加到MySQL网络的Django应用程序的图像。

问题是我无法构建我的django应用的图像,因为它在尝试安装mysqlclient时会引发以下错误:

OSError: mysql_config not found

环顾四周,我找到了另一个主题建议安装mysql-config,这是另一个软件包,但我想保持所有mysql隔离的所有内容在MySQL容器中。

没有使用Docker-Compose可以做到这一点?

I have a "point of sales" application in Django that uses a mysql database.
I followed this Docker guide: Python getting started guide

In order to setup the mysql container I created a couple of volumes and its network:

docker volume create mysql
docker volume create mysql_config

docker network create mysqlnet

My Dockerfile looks like this:
(I don't want to use docker-compose yet becouse I want to learn the bases)

Dockerfile

# syntax=docker/dockerfile:1

FROM python:3.8-slim-buster

RUN apt update
RUN apt upgrade -y
RUN apt dist-upgrade
RUN apt-get install procps -y
RUN apt install curl -y
RUN apt install net-tools -y

WORKDIR /home/pos

COPY requirements.txt ./.

RUN pip3 install -r requirements.txt

COPY ./src ./src

CMD ["python", "./src/manage.py", "runserver", "0.0.0.0:8000"]

And in the Django project my database settings and requirements looks like:

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'point-of-sales',
        'USER': 'root',
        'PASSWORD': 'r00t',
        'HOST': 'localhost',
        'PORT': '3307'
    }
}

requirements.txt

# Django
django>=3.1,<3.2

# DRF
djangorestframework>=3.12,<3.13

# Databases
mysqlclient

What I want is to build an image of the Django application that I could run and attach to the mysql network.

The problem is that I can't build the image of my django app becouse it throws the following error when trying to install mysqlclient:

OSError: mysql_config not found

Looking around I found another topic that suggest to install mysql-config which is another package but I want to kept everything of mysql isolated in the mysql container.

Is there any way to do that without using docker-compose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文