CodeGym /Courses /Docker SELF /Basic Commands

Basic Commands

Docker SELF
Level 11 , Lesson 1
Available

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.

Terminal

docker start my_container

2. Starting multiple containers:

In this example, we'll start containers container1 and container2 simultaneously.

Terminal

docker start container1 container2

Options

  1. -a or --attach: attaches your terminal to the container to view its output in real-time, like logs or error messages.
Terminal

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.

Terminal

docker stop my_container

2. Stopping multiple containers:

In this example, the containers container1 and container2 will be stopped at the same time.

Terminal

docker stop container1 container2

Options

  • -t or --time: sets a timeout in seconds before forcibly stopping the container (default is 10 seconds).
Terminal

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.

Terminal

docker restart my_container

2. Restart Multiple Containers:

In this example, the containers container1 and container2 will be restarted simultaneously.

Terminal

docker restart container1 container2

Options

  • -t or --time: sets the timeout in seconds before restarting the container (default is 10 seconds).
Terminal

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.

3
Task
Docker SELF, level 11, lesson 1
Locked
Launching multiple containers
Launching multiple containers
3
Task
Docker SELF, level 11, lesson 1
Locked
Stopping a container with a timeout
Stopping a container with a timeout
3
Task
Docker SELF, level 11, lesson 1
Locked
Restarting a container with a timeout
Restarting a container with a timeout
3
Task
Docker SELF, level 11, lesson 1
Locked
Removing a container with volumes
Removing a container with volumes
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION