Going by question title, and if one doesn't want to create docker image but just want to run a script using standard python docker images, it can run using below command

docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3.7-alpine python script_to_run.py
Answer from Shambu on Stack Overflow
Top answer
1 of 7
48

Going by question title, and if one doesn't want to create docker image but just want to run a script using standard python docker images, it can run using below command

docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3.7-alpine python script_to_run.py
2 of 7
34

Alright, first create a specific project directory for your docker image. For example:

mkdir /home/pi/Desktop/teasr/capturing

Copy your dockerfile and script in there and change the current context to this directory.

cp /home/pi/Desktop/teasr/capturing.py /home/pi/Desktop/teasr/dockerfile /home/pi/Desktop/teasr/capturing/

cd /home/pi/Desktop/teasr/capturing

This is for best practice, as the first thing the docker-engine does on build, is read the whole current context.

Next we'll take a look at your dockerfile. It should look something like this now:

FROM python:latest

WORKDIR /usr/local/bin

COPY capturing.py .

CMD ["capturing.py", "-OPTIONAL_FLAG"]

The next thing you need to do is build it with a smart name. Using dots is generally discouraged.

docker build -t pulkit/capturing:1.0 .

Next thing is to just run the image like you've done.

docker run -ti --name capturing pulkit/capturing:1.0

The script now get executed inside the container and will probably exit upon completion.

Edit after finding the problem that created the following error:

standard_init_linux.go:195: exec user process caused "exec format error"

There's a different architecture beneath raspberry pi's (ARM instead of x86_64), which COULD'VE BEEN the problem, but wasn't. If that would've been the problem, a switch of the parent image to FROM armhf/python would've been enough.

Source

BUT! The error kept occurring.

So the solution to this problem is a simple missing Sha-Bang on top of the python script. The first line in the script needs to be #!/usr/bin/env python and that should solve the problem.

Source

🌐
Docker
docker.com › blog › containerized-python-development-part-1
Containerized Python Development - Part 1 | Docker
May 24, 2024 - Let’s have a look next on how we can easily containerize this Python service. The way to get our Python code running in a container is to pack it as a Docker image and then run a container based on it.
Discussions

