Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Creating docker container from a python file

Featured Replies

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 :)

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

  • Author
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!

  • Community Expert

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

  • Author

Thanks! I've gone ahead and added those lines for wordir and copy but it still seems to be not working and giving me the same error :( Hope I'm getting closer though!image.png.6181f6894b8b74529f4796615bd1c076.png

image.png.3d674102cd1a68376a746c26a968aa41.png

  • Author

I got it working! instead of running docker build -t discordbot -f PathToFile I navigated to the directory of the file and then run docker build there and that allowed everything to work correctly :)

  • Community Expert

On nitpick, while you can copy everything from the build context into your image like you are above, it might be cleaner to COPY only the bot.py script.

  • 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

  • 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?

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...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.