Reputation: 9063
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
?
Upvotes: 173
Views: 257475
Reputation: 1171
docker container ls
Copy CONTAINER ID from the output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aeb64b607336 dbeaver/cloudbeaver:latest "./run-server.sh" About an hour ago Up 2 minutes 0.0.0.0:8080->8978/tcp cloudbeaver
docker container pause container_id
docker container unpause container_id
Upvotes: 0
Reputation: 843
Unfortunately, if you restart your VM/System and it shows
mysql-tls:5.7 "docker-entrypoint.s…" 18 hours ago Exited (255) 44 seconds ago
Answer :
Start the Container
docker start mysql
or
docker start your_container_name
Upvotes: 0
Reputation: 11445
From the above picture we see one container is up and other status is Exited.
When a container is exited we can still start it back up, because a container stop doesn't mean that it's like dead or cannot be used again we can very easily stop and then start containers again at some point in the future. To start a container backup we can take it's ID and then execute docker start and paste the ID end.
sudo docker start container_id
command for exited container in the above picture will be.
sudo docker start -a bba606a95392
Out put:
By the way: While restarting a container we can not replace the default command, as soon as you started up with the default command is set for the container, for example if we start our container overriding the default command let's see what happened:
Docker is thinking we are trying to start and attach multiple container at the same time.
So when we up a container and let it exit, we can start it back up again which is going to reissue the default command that was used when the container was first created. It is part of docker container lifecycle.
Upvotes: 6
Reputation: 105
It should be
$ docker restart container_id # OR
$ docker restart container_name
Upvotes: 8
Reputation: 69655
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.
Upvotes: 26
Reputation: 5576
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.
Upvotes: 228