How restart a stopped docker container

2020-02-16 11:22发布

I launch a docker container from an image with the following command:

$ docker run -d myimage /bin/bash -c "mycommand"

When "mycommand" is finished, the container is stopped (I suppose it is stopped), but it is not deleted, because I can see it with this command:

$ docker ps -a

Is there any way to restart this container with the same parameters and keep data generated by mycommand?

标签: docker
2条回答
何必那么认真
2楼-- · 2020-02-16 11:42

First, $ docker ps -a shows all containers (the ones that are running and the stopped ones), so that is the reason you are not seeing your stopped container listed.

Second, you can easily start a stopped container running:

$ docker start container_name

Once the container has been started, you can run your command by:

$ docker exec -it container_name bash -c "mycommand"

The stuff you create in your container will remain inside your container as long as it exists. If you want to keep data even if your container is removed you can use a volume.

查看更多
Bombasti
3楼-- · 2020-02-16 11:59

Yes, when the initial command finish its execution then the container stops.

You can start a stopped container using:

docker start container_name

If you want to see the output of your command then you should add -ai options:

docker start -ai container_name

PS. there is a docker restart container_name but that is used to restart a running container - I believe that is not your case.

查看更多
登录 后发表回答