Home > Software design >  Python docker container shuts down immediately after finishing running the app, even if specified to
Python docker container shuts down immediately after finishing running the app, even if specified to

Time:09-23

I have a dockerfile

FROM python:3

WORKDIR /app

ADD ./venv ./venv

ADD ./data/file1.csv.gz ./data/file1.csv.gz

ADD ./data/file2.csv.gz ./data/file2.csv.gz

ADD ./requirements.txt ./venv/requirements.txt

WORKDIR /app/venv

RUN pip install --no-cache-dir -r requirements.txt

CMD ["python", "./src/script.py", "/app/data/file1.csv.gz", "/app/data/file2.csv.gz"]

After building an image from it and running it, the image runs the app as it should, but then the container shuts down immediately after finishing. This is definitely problematic since I can't expect the output file.

I have tried using docker run -d -t <imgname> and docker ps shows the app for a few seconds, but once again, as soon as it finishes the process, the container shuts itself down.

So it's impossible to access, even with docker exec <imgid> -it --entrypoint /bin/bash, it just immediately exits.

I've also tried adding a last RUN /bin/bash after the last CMD but it doesn't help either.

What can I do actually be able to log into the container and inspect the file?

CodePudding user response:

As long as the container hasen't been removed, you will be able to get at the data. You can find the name of the container using docker ps -a.

Then, if you know the location of the file, you can copy it to your host using

docker cp <container name>:<file> .

Alternatively, you can commit the contents of the container to a new image and run a shell in that using

docker commit <container name> newimagename
docker run --rm -it newimagename /bin/bash

Then you can look around in the container and find your files.

Unfortunately there's no way to start the container up again and look around in it. docker start will start the container, but will run the same command again as was run when you did docker run.

  • Related