Gibbs
Gibbs

Reputation: 22974

Why docker container exits immediately

I run a container in the background using

 docker run -d --name hadoop h_Service

it exits quickly. But if I run in the foreground, it works fine. I checked logs using

docker logs hadoop

there was no error. Any ideas?

DOCKERFILE

 FROM java_ubuntu_new
 RUN wget http://archive.cloudera.com/cdh4/one-click-install/precise/amd64/cdh4-repository_1.0_all.deb
 RUN dpkg -i cdh4-repository_1.0_all.deb
 RUN curl -s http://archive.cloudera.com/cdh4/ubuntu/precise/amd64/cdh/archive.key | apt-key add -
 RUN  apt-get update
 RUN apt-get install -y hadoop-0.20-conf-pseudo
 RUN dpkg -L hadoop-0.20-conf-pseudo
 USER hdfs
 RUN hdfs namenode -format
 USER root
 RUN apt-get install -y sudo
 ADD . /usr/local/
 RUN chmod 777 /usr/local/start-all.sh
 CMD ["/usr/local/start-all.sh"]

start-all.sh

 #!/usr/bin/env bash
 /etc/init.d/hadoop-hdfs-namenode start
 /etc/init.d/hadoop-hdfs-datanode start
 /etc/init.d/hadoop-hdfs-secondarynamenode start
 /etc/init.d/hadoop-0.20-mapreduce-tasktracker start
 sudo -u hdfs hadoop fs -chmod 777 /
 /etc/init.d/hadoop-0.20-mapreduce-jobtracker start
 /bin/bash

Upvotes: 473

Views: 709179

Answers (22)

z atef
z atef

Reputation: 7689

This worked for me command: tail -f /dev/null, by adding in docker-compose.yml

version: '3.8'
services:
  my_container:
    image: your_image
    command: tail -f /dev/null

Upvotes: 0

Amayuru Upanith
Amayuru Upanith

Reputation: 1

Try using this command, it has been worked for me.

docker run -it --entrypoint=/bin/bash image

Note-image should be replaced with the actual docker image you have created. Container is running now(after executing that in terminal)

These are container logs. 2024-04-24 11:23:42 I have no name!@f1490d9a208e:/usr/src/app#

Upvotes: -1

jkix
jkix

Reputation: 496

If you're using docker compose, you can put tty: true in your compose.yml as an argument under a given service:

services: 
  ubuntu_test:
    container_name: dev
    build:
      context: .
    
    tty: true
    

See this answer for more details: Docker Compose keep container running

Upvotes: 3

questionto42
questionto42

Reputation: 9590

If you need to just have a container running without exiting, just run

docker run -dit --name MY_CONTAINER MY_IMAGE:latest

and then

docker exec -it MY_CONTAINER /bin/bash

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

The following worked only with roslaunch in a ROS simulation, this "--wait" is not a default parameter for docker-compose!

Mind: This is hard to read, and it seems to be only about a self-written parameter. Just skip it.

Or if the exit happens during docker-compose, use

command: bash -c "MY_COMMAND --wait"

as already stated by two other answers here (though not that clearly referring to docker-compose, that is why I still mention the "wait" trick again).

In the ROS simulation, it would be, for example:

command: bash -c "roslaunch gazebo_ros empty_world.launch --wait"

Using --wait was the core to get a so-called ROS network on docker-compose to work, see example code at ROS in docker-compose leads to "bash: line 0: cd: MYPROJECT: No such file or directory". More on roslaunch is at Docker on WSL2: Dockerfile: how to test whether ROS gazebo can connect to already working X server (using its X11 display on Windows)?.

Thus, this --wait must have been an argument for a command like roslaunch gazebo_ros empty_world.launch. But when checking the linked tutorial Tutorial: Using roslaunch to start Gazebo, world files and URDF models, there is no such parameter in the code, just:

