Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile CMD instruction will exit the container just after running it

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> 
like image 390
Anand Suthar Avatar asked Feb 14 '17 06:02

Anand Suthar


People also ask

Why does my docker container exit immediately?

You're running a shell in a container, but you haven't assigned a terminal: If you're running a container with a shell (like bash ) as the default command, then the container will exit immediately if you haven't attached an interactive terminal.

How does CMD work in Dockerfile?

A CMD command is used to set a default command that gets executed once you run the Docker Container. In case you provide a command with the Docker run command, the CMD arguments get ignored from the dockerfile. In the case of multiple CMD commands, only the last one gets executed.

How do I run a docker container without exiting?

Press Ctrl-P, followed by Ctrl-Q, to detach from your connection. You'll be dropped back into your shell but the previously attached process will remain alive, keeping your container running.

How do I keep my Dockerfile from running a container?

Dockerfile Command to Keep the Container Running Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.


Video Answer


1 Answers

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.

like image 105
NZD Avatar answered Oct 22 '22 11:10

NZD