Creating docker container from a python file


BobFish

Recommended Posts

Hi Everyone, super new to unraid and docker but loving it all so far. I've built a discord bot in python which I'd like to run on my unraid server as a docker container. How do I go about doing this in the easiest way? I have been trying to research online on how to do this but not really found any instructions that walked me through it step by step.

 

Thanks :)

Link to comment

Hey,

 

thats very easy. 

You create a new container, give it a name and use the source python:3 volume mount your source code folder to /source/ and enter post execution "python3 /source/main.py"

if you have some requirements you have to install those with docker exec and commit the container after you made changes.

 

cheers

Link to comment
5 hours ago, knex666 said:

Hey,

 

thats very easy. 

You create a new container, give it a name and use the source python:3 volume mount your source code folder to /source/ and enter post execution "python3 /source/main.py"

if you have some requirements you have to install those with docker exec and commit the container after you made changes.

 

cheers

 

Thanks for the reply! So I think I'm almost there. I've made a folder here on my unraid server with my dockerfile and my python fileimage.png.5cbcf2d05e959205e807afbe157a6ccc.png

 

And then here is my dockerfile:

image.png.cbfa080d710eb0ad1afedce03307ae13.png

 

I then run sudo docker build -t discord -f /mnt/user/Media/discordbot/dockerfile . in my terminal on unraid

 

which if I go docker images gives me this:

image.png.399b7ee35888cf05996b42b46faa4f8b.png

 

so I then should just go docker run discord right?

but this gives me this error:

image.png.6f656a60a1571b03b87f98113ae2545e.png

 

I think I need to copy the bot.py file somehow to where the docker image is stored or something like that? Very unsure what to do next or if what I have is correct. Thanks!

Link to comment

It depends on how you want to go about it. knex666's comment above suggested bind-mounting the your source code into the container (using a -v flag in docker run). That would make it easier to make changes to the source (bot.py) without recreating the container. On the other hand what you have above is most of what you need to build your bot.py directly into the container, which would make it easier to distribute. To finish building your code into the container you need to add a COPY line in you dockerfile to copy bot.py to a location within the filesystem of your image and a WORKDIR line to ensure your container begins running from the proper internal directory.

Edited by primeval_god
Link to comment
  • 1 month later...

If you're like me and ended up here just trying to get a .py script to run in a container with dependencies, this is how I got it working.

 

Add new cointainer, use the blank template

repository: python:latest

project page: https://hub.docker.com/_/python

icon url: https://d1q6f0aelx0por.cloudfront.net/product-logos/library-python-logo.png

post arguments:   /bin/bash /app/init.sh

map a volume from /mnt/user/appdata/python/this_script -> /app

(after you add a single py script, this will be saved as a template and you can just tack on a different directory like "that_script" and keep everything in one place)

 

Put your main.py file in the aforementioned directory, but you must accompany it with an init.sh script.  Here's what mine looks like. (Note that both scripts are executable, so chmod +x them):

 

Quote

root@blaaaaaarg:/mnt/user/appdata/python/this_script# ls -la
total 8
drwxrwxrwx 1 nobody users   28 Dec 17 19:44 ./
drwxrwxrwx 1 root   root    12 Dec 19 13:30 ../
-rwxrwxrwx 1 root   root   120 Dec 17 19:47 init.sh*
-rwxrwxrwx 1 root   root  1351 Dec 17 19:36 main.py*
root@blaaaaaarg:/mnt/user/appdata/python/crypto# 

 

root@blaaaaaarg:/mnt/user/appdata/python/this_script# cat init.sh

#!/bin/bash

/usr/local/bin/python -m pip install --upgrade pip
pip install requests
/usr/local/bin/python /app/main.py

 

This method is easily deployable for multiple scripts, and it was so obvious after slowly reading and re-reading @knex666 response over and over again. I don't think this is exactly what he meant, but this works for me.

Edited by lotekjunky
  • Like 2
Link to comment
  • 11 months later...
On 12/20/2021 at 6:24 AM, lotekjunky said:

Put your main.py file in the aforementioned directory, but you must accompany it with an init.sh script.  Here's what mine looks like. (Note that both scripts are executable, so chmod +x them):

 

Quote

root@blaaaaaarg:/mnt/user/appdata/python/this_script# ls -la
total 8
drwxrwxrwx 1 nobody users   28 Dec 17 19:44 ./
drwxrwxrwx 1 root   root    12 Dec 19 13:30 ../
-rwxrwxrwx 1 root   root   120 Dec 17 19:47 init.sh*
-rwxrwxrwx 1 root   root  1351 Dec 17 19:36 main.py*
root@blaaaaaarg:/mnt/user/appdata/python/crypto# 

 

root@blaaaaaarg:/mnt/user/appdata/python/this_script# cat init.sh

#!/bin/bash

/usr/local/bin/python -m pip install --upgrade pip
pip install requests
/usr/local/bin/python /app/main.py

 

Just stumbled upon this. Could you clarify this for me? Are these two separate scripts? Or is it one script that got separated by formatting?

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.