4

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.
gomisha
  • 2,587
  • 3
  • 25
  • 33
  • you could view container's log by `docker logs your-container-id-or-name` and start debugging from there. – sipp11 Oct 23 '18 at 18:55
  • I did that and it showed nothing. I'll add that to my question. – gomisha Oct 23 '18 at 18:57
  • The machine on which you are running does it have enough memory to run Ubuntu on it? Are you running multiple containers. You can check running containers by `docker ps -a` – Yogesh D Oct 23 '18 at 19:04
  • I´d second @Ntwobike answer, if your container doesn´t actually do nothing to keep it running you could add a ``` CMD watch -n1 ls ``` to keep it busy. – Oscar Nevarez Oct 23 '18 at 19:49

6 Answers6

5

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.

Ntwobike
  • 2,406
  • 1
  • 21
  • 27
5

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.

MrE
  • 19,584
  • 12
  • 87
  • 105
3

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.

dotnetstep
  • 17,065
  • 5
  • 54
  • 72
2

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.

Jonathan Schoreels
  • 1,660
  • 1
  • 12
  • 20
2

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.

Mahattam
  • 5,405
  • 4
  • 23
  • 33
0

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
stevec
  • 41,291
  • 27
  • 223
  • 311