Running a simple Python Container in Docker
So I have a directory that just includes a Dockerfile. I want to experiment with the poetry package in python. So I do not want any python files to be inside it initially because I want to create a poetry project from scratch inside the directory. So ı went ahead and built the image called ... More on forums.docker.com
🌐 forums.docker.com
1
0
September 3, 2022
How to run a Python script IN LINUX IN DOCKER?
easiest way is to pull and run the Python image that has python installed already. Otherwise you need a Dockerfile to install Python on top of Ubuntu. Reading a Docker tutorial might help. Crawl before you walk. More on reddit.com
🌐 r/docker
4
0
September 16, 2023
How to run python code in Docker
Very simple question, but surprisingly I couldn’t find answer for hours. This is because most tutorials show how to run python code like: print(“Hello World!”). These codes require few or no packages. But there is no good tutorial that show how to run python codes that require many packages. More on forums.docker.com
🌐 forums.docker.com
3
0
June 6, 2025
How to run python script in docker container? - Stack Overflow
For some reason unknown to me section CMD does not work, but if I run the script by going into the container, it works. FROM ubuntu:20.04 RUN mkdir /home/bob WORKDIR /home/bob RUN apt-get install -y More on stackoverflow.com
🌐 stackoverflow.com
🌐
Docker Community
forums.docker.com › docker desktop
How i can run python script from Docker container - Docker Desktop - Docker Community Forums
May 13, 2021 - I am newbie to Docker and trying to run python script in Docker container. My docker file: escape=` Let us use PowerShell line continuation. #FROM Microsoft Artifact Registry #FROM python:3-windowsservercore FROM python:3 Restore the default Windows shell for correct batch processing.
🌐
Docker Community
forums.docker.com › general
Running a simple Python Container in Docker - General - Docker Community Forums
September 3, 2022 - So I have a directory that just includes a Dockerfile. I want to experiment with the poetry package in python. So I do not want any python files to be inside it initially because I want to create a poetry project from scratch inside the directory. So ı went ahead and built the image called local:python3.10ubuntu for it.
🌐
dockerlabs
dockerlabs.collabnix.com › beginners › dockerfile › lab_dockerfile_python.html
Writing Dockerfile with Hello Python Script Added | dockerlabs
[node1] (local) root@192.168.0.38 /test $ docker run --name test1 pythonimage Hello World [node1] (local) root@192.168.0.38 /test $ So, here /home/hello.py file placed in the container executed and displayed the output ‘Hello World’, since it was specified as default with CMD keyword.
🌐
Reddit
reddit.com › r/docker › how to run a python script in linux in docker?
r/docker on Reddit: How to run a Python script IN LINUX IN DOCKER?
September 16, 2023 -

All the tutorial I get are for "running Python in Docker" or "making a Linux Docker container", but what should the dockerfile look like if I want to run a Python script, as if on a Linux machine, via a Docker container??

ELI5 new to docker. Whenever I pull the Ubuntu image from Docker, starting a container seems to do nothing per the Terminal. How do you "run stuff" once you've gotten your hands on the Ubuntu image? How do you "edit the dockerfile" to include commands such as CMD ["python", "myfile.py"]?

Generalized, my question can be: how do you incrementally add commands to an image/dockerfile that you pulled as opposed to hand-wrote from scratch (or Docker init)?

🌐
GeeksforGeeks
geeksforgeeks.org › how-to-run-a-python-script-using-docker
How to Run a Python Script using Docker? - GeeksforGeeks
May 3, 2025 - List all required Python packages in a requirements.txt file. Make sure your Dockerfile includes a line to install these dependencies: RUN pip install --no-cache-dir -r requirements.txt · Confirm that the requirements.txt file is properly copied into the container using the COPY command.
Find elsewhere
🌐
LabEx
labex.io › tutorials › docker-how-to-run-a-python-script-when-a-docker-container-starts-410107
How to run a Python script when a Docker container starts | LabEx
CMD ["python", "app.py"]: This specifies the command to run when the container starts. In this case, it will run our Python script. Now, let's build our Docker image using this Dockerfile:
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › quickstart-python
Python in a container
November 3, 2021 - Create a Dockerfile file describing a simple Python container. Build, run, and verify the functionality of a Django, Flask, or General Python app.
🌐
TechGeekBuzz
techgeekbuzz.com › blog › how-to-run-python-scripts-inside-docker-containers
How to run Python Scripts inside Docker Containers? [Complete Guide]
Finally, we have used the CMD and ENTRYPOINT instructions to tell the daemon to run the Python script called test.py using the Python3 command when the container is started. Our directory structure is - Let’s use the Docker build command to build our Docker image.
🌐
Docker
docker.com › blog › how-to-dockerize-your-python-applications
How to “Dockerize” Your Python Applications | Docker
Lastly, you’ll enter the command that Docker will execute once your container has started: CMD [“python”, “./main.py”] # Or enter the name of your unique directory and parameter set.
Published   November 6, 2024
🌐
Icefire555
icefire555.com › home › software › python › how to run a python script in a docker container
How to run a python script in a docker container – Icefire's Notebook
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. ... 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 tag the docker file with the name and version of our repository.
🌐
Tim Santeford
timsanteford.com › posts › running-a-python-script-in-docker
Running a Python Script in Docker - Tim Santeford
In this post, I’ll walk you through the process of setting up, running, and managing Python scripts in Docker, step by step. ... By the end of this guide, you’ll feel confident executing Python scripts in a containerized environment.
🌐
Medium
medium.com › @pv.gomes89 › docker-running-python-code-without-copying-the-files-2cdfbb963ae1
Docker running Python code without copying the files | by Paulo Victor Gomes | Medium
July 14, 2024 - Write a Dockerfile: Use this simple Dockerfile to create a Python environment: FROM python:3.9 WORKDIR /usr/src/app CMD ["tail", "-f", "/dev/null"] docker run -d --name my-python-container -v /path/to/your/scripts:/usr/src/app my-python-runner
🌐
Dojo Five
dojofive.com › home › testing python scripts in a docker container
Testing Python Scripts in A Docker Container - Dojo Five - Modern Embedded Firmware Development
April 25, 2025 - Therefore, while developing the script in the container, the local script will always be up-to-date. Bind mounts can also be used for pulling in drivers, third-party libraries, or any other helper resources needed by the script-under-test. % docker run -it --rm -v $(pwd)/<DIR>:/<DIR> python:3.13.0b1-slim-bullseye /bin/bash
Top answer
1 of 2
1

Write a script named start.sh like this:

python3 /home/bob/test1/bin/start.py &> /home/bob/test1/status.log

Then update your docker file as follow:

FROM ubuntu:20.04

RUN mkdir /home/bob
WORKDIR /home/bob

RUN apt-get install -y python3 python3-pip
RUN echo "pyodbc" > /tmp/req.txt
RUN pip3 install -r /tmp/req.txt

# Will attached to /home/bob/folder1 in container (folder0 contain files and folder with test1 name)

ADD ./folder0 ./folder1
RUN chmod ug+x /home/bob/test1

COPY start.sh .
RUN chmod a+x start.sh

CMD ./start.sh
2 of 2
1

Syntactically, CMD can have two forms. The exec form must be a JSON array, with individual words double-quoted; you are responsible for breaking the command into words, and you cannot use shell forms like redirection. The shell form does not have extra punctuation and implicitly wraps its command in sh -c, but can use all shell features. (These links are to ENTRYPOINT examples but the same syntax works for CMD and also RUN.) Your example has square brackets, like the exec form, but not double-quotes; you should just remove those.

Stylistically, remember that a Docker image has an isolated filesystem. You generally will want to build a working Python application on the host and then package it in a container; so use the requirements.txt file from running pip freeze on the host, rather than trying to rebuild it from scratch in the Dockerfile. Similarly, since the image is isolated, it doesn't usually make sense to have a "home directory", and since the container will also have an isolated filesystem, it doesn't make sense to capture the process's output to a file in the container that will be hard to read later.

I'd rewrite this Dockerfile as:

# Use the standard Docker Hub Python image, rather than installing
# Python on an Ubuntu image.  (You may have a specific requirement
# to use specific base images.)
FROM python:3.9

# Use a short directory name.  Don't make it look like a user's home
# directory.  This will automatically create the directory for you.
WORKDIR /app

# Install Python package dependencies.  The base image will already
# have pip installed.  Do this as a separate step, so that it can
# be cached on rebuild if the requirements.txt file hasn't changed.
COPY requirements.txt .
RUN pip install -r requirements.txt

# Copy the rest of the application in.  You can rename things here if
# you really need to, but it can lead to confusing differences between
# your development environment and the deployed application.
COPY . .

# The main script should be executable on the host system (run
# `chmod +x start.py`) and start with a shebang line
# #!/usr/bin/env python3
# so you should be able to just
CMD ./start.py
# without any redirections or any special punctuation

If you just docker run the built image, it will print its output to stdout; if you docker run -d the image in the background, docker logs will be able to retrieve its logs.

🌐
Medium
medium.com › swlh › dockerize-your-python-command-line-program-6a273f5c5544
Dockerize Your Python Command-Line Program | by Piotr Maślewski | The Startup | Medium
October 26, 2020 - As you will see in the next paragraph, we will run this Docker image in interactive mode with pseudo-tty attached. The latter is actually required by bash, without it the shell process would exit immediately and the container would exit as well. This setup will allow us to run our script directly from the container and also will keep our container running as long as the bash is active. ... FROM python:3.8-slimRUN useradd --create-home --shell /bin/bash app_userWORKDIR /home/app_userCOPY requirements.txt ./RUN pip install --no-cache-dir -r requirements.txtUSER app_userCOPY .
🌐
Runnable
runnable.com › docker guides › python › dockerize your python application
Dockerize your Python Application | Runnable Docker Guides
July 19, 2016 - Create a new text file in my_new_docker_build called Dockerfile (note no extension; on Windows, you may need to save the file as “All types” and put the filename in quotes to avoid automatically appending an extension); use whatever text file editor you already know (you might use Sublime, ...
🌐
Reddit
reddit.com › r/synology › how to run a python script inside a docker container on synology
r/synology on Reddit: How to run a python script inside a docker container on synology
April 11, 2024 -

I need some help with synology and docker. Maybe some of the pros can help me out here. I cant get it to work.

I want to run a python script on docker which should keep on running over several day.

I already have a docker container with python and all packages I need installed. The only thing I can't wrap my head around is how I start my python file without using a terminal. If I start it via terminal and I close the windows the process stops... obviously.

I found out already that I have to achieve via the docker commands. With this I'm trying to start my python file when I start the container:

CMD ["python", "/my/folder/run.py", "--config", "/data/config.yml"]

There is unfortunatley not much going on... Any ideas?

Thanks!