Search code examples
pythondockerdocker-composeyamlini

Dockerfile with docker-compose.yml


In general, I do have an understanding of the purposes behind using Dockerfile and docker-compose.yml. However, I am facing some issues with using both at the same time. Likely, I'm just not specifying all the locations correctly.

I built an app that takes a config.ini file as one of the inputs (it should be located in the same directory as my app.py code). My goal is to avoid re-building the image every time I made changes to the config file. I understand that I can do it by building an image and then running/restarting the container via docker-compose.yml.

Below is the structure of my project:

my_local_dir_with_py_projects/
|__config.ini
|__docker-compose.yml
|__my_py_app/
   |__app.py
   |__requirements.txt
   |__Dockerfile

My Dockerfile looks like this:

# syntax=docker/dockerfile:1

FROM python:3.10

WORKDIR app/

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

My docker-compose.yml looks like this:

version: '2.4'
services:
  py_app:
    image: py_app:latest
    restart: always
    volumes:
      - "./config.ini:/Users/my_user/my_local_dir_with_py_projects/config.ini:z" 
    ports:
      - "5005:5000"
    command: python3 app.py

I have built an image via docker build --tag py-app . being in /Users/my_user/my_local_dir_with_py_projects/py_app. I then cd /Users/my_user/my_local_dir_with_py_projects/ and running my .yml file via docker-compose -f docker-compose.yml up.

Thank you for your help.

Edit: Just to clarify the issue. I am getting the following error:

py_app_1  | Traceback (most recent call last):
py_app_1  |   File "/usr/local/lib/python3.10/configparser.py", line 790, in get
py_app_1  |     value = d[option]
File "/usr/local/lib/python3.10/collections/__init__.py", line 986, in __getitem__
py_app_1  |     return self.__missing__(key)            # support subclasses that define __missing__
py_app_1  |   File "/usr/local/lib/python3.10/collections/__init__.py", line 978, in __missing__
py_app_1  |     raise KeyError(key)
py_app_1  | KeyError: 'base_url'
...

A part of my config.ini file:

[DEFAULT]
base_url = https://api.binance.com
klines_url = /api/v1/klines

Which I am reading via:

import configparser 
config = configparser.ConfigParser()
config.read('config.ini')

Solution

  • In the Dockerfile, you're installing the application into /app. Your Compose setup mounts the config file into a /Users/... directory, but your application doesn't know anything about this directory; it's running inside /app and presumably is looking for the config.ini file there.

    So in your docker-compose.yml file, this line:

    volumes:
      - ./config.ini:/app/config.ini
      #              ^^^^
    

    needs to match this line in your Dockerfile:

    WORKDIR /app
    #       ^^^^
    

    (It's probably clearer to use an absolute path in the Dockerfile as I've shown. It's also better practice to put a CMD in the Dockerfile than to depend on a command: override showing up in the docker-compose.yml file.)