Dockerfile CMD instruction will exit the container just after running it

BashShellDockerShDockerfile

Bash Problem Overview


I want to setup some configuration when my container starts, for this I am using shell scripts. But my container will exits as soon as my scripts ends, I have tried with -d flag / detached mode but It will never run in detached mode.

Below is my Dockerfile

FROM ubuntu:14.04

ADD shell.sh /usr/local/bin/shell.sh

RUN chmod 777 /usr/local/bin/shell.sh

CMD /usr/local/bin/shell.sh

Below is my shell script

#!/bin/bash
echo Hello-docker
  1. Run without any flag

     docker run hello-docker
    

This will print 'Hello-docker' on my console and exits 2. Run with -itd flags

    docker run -itd hello-docker

and as below my console output, This time also will exits soon. :( enter image description here

  • The difference I saw is in COMMAND section when I run other images command section will shows "/bin/bash" and will continue in detached mode.

  • And when I run my image in container with shell script COMMAND section will show "/bin/sh -c /usr/loca", and Exit.

  • I want to run container till I not stop it manually.

EDIT:

After adding ENTRYPOINT instruction in Dockerfile, this will not execute my shell script :(

FROM ubuntu:14.04

ADD shell.sh /usr/local/bin/shell.sh

RUN chmod 777 /usr/local/bin/shell.sh

CMD /usr/local/bin/shell.sh

ENTRYPOINT /bin/bash

As per docker documentation here

CMD will be overridden when running the container with alternative arguments, so If I run docker image with some arguments as below, will not execute CMD instructions. :(

sudo docker run -it --entrypoint=/bin/bash <imagename>

Bash Solutions


Solution 1 - Bash

A docker container will run as long as the CMD from your Dockerfile takes.

In your case your CMD consists of a shell script containing a single echo. So the container will exit after completing the echo.

You can override CMD, for example:

sudo docker run -it --entrypoint=/bin/bash <imagename>

This will start an interactive shell in your container instead of executing your CMD. Your container will exit as soon as you exit that shell.

If you want your container to remain active, you have to ensure that your CMD keeps running. For instance, by adding the line while true; do sleep 1; done to your shell.sh file, your container will print your hello message and then do nothing any more until you stop it (using docker stop in another terminal).

You can open a shell in the running container using docker exec -it <containername> bash. If you then execute command ps ax, it will show you that your shell.sh is still running inside the container.

Solution 2 - Bash

Finally with some experiments I got my best result as below

There is nothing wrong with my Dockerfile as below it's correct.

FROM ubuntu:14.04

ADD shell.sh /usr/local/bin/shell.sh

RUN chmod 777 /usr/local/bin/shell.sh

CMD /usr/local/bin/shell.sh

What I do to get expected result is, I just add one more command(/bin/bash) in my shell script file as below and vola everything works in my best way.

#!/bin/bash

echo “Hello-docker” > /usr/hello.txt

/bin/bash

Solution 3 - Bash

You can also modify your first Dockerfile, replacing

CMD /usr/local/bin/shell.sh

by

CMD /usr/local/bin/shell.sh ; sleep infinity

That way, your script does not terminate, and your container stays running.

Solution 4 - Bash

CMD bash -C '/path/to/start.sh';'bash'

Solution 5 - Bash

At your start shell append a line code: tail -f /dev/null or /bin/bash to make sure you shell done and suspend a process in system so that docker container not shutdown.Don't forget to give "chmod +x" access to start.sh. there is demo:

#!/bin/bash
cp /root/supervisor/${RUN_SERVICE}.ini /etc/supervisor/conf.d/
sleep 1
service supervisor start
/bin/bash

Solution 6 - Bash

Try

CMD /bin/bash -c 'MY_COMMAND_OR_SHELL_SCRIPT; /bin/bash'

Trying an explanation here to the answer of @lanni654321. sh shell is standard in Dockerfile. You must call bash shell to start bash with .bashrc, many commands also need RUN /bin/bash -c '...' in the same way as in CMD above, since sh shell is often not enough. If you add 'bash' in the end of CMD, the container will not exit because the image was committed with something that is still open.

See “/bin/sh: 1: MY_COMMAND: not found” for an error caused by sh and solved by bash.

I think that you will usually not need this. You can just use RUN /bin/bash -c '...', in my case, this could do anything that can be done in a base image before you go into varying details in docker-compose to start the containers.

But that is all not needed if you need to just have a container running without exiting. Just

docker run -dit --name MY_CONTAINER MY_IMAGE:latest

and then

docker exec -it MY_CONTAINER /bin/bash

and you should be in the bash of the container, and it should not exit.

Or if the exit happens during docker-compose, use

command: bash -c "MY_COMMAND --wait"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnand SutharView Question on Stackoverflow
Solution 1 - BashNZDView Answer on Stackoverflow
Solution 2 - BashAnand SutharView Answer on Stackoverflow
Solution 3 - Bashuser2915097View Answer on Stackoverflow
Solution 4 - Bashlanni654321View Answer on Stackoverflow
Solution 5 - BashghostyushengView Answer on Stackoverflow
Solution 6 - Bashquestionto42standswithUkraineView Answer on Stackoverflow