1

My Dockerfile

FROM ubuntu
WORKDIR .
RUN apt-get update
RUN apt-get install apache2 -y
COPY fffff.txt /var/www/html/
ENTRYPOINT ["sh", "-c", "touch /root/FILE1"]

Build I'm using

docker build -ti first:latest .

but when i try to run it using

docker run -it -d first:latest bash 

its going to exit directly how can i make this container persistent?

Szymon Maszke
  • 22,747
  • 4
  • 43
  • 83
Nitin Shinde
  • 11
  • 1
  • 2

3 Answers3

1

Please see how ENTRYPOINT works in Docker. If you pass any command ( as CMD or like you did with bash), it will run it, so in your case it would be:

"sh", "-c", "touch /root/FILE1" bash

which makes absolutely no sense.

If you want to create a file inside a container and run it as daemon, do it like this:

Dockerfile:

FROM ubuntu

WORKDIR .
RUN apt-get update
RUN apt-get install apache2 -y

COPY fffff.txt /var/www/html/

RUN touch /root/myfile

Build it the same as you did, but run like this:

docker run -it -d first:latest /bin/bash 

Beware, it will run your container as a daemon (-d flag), so it will not go to the container's shell.

If you want to attach to the daemon container, use docker attach <container_id> (or <container_name>)

Szymon Maszke
  • 22,747
  • 4
  • 43
  • 83
1

The ENTRYPOINT and CMD directives tell Docker what to do when it starts a container from the image.

You're telling Docker that, when it starts a container from the image, it should create an empty file in the container's temporary filesystem. Once that process is complete, the container has finished its work and should exit.

In practice you usually want to actually set this metadata to tell Docker to run the server or application you've installed into the image, as a foreground process. In the case of Apache this would look something more like

CMD ["apachectl", "start", "-DFOREGROUND"]

Getting an interactive shell in a container can be handy for debugging but isn't usually the way you want to work with containers. docker run on its own should ideally produce a fully functional server.

David Maze
  • 130,717
  • 29
  • 175
  • 215
0

I had the same problem, after two days reading these docs :

Docker docs : CMD

Docker docs : ENTRYPOINT

What is the difference between CMD and ENTRYPOINT in a Dockerfile?

Docker RUN vs CMD vs ENTRYPOINT

and build and run about 30 Dockerfiles :| , finally I found out the way I can touch a file and make container persistent and not exit.

I used ENTRYPOINT as you did but added a sh as last command in this way :

exec form :

ENTRYPOINT [ "sh", "-c", "touch 1.txt && echo hello >> 1.txt &&  echo $myvar && sh" ]

shell form :

ENTRYPOINT touch 1.txt && echo hello >> 1.txt && echo $myvar && sh

Both these forms are persistent and do not exit after this command :

docker run -it --rm --name mycontainer -e "myvar=parsa" imagename

The advantage of these two forms is that they support variable substitution and you can run multiple commands separated by && .

Good luck.

Parsa
  • 7,995
  • 2
  • 27
  • 37