Starting and stopping Docker containers are fundamental skills in daily Docker operations. Below, I will explain how to perform both operations.
Starting Docker Containers
Starting Docker containers typically involves using the docker start command. For launching a new container for the first time, use docker run. Here are the specific steps and examples:
-
Using
docker runto start a new container:-
Command syntax:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...] -
For example, if I want to run the
ubuntuimage and execute thebashcommand within it, I can use the following command:bashdocker run -it ubuntu bashThis command creates a new container from the
ubuntuimage and starts an interactive terminal running thebashcommand.
-
-
Using
docker startto start an existing container:-
Command syntax:
docker start [OPTIONS] CONTAINER [CONTAINER...] -
For example, if I already have a container named
mycontainer, I can use the following command to start it:bashdocker start mycontainerThis command only starts the container without entering it. If you need to enter the container, you can use the
docker attachordocker execcommands.
-
Stopping Docker Containers
Stopping Docker containers primarily involves using the docker stop command. Here are the specific steps and examples:
- Using
docker stopto stop a container:-
Command syntax:
docker stop [OPTIONS] CONTAINER [CONTAINER...] -
For example, if I want to stop the container named
mycontainer, I can use the following command:bashdocker stop mycontainerThis command sends a stop signal to the container, which then performs cleanup tasks before stopping.
-
Important Notes
- When using
docker run, you can control container behavior with various options, such as memory allocation (-m), CPU (--cpus), and network configuration. - When stopping a container, if the application inside is slow to respond to stop signals or does not respond, you can use the
docker killcommand to forcibly stop the container.