gomisha
gomisha

Reputation: 2924

Docker container keeps stopping after 'docker start'

I'm fairly new to Docker. I have a long Dockerfile that I inherited from a previous developer, which has many errors and I'm trying to get it back to a working point. I commented out most of the file except for just the first line:

FROM ubuntu:14.04

I did the following:

  1. docker build -t pm . to build the image - this works because I can see the image when I execute docker images
  2. docker run <image-id> returns without error or any message. Now I'm expecting the container to be created from the image and started. But when I do a docker ps -a it shows the container exited:
CONTAINER ID        IMAGE               COMMAND             CREATED    STATUS                          PORTS               NAMES 

b05f9727f516 f216cfb59484 "/bin/bash" About a minute ago Exited (0) About a minute ago
lucid_shirley

Not sure why can't I get a running container and why does it keep stopping after the docker run command.

  1. executing docker logs <container_id> displays nothing - it just returns without any output.

Upvotes: 4

Views: 20814

Answers (6)

stevec
stevec

Reputation: 52258

Here's 3 ways:

Check that CMD is a long-running process

Ensure your CMD or ENTRYPOINT is a long-running or blocking process (like starting an app server, for example), otherwise it will just run and afterwards the container will stop.

Use -t option

Try using the -t option. Example

docker run -dt -p 8888:8888 my-image

Add a long-running command to CMD instruction

You can always place a long running or blocking process in your CMD instruction. For example if this is your CMD instruction:

CMD["sh", "-c", "/runme.sh"]

then you could add && tail -f /dev/null to it like so:

CMD["sh", "-c", "/runme.sh && tail -f /dev/null"]

That's simply giving CMD a long-running process, thus keeping your container running. Open another terminal window and run docker ps to see the container running.

Note

  • Related answers here could also be useful

Upvotes: 1

dotnetstep
dotnetstep

Reputation: 17485

Docker container categorized following way.

  1. Task Based : When container start it will start processing and it complete the process then exited.

  2. Background container : It will wait for some request.

As you not provided your docker file so I assume that you have only one statement.

FROM ubuntu:14.04

your build statement create image with name pm.

  1. Now you run docker run pm It will start container and stop as you did not provide any entry point.

  2. Now try this This is one command prompt or terminal.

    docker run -it pm /bin/bash
    

    Open another terminal or command prompt.

    docker ps ( Now you will see there is one container).
    
  3. If you want to see container that is continuously running then use following image. (This is just a example)

    docker run -d -p 8099:80 nginx 
    

Above line run one container with Nginx image and when you open your browser http://localhost:8099 you can see the response.

Upvotes: 3

Mahattam
Mahattam

Reputation: 5743

Each instruction of Dockerfile is a layer within a container which perform some task. In your docker file It's just the loading the ubuntu which is completed when you run the docker within a fraction of seconds and exit since process finished. So if want to have your container running all the time then there should be a foreground process running in your docker. For testing if you run

docker run <imageid> echo hi it will return the output means your container is fine.

Upvotes: 2

MrE
MrE

Reputation: 20778

try docker run -it <image> /bin/bash to run a shell inside the container.

That won't do much for you, but that'll show you what is happening: as soon as you exit the shell, it will exit the container too.

Your container basically doesn't do anything: it has an image of Ubuntu but doesn't have an ENTRYPOINT or a CMD command to run 'something' Containers are ephemeral when ran: they run a single command and exit when the command finishes.

Upvotes: 5

Jonathan Schoreels
Jonathan Schoreels

Reputation: 1720

Docker Containers are closely related to the process they are running. This process is specified by the "CMD" part on the Dockerfile. This process has the PID "1". If you kill it, your container is killed. If you haven't one, your container will stop instantly. In your case, you have to "override" your CMD. You can do it with a simple : "docker run -it ubuntu:18.04 bash". "-it" is mandatory since it allows the stdin to be attached to your container.

Have fun with docker.

Upvotes: 2

Ntwobike
Ntwobike

Reputation: 2741

Your Docker image doesn’t actually do anything, container stop when finish its job. Since here no foreground process running it will start and then immediately stop. To confirm your container have no issues, try to put below code into a docker-compose.yml(in same folder as the Dockerfile) and run docker-compose up, now you will see your container is running without exiting.

version: '3'
services:
  my-service:
    build: .
    tty: true    

Please have a look here Docker official tutorial it will guide you to how to work with docker.

Upvotes: 5

Related Questions