Search code examples
pythondockerdocker-composepip

network issues in dockerfile while accessing database


I have an small python backend and a mariaDB. Deperated in Docker Services.

The docker-compse looks like this:

version: '3.5'

networks:
  web:
    name: web
    external: true
  wsm:
    name: wsm
    internal: true
volumes:
  wsm-partsfinder-db:
    name: wsm-partsfinder-db

services:
  wsmbackend:
    build:
      context: .
      dockerfile: ./docker/Dockerfile
    container_name: wsm-file-parts-backend
    restart: always
    depends_on:
      - wsmdb
    ports:
      - "8888:8888"
    networks:
      - web
      - wsm
  wsmdb:
    container_name: wsmdb
    image: mariadb:10.7.1
    command: --default-authentication-plugin=mariadb_native_password
    restart: unless-stopped
    environment:
      MARIADB_ROOT_PASSWORD: password
      MARIADB_USER: wsm
      MARIADB_PASSWORD: password
      MARIADB_DATABASE: wsm_parts
    volumes:
      - wsm-partsfinder-db:/var/lib/mysql
    networks:
      - wsm
    ports:
      - "4485:3306"

The Dockerfile which is called for wsmbackend service looks like this:

FROM python

RUN apt-get update -y
RUN apt-get upgrade -y

COPY . /wsm
WORKDIR /wsm

RUN pip install -r requirements.txt

RUN yoyo apply --database mysql://wsm:password@wsmdb:4485/wsm_parts ./migrations

EXPOSE 8888
CMD ["/bin/sh", "-c", "python main.py"]

I got an error in yoyo apply ...

enter image description here

What is the issue in this case? Thanks in advance


Solution

  • You are not able to run queries on the database in your build stage, because your database container not is started at that current point.

    The RUN statement is only executed in the build stage. You need to move it to the CMD (entrypoint), so it's executed when the container and the database is started.