roslaunch gazebo_ros empty_world.launch paused:=true use_sim_time:=false gui:=true throttled:=false recording:=false debug:=true verbose:=true gui_required:=true

That is why I guess that it must have been a self-written extension of the given parameters of the project.

I tried this --wait later again in other settings, and it did not work. In the ROS launch script, where it worked, it likely just shadowed the workaround of another answer in this Q/A (&& tail ...).

Upvotes: 35

ychz
ychz

Reputation: 571

docker logs information is not usually helpful to me. Sometimes app's log is same as docker logs output, sometimes is not.

docker inspect not helpful in most case either, unless it is a very obvious error.

If your Entrypoint or last command failed, just find the log generated by that command. Persist the container log through a volume on your host and check the log.

Alternatively, if you prefer a somehow interactive environment. Let you container enter sleep mode. Entrypoint sleep(whatever long you like), then log into the container with docker exec -it container_id sh and run your Entrypoint command manually, see what is going on.

Upvotes: 0

Richi RM
Richi RM

Reputation: 1197

$ docker run -di image

It works.

Upvotes: -1

kta
kta

Reputation: 20140

Docker container terminates immediately when it did not have any foreground process that helps to connect to user terminal. For example, there is no web server up running in that container.

There are couple of ways to create a foreground process. One such method is to redirect to /dev/null which prevents container from immediate exit. After that you can use exec command with -it parameter to attach it to your terminal.

docker run -d ubuntu tail -f /dev/null
docker exec -it 0ab99d8ab11c /bin/bash

Upvotes: 1

Vivek
Vivek

Reputation: 707

You need to run it with -d flag to leave it running as daemon in the background.

docker run -d -it ubuntu bash

Upvotes: 20

xavierbaez
xavierbaez

Reputation: 9

If you check Dockerfile from containers, for example fballiano/magento2-apache-php

you'll see that at the end of his file he adds the following command: while true; do sleep 1; done

Now, what I recommend, is that you do this

docker container ls --all | grep 127

Then, you will see if your docker image had an error, if it exits with 0, then it probably needs one of these commands that will sleep forever.

Upvotes: 0

Adam k
Adam k

Reputation: 39

Adding

exec "$@"

at the end of my shell script was my fix!

Upvotes: 3

tripleee
tripleee

Reputation: 189789

Coming from duplicates, I don't see any answer here which addresses the very common antipattern of running your main workload as a background job, and then wondering why Docker exits.

In simple terms, if you have

my-main-thing &

then either take out the & to run the job in the foreground, or add

wait

at the end of the script to make it wait for all background jobs.

It will then still exit if the main workload exits, so maybe run this in a while true loop to force it to restart forever:

while true; do
    my-main-thing &
    other things which need to happen while the main workload runs in the background
    maybe if you have such things
    wait
done

(Notice also how to write while true. It's common to see silly things like while [ true ] or while [ 1 ] which coincidentally happen to work, but don't mean what the author probably imagined they ought to mean.)

Upvotes: 2

Jaydeep Ranipa
Jaydeep Ranipa

Reputation: 439

I added read shell statement at the end. This keeps the main process of the container - startup shell script - running.

Upvotes: 1

Kert Kukk
Kert Kukk

Reputation: 715

Add this to the end of Dockerfile:

CMD tail -f /dev/null

Sample Docker file:

FROM ubuntu:16.04

# other commands

CMD tail -f /dev/null

Reference

Upvotes: 30

RJFalconer
RJFalconer

Reputation: 11721

Why docker container exits immediately?

If you want to force the image to hang around (in order to debug something or examine state of the file system) you can override the entry point to change it to a shell:

docker run -it --entrypoint=/bin/bash myimagename

Upvotes: 90

camposer
camposer

Reputation: 5622

This did the trick for me:

docker run -dit ubuntu

After it, I checked for the processes running using:

docker ps -a

For attaching again the container

docker attach CONTAINER_NAME

TIP: For exiting without stopping the container type: ^P^Q

Upvotes: 410

