Cannot start simple docker container with “docker

2019-07-28 05:01发布

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.

2条回答
Evening l夕情丶
2楼-- · 2019-07-28 05:26

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
查看更多
仙女界的扛把子
3楼-- · 2019-07-28 05:44

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

Check the docs about ENTRYPOINT

查看更多
登录 后发表回答