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 OverflowGoing 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
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
Running a simple Python Container in Docker
How to run a Python script IN LINUX IN DOCKER?
How to run python code in Docker
How to run python script in docker container? - Stack Overflow
Videos
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)?
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
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.
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!