Jump to content

Help with Dockerfile (Image for running Python file)

Go to solution Solved by Kilrah,

Mount an external folder (or single file) to the container.

 

e.g. don't do "ADD bot.py ." in the Dockerfile, but when you run the container add a

 

-v /hostpath/to/bot.py:/innerpath/bot.py

 

with

CMD [ "python", "/innerpath/bot.py" ]

 

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?

I might be experienced, but I'm human and I do make mistakes. Trust but Verify! I edit my messages after sending them alot, please refresh before posting your reply. Please try to be clear and specific, you'll get a better answer. Please remember to mark solutions once you have the information you need. Expand this signature for common PC building advice, a short bio and a list of my components.

 

Common build advice:

1) Buy the cheapest (well reviewed) motherboard that has the features you need. Paying more typically only gets you features you won’t use. 2) only get as much RAM as you need, getting more won’t (typically) make your PC faster. 3) While I recommend getting an NVMe drive, you don’t need to splurge for an expensive drive with DRam cache, DRamless drives are fine for gamers. 4) paying for looks is fine, just don’t break the bank. 5) Tower coolers are usually good enough, unless you go top tier Intel or plan on OCing. 6) OCing is a dead meme, you probably shouldn’t bother. 7) "Bottlenecks" rarely matter and "Future-proofing" is a myth. 8) AIOs don't noticeably improve performance past 240mm and don't improve at all past 360mm. 9) RTFM.

 

Useful Websites:

https://www.productchart.com - helps compare monitors, https://uk.pcpartpicker.com - makes designing a PC easier.

 

Bio:

He/Him - I'm a PhD student working in the fields of reinforcement learning and traffic control. PCs are one of my hobbies and I've built many PCs and performed upgrades on a few laptops (for myself, friends and family). My personal computers include 4 windows (10/11) machines and a TrueNAS server (and I'm looking to move to dual booting Linux Mint on my main machine in future). Aside from computers, I also dabble in modding/homebrew retro consoles, support Southampton FC, and enjoy Scuba Diving and Skiing.

Fun Facts

1) When I was 3 years old my favourite toy was a scientific calculator. 2) My father is a British Champion ploughman in the Vintage Hydraulic Class. 3) On Speedrun.com, I'm the world record holder for the Dream Bobsleigh event on Mario & Sonic at the Olympic Winter Games 2010.

 

My Favourite Games: World of Tanks, Runescape, Subnautica, Metroid (Fusion and Dread), Spyro: Year of the Dragon (Original and Reignited Trilogy), Crash Bash, Mario Kart Wii, Balatro

 

My Computers: Primary: My main gaming rig - https://uk.pcpartpicker.com/user/will0hlep/saved/NByp3C Second: Hosts Discord bots as well as a Minecraft and Ark server, and also serves as a reinforcement learning sand box - https://uk.pcpartpicker.com/user/will0hlep/saved/cc9K7P NAS: TrueNAS Scale NAS hosting SMB shares, DDNS updater, pi-hole, and a Jellyfin server - https://uk.pcpartpicker.com/user/will0hlep/saved/m37w3C Foldatron: My folding@home and BOINC rig (partially donated to me by Folding Team Leader GOTSpectrum) - Mobile: Mini-ITX gaming rig for when I'm away from home -

Link to post
Share on other sites

Docker Compose would be great for this
compose.yaml

services:
	bot:
    	build:
        	context: .
            dockerfile: Dockerfile

