Help with Dockerfile (Image for running Python file)

So, I’ve wrote this dockerfile and it does the bare minimum. It allows a python coded discord bot to be run inside a docker container.

FROM python:3.10
ADD bot.py .
RUN pip install --upgrade pip
RUN pip install discord {other packages}
CMD [ "python", "./bot.py" ]

However, whenever I want to make changes to the code of the discord bot, I have to come back to this dockerfile and rebuild the image, then go into docker and build a new container from the newly rebuilt image. In an Ideal world, it would be great if all I had to do was replace the python file and, if needed, alter another file or an enviroment variable to add any new packages.

Problem is, I’m very new to this and I have very little idea how it would be done.

Anyone able to point me in the right direction?

Hello

For this use, i.e. synchronize files during development, the easy way is to mount your current folder in your container.

If you are using “docker run” to create your container, use the “-v” flag like “-v $pwd:/.”.

If you are using Docker compose, see the “volumes:” entry in the yaml file.

By mounting files like that, changes made on your host will be synchronized (in both directions).

@cavo789
so, like this?

services:
  bot:
    container_name: "discordbot"
    build:
      context:.
      dockerfile: dockerfile
    volumes:
      - /mnt/{filepath}/discordbot/:/app
    restart: unless-stopped

Something like that yes but are you sure about your paths ?

The left side is your current folder (on your host). If the yaml file is in the same folder of discordbot, just put
volumes:
- ./:/app

And for the second path (the right side), this didn’t sound good with your initial post (In your dockerfile you don’t use /path)

@cavo789

So for left: I’m going to be running this application on TrueNAS as a custom app, so I can’t really ensure that the the yaml file is in the same folder as the discordbot and dockerfile.

As for the right: yeah, that is a misunderstanding on my part. Should it just be “/.”?

      - /mnt/{filepath}/discordbot/:/.

To be more precise, you referred to bind mounting. There is a “synchronized file share” feature which is different and that is for Docker Desktop.

As Christophe correctly wrote

the right side is the path in the container and using the relative path was suggested only for the left side. If you can’t use the relative path, that’s okay. The absolute path will work too for mounting folders into containers. The context matters only when building images and copying files into the image. The relative path is just preferred in general when working locally.

1 Like

Worked it out in the end via trial and error, thanks very much for your help.

For anyone who is interested, my solution is here:
https://linustechtips.com/topic/1617815-help-with-dockerfile-image-for-running-python-file

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.