1

I have simple commands for making and starting container (create.sh):

docker build -t foo .

docker rm bar
docker create --name=bar foo && \
docker start bar && \
docker exec bar sh /bin/echo Test!!!

Dockerfile:

#/bin/bash
FROM centos:7
RUN yum update -y

But it cannot be started:

$ bash create.sh
Sending build context to Docker daemon 4.608 kB
Step 1 : FROM centos:7
 ---> 980e0e4c79ec
Step 2 : RUN yum update -y
 ---> Using cache
 ---> 80b94205920c
Successfully built 80b94205920c
bar
a1db507225ca7479bdcc3bb3d4e3a86339827f4bf0e9365f507978b11d99df19
bar
Error response from daemon: Container bar is not running

The container has been created but it hasn't started.

Kirby
  • 2,847
  • 2
  • 32
  • 42

2 Answers2

4

Your Dockerfile has no ENTRYPOINT or CMD, so it finishes immediately, that is normal.

Check the docs about ENTRYPOINT

user2915097
  • 30,758
  • 6
  • 57
  • 59
  • extract from the doc https://docs.docker.com/engine/reference/builder/#/cmd `The main purpose of a CMD is to provide defaults for an executing container.` – user2915097 Oct 14 '16 at 12:02
  • from https://docs.docker.com/engine/reference/builder/#/entrypoint `An ENTRYPOINT allows you to configure a container that will run as an executable. For example, the following will start nginx with its default content, listening on port 80: docker run -i -t --rm -p 80:80 nginx` – user2915097 Oct 14 '16 at 12:08
  • Hm, it's still not clear. Do you mean this container stops immediately because there is tasks in background? Like nginx, apache, etc. – Kirby Oct 14 '16 at 12:18
  • No the container stops because he has completed what it had to do, see a basic nginx Dockerfile at https://github.com/nginxinc/docker-nginx/blob/8921999083def7ba43a06fabd5f80e4406651353/mainline/jessie/Dockerfile it has as last line `CMD ["nginx", "-g", "daemon off;"]` or see https://hub.docker.com/r/k3ck3c/nethogs/ it launches nethogs, by default on wlan0, but can also run on eth0 or ra1 – user2915097 Oct 14 '16 at 12:24
  • the last line of the docker mysql https://github.com/docker-library/mysql/blob/9f95658f528699d2c2017ca42ad163a9d5c5e7c1/5.7/Dockerfile launches the mysql daemon, as `CMD ["mysqld"]` – user2915097 Oct 14 '16 at 12:26
  • if you have no action, the docker container exists – user2915097 Oct 14 '16 at 12:27
  • Yeah, I got your point. So, I didn't know it should be stopped after finished all tasks. I found similar question: http://stackoverflow.com/questions/30209776/docker-container-will-automatically-stop-after-docker-run-d – Kirby Oct 14 '16 at 12:34
2

The Dockerfile for CentOS specifies the shell as the CMD to run when you start a container:

CMD ["/bin/bash"]

Docker containers exit when the process they started with finishes, so what's happening is when you start your container, it runs bash and then ends, because there's no more input to the shell.

Using your Docker image, this does what you expect - the echo command overrides the bash command in the Dockerfile:

> docker run temp echo 'Test'
Test 

If you want to keep a container running in the background, then you need the process inside the container to keep running. You can specify a command when you create the container:

 >docker create temp sleep infinity
a34a4528b3cfbb7a36fb429d32510c5576831adeb899c07e4596a6b9731c945b
> docker start a34
a34
> docker exec a34 echo 'Test'
Test
Elton Stoneman
  • 17,906
  • 5
  • 47
  • 44