(indentation is wrong here, I can't get it quite right but just Google it)


This command builds and deploys the image & container

docker compose up --build


This command takes it down

docker compose down

 

Link to post
Share on other sites

1 hour ago, Dr. Will0hlep said:

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?

First, it'd be nice if you gave a read at how a dockerfile works, specially when it comes to layers and caching. This is a great resource with an overview:

https://docs.docker.com/build/cache/

 

A container is immutable, so once it's built, you should not edit it. With that said, what's taking place is that, given how your "bot.py" file is on the first line, it will throw away most of the cache and rebuild everything from that point onwards.

 

First of all, it'd be nice for you to have at least a "requirements.txt" file, with your dependencies and their versions stored in there. This way you could first copy this, and then install the required dependencies.

Let's say you now have such file, with the following contents:

discord.py==2.5.2
requests==2.32.4

 

You could have your Dockerfile looking like so:

FROM python:3.10
RUN pip install --upgrade pip

# The below will only copy and install your dependencies
COPY requirements.txt ./requirements.txt
# Will create a cached layer with your installed dependencies. If no changes happen to the requirements.txt file, this will just be reused in other build calls
RUN pip install -r requirements.txt

# Copy only your actual code. Any changes that you do to this file will only update this layer, while re-using the above ones that already exist
COPY bot.py ./bot.py

CMD [ "python", "./bot.py" ]

 

It will still require you to rebuild the image and re-deploy it, but that's exactly how it's meant to be done.

You could either automate this process with some sort of CI/CD pipeline, or you could just not use Docker and use something else instead, like a VM or just running your code straight out of a venv.

 

1 hour ago, AwesomeBFM said:

Docker Compose would be great for this

I don't think compose is a good suggestion, specially when we're talking about a single container.

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to post
Share on other sites

Mount an external folder (or single file) to the container.

 

e.g. don't do "ADD bot.py ." in the Dockerfile, but when you run the container add a

 

-v /hostpath/to/bot.py:/innerpath/bot.py

 

with

CMD [ "python", "/innerpath/bot.py" ]

 

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to post
Share on other sites

On 7/15/2025 at 9:25 PM, Kilrah said:

Mount an external folder (or single file) to the container.

e.g. don't do "ADD bot.py ." in the Dockerfile, but when you run the container add a

-v /hostpath/to/bot.py:/innerpath/bot.py

with

CMD [ "python", "/innerpath/bot.py" ]

After a bit of fiddeling, practice, and trial and error, I've ended up with this yaml script:

services:
  inglewood:
    build:
      context: /mnt/Pool/Storage/Inglewood/
      dockerfile: Dockerfile
    container_name: inglewood
    environment:
      - PYTHONUNBUFFERED=1
    restart: unless-stopped
    volumes:
      - /mnt/Pool/Storage/Inglewood:/app

which I can use to create a custom app on TrueNAS Scale. It uses this dockerfile:

FROM python:3.10
RUN pip install --upgrade pip
RUN pip install discord mcstatus pytz
CMD [ "python3", "/app/inglewood.py" ]

,which is saved at "/mnt/Pool/Storage/Inglewood/Dockerfile", to create a container that runs my discord bot, or any python script saved at "/mnt/Pool/Storage/Inglewood/inglewood.py".

 

This achieves exactly what I wanted. In order to alter the behaviour of my discord bot, I now only need to change the python script (and maybe the dockerfile if I need to add new packages) and then restart the app in the TrueNAS Scale web UI. Perfect.

 

image.png.00ef975eca402bd8edf94dd3a5b3784c.png

I might be experienced, but I'm human and I do make mistakes. Trust but Verify! I edit my messages after sending them alot, please refresh before posting your reply. Please try to be clear and specific, you'll get a better answer. Please remember to mark solutions once you have the information you need. Expand this signature for common PC building advice, a short bio and a list of my components.

 

Common build advice:

1) Buy the cheapest (well reviewed) motherboard that has the features you need. Paying more typically only gets you features you won’t use. 2) only get as much RAM as you need, getting more won’t (typically) make your PC faster. 3) While I recommend getting an NVMe drive, you don’t need to splurge for an expensive drive with DRam cache, DRamless drives are fine for gamers. 4) paying for looks is fine, just don’t break the bank. 5) Tower coolers are usually good enough, unless you go top tier Intel or plan on OCing. 6) OCing is a dead meme, you probably shouldn’t bother. 7) "Bottlenecks" rarely matter and "Future-proofing" is a myth. 8) AIOs don't noticeably improve performance past 240mm and don't improve at all past 360mm. 9) RTFM.

 

Useful Websites:

https://www.productchart.com - helps compare monitors, https://uk.pcpartpicker.com - makes designing a PC easier.

 

Bio:

