How do I stop a docker container so it will rerun

2019-07-30 06:54发布

问题:

I start my docker container with a name. like this:

docker run --name regsvc my-registrationservice

If I call docker stop regsvc, the next time I run the above command it fails. I have to run these commands first.

docker kill regsvc
docker system prune

That seems excessive. Is there a better way to stop the container and restart it?

Thnx Matt

回答1:

I believe you want to run a new container every time you issue docker run and it would be better for you to use --rm flag:

docker run --rm --name regsvc my-registrationservice

This will remove the container when your container exits. This is better if you don't want to save data of container.

As suggested by @trong-lam-phan you could restart your existing container using

docker restart regsvc


回答2:

When you stop a container, you can still see it with:

docker ps -a

Now the container is not alive but it is still there. So, you only need to restart it if you want it to work again:

docker restart regsvc

The command docker run will create a container from your image. So if you want to use docker run again, you need firstly remove your container (after stop it):

docker rm regsvc
docker run --name regsvc my-registrationservice 


标签: docker