DockerFile: Containerising python do not allow execute further commands

1/19/2021

This is my DockerFile

# set base image (host OS)
FROM python:3.8

# set the working directory in the container
WORKDIR /code

# command to run on container start
RUN mkdir -p /tmp/xyz-agent

And when I execute the following command - docker -v build . the docker builds successfully and I don't get any error. This is the output -

Step 1/3 : FROM python:3.8
3.8: Pulling from library/python
b9a857cbf04d: Already exists 
d557ee20540b: Already exists 
3b9ca4f00c2e: Already exists 
667fd949ed93: Already exists 
4ad46e8a18e5: Already exists 
381aea9d4031: Pull complete 
8a9e78e1993b: Pull complete 
9eff4cbaa677: Pull complete 
1addfed3cc19: Pull complete 
Digest: sha256:fe08f4b7948acd9dae63f6de0871f79afa017dfad32d148770ff3a05d3c64363
Status: Downloaded newer image for python:3.8
 ---> b0358f6298cd
Step 2/3 : WORKDIR /code
 ---> Running in 486aaa8f33ad
Removing intermediate container 486aaa8f33ad
 ---> b798192954bd
Step 3/3 : CMD ls
 ---> Running in 831ef6e6996b
Removing intermediate container 831ef6e6996b
 ---> 36298963bfa5
Successfully built 36298963bfa5

But when I login inside the container using terminal. I don't see the directory created. Same goes for other commands as well. Doesn't throw error, doesn't create anything. NOTE: I'm using Docker for Desktop with Kubernetes running.

-- bosari
docker
kubernetes
python

4 Answers

1/19/2021

CMD is a runtime instruction. How are you running your container? If you are doing anything like docker run -it 36298963bfa5 bash you are effectively overwriting the CMD-instruction with bash.

So switch the CMD to RUN action to create the directory already on the image building phase.

-- Teemu Risikko
Source: StackOverflow

1/19/2021

Why you mkdir in CMD?

The CMD is to provide defaults for an executing container.

The right way is to change CMD to RUN, means that mkdir in docker image, and then you will see the folder in any container from this image.

-- Ben
Source: StackOverflow

1/19/2021

For creating a directory inside a container it is better to use the RUN command and specify -p parameter for mkdir to create the parent directories.

You can also try to build your container via docker-compose.yml which contains

<!-- language: lang-yaml -->
version: '3'
services:
  python-app:
    build: .
    container_name: <your_python_container_name>
    working_dir: /code
    volumes:
      - <path_on_host_for_share>:/code
    stdin_open: true
    tty: true

and build your container with docker-compose build and docker-compose up afterwards.

-- Mihail
Source: StackOverflow

1/19/2021

Are you sure you create your container from the newly built image ?

Let's take a look again at the whole process:

We have the following Dockerfile:

# set base image (host OS)
FROM python:3.8

# set the working directory in the container
WORKDIR /code

# command to run on container start
RUN mkdir -p /tmp/xyz-agent

In the directory where Dockerfile is placed, we run:

docker build .

Then we run:

docker images

which shows us our newly built image:

$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
<none>       <none>    775e0c698e81   29 seconds ago   882MB
python       3.8       b0358f6298cd   6 days ago       882MB

Now we need to tag our newly created image:

docker tag 775e0c698e81 my-python:v1

When we run docker images again it shows us:

$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
my-python    v1        775e0c698e81   About a minute ago   882MB
python       3.8       b0358f6298cd   6 days ago           882MB

Let's run a new container from this image and check whether our directory has been successfully created:

$ docker run -ti my-python:v1 /bin/bash
root@6e93ed4a6e94:/code# pwd
/code
root@6e93ed4a6e94:/code# ls -ld /tmp/xyz-agent
drwxr-xr-x 2 root root 4096 Jan 19 14:58 /tmp/xyz-agent
root@6e93ed4a6e94:/code#

As you can see above, /tmp/xyz-agent directory is in it's place as expected. I hope this helps you figure out where you are making a mistake.

-- mario
Source: StackOverflow