WORKDIR instruction in Dockerfile has no effect when containerize a Python app

  docker, python

I have the following Dockerfile to containerize a Python app:

FROM python:3.8-slim
WORKDIR /app
COPY ./simple_http_server.py .
CMD [ "python", "./simple_http_server.py" ]

I run with the following command:

docker run -d -p 5000:8000 simple_http_server

It runs fine. docker ps -a shows the container is up for n seconds and container id of xyz. Then I attach to the container to inspect inside the container using the command

docker exec -i  -t xyz /bin/bash

Once inside the container, doing a pwd command reveals it’s inside the /code directory.

[email protected]:/code# pwd
/code

In fact, the WORKDIR /app instruction above seems to have no effect at all. I don’t see /app anywhere.

What is the explanation of why WORKDIR has no effect and how it can be fixed?

Source: Docker Questions

LEAVE A COMMENT