Brian Olsen
Brian Olsen

Reputation: 945

A nice approach would be to start up your processes and services running them in the background and use the wait [n ...] command at the end of your script. In bash, the wait command forces the current process to:

Wait for each specified process and return its termination status. If n is not given, all currently active child processes are waited for, and the return status is zero.

I got this idea from Sébastien Pujadas' start script for his elk build.

Taking from the original question, your start-all.sh would look something like this...

 #!/usr/bin/env bash
 /etc/init.d/hadoop-hdfs-namenode start &
 /etc/init.d/hadoop-hdfs-datanode start &
 /etc/init.d/hadoop-hdfs-secondarynamenode start &
 /etc/init.d/hadoop-0.20-mapreduce-tasktracker start &
 sudo -u hdfs hadoop fs -chmod 777 /
 /etc/init.d/hadoop-0.20-mapreduce-jobtracker start &
 wait

Upvotes: 23

Sibeesh Venu
Sibeesh Venu

Reputation: 21779

There are many possible ways to cause a docker to exit immediately. For me, it was the problem with my Dockerfile. There was a bug in that file. I had ENTRYPOINT ["dotnet", "M4Movie_Api.dll] instead of ENTRYPOINT ["dotnet", "M4Movie_Api.dll"]. As you can see I had missed one quotation(") at the end.

To analyze the problem I started my container and quickly attached my container so that I could see what was the exact problem.

C:\SVenu\M4Movie\Api\Api>docker start 4ea373efa21b


C:\SVenu\M4Movie\Api\Api>docker attach 4ea373efa21b

Where 4ea373efa21b is my container id. This drives me to the actual issue.

enter image description here

After finding the issue, I had to build, restore, publish my container again.

Upvotes: 0

Thiago Ramos
Thiago Ramos

Reputation: 669

whenever I want a container to stay up after finish the script execution I add

&& tail -f /dev/null

at the end of command. So it should be:

/usr/local/start-all.sh && tail -f /dev/null

Upvotes: 51

Leon
Leon

Reputation: 3244

My pracitce is in the Dockerfile start a shell which will not exit immediately CMD [ "sh", "-c", "service ssh start; bash"], then run docker run -dit image_name. This way the (ssh) service and container is up running.

Upvotes: 6

dskow
dskow

Reputation: 944

Since the image is a linux, one thing to check is to make sure any shell scripts used in the container have unix line endings. If they have a ^M at the end then they are windows line endings. One way to fix them is with dos2unix on /usr/local/start-all.sh to convert them from windows to unix. Running the docker in interactive mode can help figure out other problems. You could have a file name typo or something. see https://en.wikipedia.org/wiki/Newline

Upvotes: -8

Krishna Gupta
Krishna Gupta

Reputation: 1309

I would like to extend or dare I say, improve answer mentioned by camposer

When you run

docker run -dit ubuntu

you are basically running the container in background in interactive mode.

When you attach and exit the container by CTRL+D (most common way to do it), you stop the container because you just killed the main process which you started your container with the above command.

Making advantage of an already running container, I would just fork another process of bash and get a pseudo TTY by running:

docker exec -it <container ID> /bin/bash

Upvotes: 98

Adrian Mouat
Adrian Mouat

Reputation: 46548

A docker container exits when its main process finishes.

In this case it will exit when your start-all.sh script ends. I don't know enough about hadoop to tell you how to do it in this case, but you need to either leave something running in the foreground or use a process manager such as runit or supervisord to run the processes.

I think you must be mistaken about it working if you don't specify -d; it should have exactly the same effect. I suspect you launched it with a slightly different command or using -it which will change things.

A simple solution may be to add something like:

while true; do sleep 1000; done

to the end of the script. I don't like this however, as the script should really be monitoring the processes it kicked off.

(I should say I stole that code from https://github.com/sequenceiq/hadoop-docker/blob/master/bootstrap.sh)

Upvotes: 216

Related Questions