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.
The Dockerfile for CentOS specifies the shell as the
CMD
to run when you start a container:Docker containers exit when the process they started with finishes, so what's happening is when you
start
your container, it runsbash
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 thebash
command in the Dockerfile: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:
Your Dockerfile has no ENTRYPOINT or CMD, so it finishes immediately, that is normal.
Check the docs about ENTRYPOINT