How to run a python script in a docker container

0

This will be a quick write up as there isn’t a ton to it surprisingly. We will not be using VScode as I personally don’t really like using it and I didn’t see an easy method to use Visual studio to do this.

Prerequisites

  • Docker Desktop
    • This will be doing the bulk of our lifting
  • A python script to put in a docker
  • An account and repository on https://hub.docker.com/

Setting things up

First things first, we need to open up command prompt and sign into our docker account. I was able to use “docker login” to do this. You may need to use “docker login -u <username> -p <password>” to do this.

Next thing we need to do it set up our directory.

  1. Create a Dockerfile file (don’t overthink this, just make a text file and rename it without .txt at the end)

Inside of this file we will add the following text.

We include the version of python we’re using, we will add a requirements file (This will be useful for dependencies in our script later), and we will tell it to copy our python file and run it. the (“-u”) is useful as it will make our python print statements print to a log so we can easily monitor our container.

FROM python:3.11-slim

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

COPY ff14.py /
CMD ["python","-u" ,"ff14.py"]

2. We need to create a requirements.txt file.
In side of this it will be incredibly simple. We just add each thing we need to install for our script to work on each line.

bs4
requests
datetime

3. We will create the UploadDocker.bat file.
This isn’t required but I’m lazy and it will make our life easier.
Inside my bat file I will have the following commands.

  • We build the docker file
  • We tag the docker file with the name and version of our repository. I’m using latest so it will be grabbed by unraid (my docker host of choice)
  • We push the docker file to docker hub to make it update our repository.
docker build -t ff14 .
docker tag ff14 icefire555/finalfantasy14congestiontracker:latest
docker push icefire555/finalfantasy14congestiontracker:latest

Final Result

We should have the following directory by this point (minus the ff14.dockerfile which generates when we run the bat file.)

Running the UploadDocker.bat should make a similar output to what is shown below. However without a “pause” at the end it will automatically close. When it closes, we know the docker has been built, tagged, and uploaded.

Looking over our logs in unraid we can see the following. which shows everything is working just how we want it to!

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *