docker

How restart a stopped docker container


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?


Solution

  • Yes, when the CMD task finishes its execution (either normally or because of an error, including "the command you're trying to run is a bad command") the container stops.

    You can start a stopped container by using:

    docker start container_name

    If you want to see the output of your command then you should add -ai options, where "a" tells docker to attach the container's stdout and stderr to your current terminal, and "i" tells docker to attach stdin, too.

    docker start -ai container_name

    (There is also a restart command, but that is only for restarting a still running container, not a stopped container)