2.1 Command docker start
Docker provides handy tools for managing containers at all stages of their lifecycle. In this lecture, we'll break down four basic commands: start
, stop
, restart
, and rm
. These let you start, stop, restart, and delete containers.
The docker start
command starts previously stopped containers. This means you can reuse an already created container instead of creating it anew every time you need it.
Syntax
docker start [OPTIONS] CONTAINER [CONTAINER...]
Here:
-
CONTAINER
: the name or ID of the container you want to start.
Examples
1. Starting a single container:
In this example, we'll start a container named my_container. It will only start if it has already been created and stopped earlier.
docker start my_container
2. Starting multiple containers:
In this example, we'll start containers container1 and container2 simultaneously.
docker start container1 container2
Options
-
-a
or--attach
: attaches your terminal to the container to view its output in real-time, like logs or error messages.
docker start -a my_container
2.2 Command docker stop
The docker stop
command is used to stop running containers. This command gives the container time to properly shut down by sending a SIGTERM signal and then a SIGKILL if the container does not stop within the set time.
Syntax
docker stop [OPTIONS] CONTAINER [CONTAINER...]
Where:
-
CONTAINER
: name or ID of the container you want to stop.
Examples
1. Stopping a single container:
In this example, the container named my_container will be stopped.
docker stop my_container
2. Stopping multiple containers:
In this example, the containers container1 and container2 will be stopped at the same time.
docker stop container1 container2
Options
-
-t
or--time
: sets a timeout in seconds before forcibly stopping the container (default is 10 seconds).
docker stop -t 30 my_container
2.3 The docker restart
Command
The docker restart
command is used to restart containers. This comes in handy when you need to quickly apply changes or fix errors.
Syntax
docker restart [OPTIONS] CONTAINER [CONTAINER...]
Where:
-
CONTAINER
: the name or ID of the container you want to restart.
Examples
1. Restart a Single Container:
In this example, we restart the container named my_container.
docker restart my_container
2. Restart Multiple Containers:
In this example, the containers container1 and container2 will be restarted simultaneously.
docker restart container1 container2
Options
-
-t
or--time
: sets the timeout in seconds before restarting the container (default is 10 seconds).
docker restart -t 20 my_container
2.4 The docker rm
Command
The docker rm
command is used to remove stopped containers. This frees up the resources that were taken up by the container. Before removing, the container should be stopped.
GO TO FULL VERSION