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 local:python3.10ubuntu for it. When I ran the container for it with the command docker run --name py3.10ubuntu local:python3.10ubuntu I can see that the docker container was not running why is that and how would I be able to run it. when I try to see the docker logs for the container it wouldn’t show anything as well. Besides starting the container how would I be able to run the command shell within the container and run python files? Is this a good practice is there a better way to go about this?

Directory Structure:

└── Dockerfile

Dockerfile contents

FROM python:3.10-slim
RUN pip install --no-cache-dir poetry

Cant Dos:

  • Get container to run.

  • Get into the python shell and run commands

Containers are not virtual machines. They will not run unless you run a command inside. You did not provide any command that would run indefinitely in the foreground, so the container runs the default command which is python, but there is no parameter, no python script to run, so it stops immediately. If you want to keep it running, you either need to define a command that would keep the container running, or you need to ask for an interactive terminal using the -i and -t flags which you can use together like -it:

docker run --name py3.10ubuntu local:python3.10ubuntu

It will run, until you use exit() in the container. After that, the container will not be removed. Use this if you want to remove it after exiting:

docker run --rm --name py3.10ubuntu local:python3.10ubuntu

or use the -d flag to run it in detached mode in the background, but you should also run a python program in the container instead of just running a python shell.