首页 > 解决方案 > How to run dockerized Django REST Framework backend with MariaDB?

问题描述

As in the title: I have troubles switching to MariaDB when I use Docker.

As long as I launched many different databases locally from my disk (default SQLite, PostgreSQL and MariaDB), every configuration worked fine. SQLite and PostgreSQL work with Docker as well (installation of Postgres' driver goes smoothly), but MariaDB reports issues with fetching MariaDB Connector/C.

Dockerfile:

FROM python:3.8.5-alpine3.12

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN apk add libmariadb3
RUN pip install -r /requirements.txt

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN adduser --disabled-password user
USER user

docker-compose.yml:

version: "3"

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command:
      sh -c "python manage.py runserver 0.0.0.0:8000"
    environment:
      [...]
    depends_on:
      - db

  db:
    image: mariadb:10.5.4
    environment:
      [...]

The error I'm getting is:

[...]
Step 4/11 : RUN apk add libmariadb3
 ---> Running in 5a3e568b8ac3
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
ERROR: unsatisfiable constraints:
  libmariadb3 (missing):
    required by: world[libmariadb3]
ERROR: Service 'app' failed to build: The command '/bin/sh -c apk add libmariadb3' returned a non-zero code: 1

According to documentation, MariaDB Connector/C can be installed with apt-get, yum, dnf, zypper, or from source, but apparently not with apk.

Considered solutions:

  1. Download MariaDB Connector/C tar.gz to the project directory and copy it while building image. Cons: one has to take care of updating the driver manually.
  2. Use a Python image run on Ubuntu. Cons: is it small enough? Is it still supported (not visible on the list in Docker's Hub)?

As I wrote above, these solutions don't seem to be satisfactory. Do you know other ways to configure MariaDB to work with Python?

标签: pythondockermariadb

解决方案


似乎python:3.9.0b5-buster已经为956MB

$ cat Dockerfile

FROM python:3.9.0b5-buster 
RUN apt-get update
RUN apt-get install libmariadb3

$ docker build -t py-booster-with-maria .

Step 3/3 : RUN apt-get install libmariadb3
 ---> Running in 40be8f94b3de
Reading package lists...
Building dependency tree...
Reading state information...
libmariadb3 is already the newest version (1:10.3.22-0+deb10u1).
libmariadb3 set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

docker image ls | grep py-booster-with-maria

py-booster-with-maria              latest                       e7c61439835f        7 minutes ago       956MB

推荐阅读