He/Him - I'm a PhD student working in the fields of reinforcement learning and traffic control. PCs are one of my hobbies and I've built many PCs and performed upgrades on a few laptops (for myself, friends and family). My personal computers include 4 windows (10/11) machines and a TrueNAS server (and I'm looking to move to dual booting Linux Mint on my main machine in future). Aside from computers, I also dabble in modding/homebrew retro consoles, support Southampton FC, and enjoy Scuba Diving and Skiing.

Fun Facts

1) When I was 3 years old my favourite toy was a scientific calculator. 2) My father is a British Champion ploughman in the Vintage Hydraulic Class. 3) On Speedrun.com, I'm the world record holder for the Dream Bobsleigh event on Mario & Sonic at the Olympic Winter Games 2010.

 

My Favourite Games: World of Tanks, Runescape, Subnautica, Metroid (Fusion and Dread), Spyro: Year of the Dragon (Original and Reignited Trilogy), Crash Bash, Mario Kart Wii, Balatro

 

My Computers: Primary: My main gaming rig - https://uk.pcpartpicker.com/user/will0hlep/saved/NByp3C Second: Hosts Discord bots as well as a Minecraft and Ark server, and also serves as a reinforcement learning sand box - https://uk.pcpartpicker.com/user/will0hlep/saved/cc9K7P NAS: TrueNAS Scale NAS hosting SMB shares, DDNS updater, pi-hole, and a Jellyfin server - https://uk.pcpartpicker.com/user/will0hlep/saved/m37w3C Foldatron: My folding@home and BOINC rig (partially donated to me by Folding Team Leader GOTSpectrum) - Mobile: Mini-ITX gaming rig for when I'm away from home -

Link to post
Share on other sites

I might be experienced, but I'm human and I do make mistakes. Trust but Verify! I edit my messages after sending them alot, please refresh before posting your reply. Please try to be clear and specific, you'll get a better answer. Please remember to mark solutions once you have the information you need. Expand this signature for common PC building advice, a short bio and a list of my components.

 

Common build advice:

1) Buy the cheapest (well reviewed) motherboard that has the features you need. Paying more typically only gets you features you won’t use. 2) only get as much RAM as you need, getting more won’t (typically) make your PC faster. 3) While I recommend getting an NVMe drive, you don’t need to splurge for an expensive drive with DRam cache, DRamless drives are fine for gamers. 4) paying for looks is fine, just don’t break the bank. 5) Tower coolers are usually good enough, unless you go top tier Intel or plan on OCing. 6) OCing is a dead meme, you probably shouldn’t bother. 7) "Bottlenecks" rarely matter and "Future-proofing" is a myth. 8) AIOs don't noticeably improve performance past 240mm and don't improve at all past 360mm. 9) RTFM.

 

Useful Websites:

https://www.productchart.com - helps compare monitors, https://uk.pcpartpicker.com - makes designing a PC easier.

 

Bio:

He/Him - I'm a PhD student working in the fields of reinforcement learning and traffic control. PCs are one of my hobbies and I've built many PCs and performed upgrades on a few laptops (for myself, friends and family). My personal computers include 4 windows (10/11) machines and a TrueNAS server (and I'm looking to move to dual booting Linux Mint on my main machine in future). Aside from computers, I also dabble in modding/homebrew retro consoles, support Southampton FC, and enjoy Scuba Diving and Skiing.

Fun Facts

1) When I was 3 years old my favourite toy was a scientific calculator. 2) My father is a British Champion ploughman in the Vintage Hydraulic Class. 3) On Speedrun.com, I'm the world record holder for the Dream Bobsleigh event on Mario & Sonic at the Olympic Winter Games 2010.

 

My Favourite Games: World of Tanks, Runescape, Subnautica, Metroid (Fusion and Dread), Spyro: Year of the Dragon (Original and Reignited Trilogy), Crash Bash, Mario Kart Wii, Balatro

 

My Computers: Primary: My main gaming rig - https://uk.pcpartpicker.com/user/will0hlep/saved/NByp3C Second: Hosts Discord bots as well as a Minecraft and Ark server, and also serves as a reinforcement learning sand box - https://uk.pcpartpicker.com/user/will0hlep/saved/cc9K7P NAS: TrueNAS Scale NAS hosting SMB shares, DDNS updater, pi-hole, and a Jellyfin server - https://uk.pcpartpicker.com/user/will0hlep/saved/m37w3C Foldatron: My folding@home and BOINC rig (partially donated to me by Folding Team Leader GOTSpectrum) - Mobile: Mini-ITX gaming rig for when I'm away from home -

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×