-2

I have been successful building an image for a simple python script that is part of the working directory as follows:

enter image description here

My DockerFile looks as below:

# Using Python runtime 3.10
FROM python:3.10

# Set the working directory of the container
WORKDIR /src

# Copy the project directory to the container
COPY . /src/

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Command to run the application
CMD [ "python", "./anonymizer/app/main.py" ]

I then build the image successfully -

PS C:\Python Test> docker build -t anonymizer -f DockerFile .

enter image description here

And when I run the image interactively -

PS C:\Python Test> docker run -it anonymizer /bin/bash

It seems to have run successfully too

enter image description here

However, the python script didn't seem to have run as the files the script were meant to create never got written into the data/* folders as below. On the contrary, when I run the same script as defined in CMD variable of DockerFile from the container, the files do get created as shown in the screenshot below.

enter image description here

Why isn't the CMD running the Python script?

Balajee Addanki
  • 690
  • 2
  • 9
  • 23
  • 2
    At first sight: just remove the `/bin/bash` argument in CLI. This argument replaces the `CMD` default program. – ErikMD May 19 '23 at 14:00
  • Does this answer your question? [How do I specify run arguments for a base image in a Dockerfile?](https://stackoverflow.com/questions/54634908/how-do-i-specify-run-arguments-for-a-base-image-in-a-dockerfile) – ErikMD May 19 '23 at 14:04
  • See also: [What is the difference between CMD and ENTRYPOINT in a Dockerfile?](https://stackoverflow.com/a/39408777/9164010) – ErikMD May 19 '23 at 14:05
  • 1
    (Reminder: [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551)) – David Maze May 19 '23 at 14:12

1 Answers1

1

By passing the last parameter to docker run, the /bin/bash, you're replacing whatever was set in the Dockerfile's CMD statement, and executing the passed command instead.

In your case, the script is not executing because you're attaching youself to a Bash terminal, and overwriting the Python command.

Try running the container in detached mode, and then checking the logs:

docker run -d --name anonymizer anonymizer
docker logs anonymizer

If the Python script is some kind of service or process, you can also attach your shell to a running container (if it's a one-off script, the container will be stopped by the time you try to attach):

docker exec -it anonymizer bash
ErikMD
  • 13,377
  • 3
  • 35
  • 71