How to use docker container start command to restart containers

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker container start command to restart stopped containers. You will begin by creating and stopping containers using the hello-world and ubuntu images.

Following that, you will explore different ways to start a stopped container, including simply starting it, starting it and attaching to its output, and starting it interactively. This will demonstrate the flexibility of the docker container start command for managing container lifecycle.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/start("Start Container") docker/ContainerOperationsGroup -.-> docker/stop("Stop Container") docker/ContainerOperationsGroup -.-> docker/attach("Attach to Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/run -.-> lab-555122{{"How to use docker container start command to restart containers"}} docker/ps -.-> lab-555122{{"How to use docker container start command to restart containers"}} docker/start -.-> lab-555122{{"How to use docker container start command to restart containers"}} docker/stop -.-> lab-555122{{"How to use docker container start command to restart containers"}} docker/attach -.-> lab-555122{{"How to use docker container start command to restart containers"}} docker/pull -.-> lab-555122{{"How to use docker container start command to restart containers"}} end

Create and stop a container

In this step, you will learn how to create and stop a Docker container. A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI.

First, let's pull the hello-world image from Docker Hub. This is a very small image that is useful for testing.

docker pull hello-world

You should see output indicating that the image is being pulled and extracted.

Using default tag: latest
latest: Pulling from library/hello-world
...
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

Now, let's create and run a container from the hello-world image. When you run this command, Docker will create a new container and execute the command specified in the image. In the case of hello-world, the command simply prints a message and then exits.

docker run hello-world

You should see output similar to this:

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

This output confirms that the container ran successfully and printed its message. Since the command in the container finished, the container has stopped.

To see the containers on your system, including those that have stopped, you can use the docker ps -a command.

docker ps -a

You will see a list of containers. The hello-world container should be in the list, and its status should be Exited.

CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                      PORTS    NAMES
...            hello-world   "/hello"   About a minute ago   Exited (0) 58 seconds ago            ...

Now, let's create another container, but this time we will use the ubuntu image and run a command that keeps the container running for a short period. We will use the sleep command to keep the container alive for 10 seconds.

First, pull the ubuntu image:

docker pull ubuntu

You should see output indicating the image is being pulled.

Using default tag: latest
latest: Pulling from library/ubuntu
...
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

Now, run a container from the ubuntu image and execute the sleep 10 command. We will run this in detached mode (-d) so that the container runs in the background and doesn't block our terminal.

docker run -d ubuntu sleep 10

This command will output the container ID.

[container_id]

Use docker ps to see the running containers.

docker ps

You should see the ubuntu container listed with a status of Up.

CONTAINER ID   IMAGE    COMMAND       CREATED         STATUS         PORTS    NAMES
[container_id]   ubuntu   "sleep 10"   About a minute ago   Up 5 seconds            ...

After about 10 seconds, the sleep 10 command will finish, and the container will stop. Use docker ps -a again to see all containers, including the stopped one.

docker ps -a

The ubuntu container should now have a status of Exited.

CONTAINER ID   IMAGE    COMMAND       CREATED         STATUS                      PORTS    NAMES
[container_id]   ubuntu   "sleep 10"   About a minute ago   Exited (0) 5 seconds ago             ...
...

Finally, let's stop the ubuntu container manually using the docker stop command. You can stop a running container by its ID or name. We will use the container ID that was output when we ran the container. Replace [container_id] with the actual ID of your ubuntu container.

docker stop [container_id]

This command will output the container ID that was stopped.

[container_id]

Use docker ps again to confirm that the container is no longer running.

docker ps

The ubuntu container should not appear in the list of running containers.

Start the stopped container

In the previous step, we created and stopped a container based on the ubuntu image. In this step, you will learn how to start a stopped container.

First, let's list all containers, including the stopped ones, to find the ID of the ubuntu container we created.

docker ps -a

Look for the container with the ubuntu image and Exited status. Note down its CONTAINER ID.

CONTAINER ID   IMAGE    COMMAND       CREATED          STATUS                      PORTS    NAMES
[container_id]   ubuntu   "sleep 10"   About 5 minutes ago   Exited (0) 4 minutes ago             ...
...

Now, we can start this stopped container using the docker start command followed by the container ID. Replace [container_id] with the actual ID of your ubuntu container.

docker start [container_id]

This command will output the container ID that was started.

[container_id]

Use docker ps to verify that the container is now running.

docker ps

You should see the ubuntu container listed with a status of Up.

CONTAINER ID   IMAGE    COMMAND       CREATED          STATUS         PORTS    NAMES
[container_id]   ubuntu   "sleep 10"   About 5 minutes ago   Up 5 seconds            ...

Since the original command for this container was sleep 10, the container will run for 10 seconds and then stop again. After a few seconds, run docker ps -a again to see its status.

docker ps -a

The ubuntu container should now be in the Exited state again.

CONTAINER ID   IMAGE    COMMAND       CREATED          STATUS                       PORTS    NAMES
[container_id]   ubuntu   "sleep 10"   About 5 minutes ago   Exited (0) 5 seconds ago              ...
...

Starting a stopped container is useful when you want to resume the work or state of a previous container instance without creating a new one.

Start the container and attach to its output

In the previous steps, we created and started containers. When we used docker run or docker start in detached mode (-d), the container ran in the background. Sometimes, you want to see the output of a container as it runs. In this step, you will learn how to start a container and attach to its standard output and standard error streams.

First, let's create a new container that will print a message every second for 5 seconds. We will use the ubuntu image again.

docker run -d ubuntu /bin/bash -c 'for i in {1..5}; do echo "Hello from container $i"; sleep 1; done'

This command creates and runs a container in detached mode (-d). The command executed inside the container is a bash script that loops 5 times, printing a message and sleeping for 1 second in each iteration. Note down the container ID that is output.

[container_id]

Now, let's attach to this running container to see its output. Use the docker attach command followed by the container ID. Replace [container_id] with the actual ID of your ubuntu container.

docker attach [container_id]

You should see the output of the container appearing in your terminal:

Hello from container 1
Hello from container 2
Hello from container 3
Hello from container 4
Hello from container 5

After the loop finishes, the container will stop, and you will be returned to your terminal prompt.

If you attach to a container that is already stopped, the docker attach command will exit immediately.

Let's verify that the container has stopped by listing all containers:

docker ps -a

You should see the container with the command /bin/bash -c 'for i in {1..5}; do echo "Hello from container $i"; sleep 1; done' in the Exited state.

CONTAINER ID   IMAGE    COMMAND                  CREATED         STATUS                      PORTS    NAMES
[container_id]   ubuntu   "/bin/bash -c 'for iโ€ฆ"   About a minute ago   Exited (0) 5 seconds ago             ...
...

Attaching to a container is useful for monitoring the output of a long-running process or for debugging purposes.

Start the container interactively

In the previous steps, we ran containers that executed a command and then exited, or ran in the background. Often, you want to interact with a container, just like you would with a virtual machine or a remote server. In this step, you will learn how to start a container interactively and get a shell prompt inside it.

To run a container interactively, we use the docker run command with the -i and -t flags.

  • The -i flag keeps the standard input (STDIN) open, even if not attached. This is necessary for interactive processes.
  • The -t flag allocates a pseudo-TTY, which is required for a shell prompt.

We will run an ubuntu container and execute the /bin/bash command to get a bash shell inside the container.

docker run -it ubuntu /bin/bash

After running this command, you will see a shell prompt that looks like this:

root@[container_id]:/#

This indicates that you are now inside the running ubuntu container as the root user. You can run standard Linux commands inside this container. For example, let's check the operating system version:

cat /etc/os-release

You should see output similar to this, confirming that you are running inside an Ubuntu container:

NAME="Ubuntu"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 22.04.3 LTS"
VERSION_ID="22.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=jammy
UBUNTU_CODENAME=jammy

You can also try other commands, like listing files in the current directory:

ls

You will see the standard root directory contents of an Ubuntu system.

To exit the container and return to your host terminal, you can type exit at the container's shell prompt:

exit

When you exit the shell, the container will stop because the main process (/bin/bash) has finished.

Let's verify that the container has stopped by listing all containers:

docker ps -a

You should see the container with the command /bin/bash in the Exited state.

CONTAINER ID   IMAGE    COMMAND       CREATED         STATUS                      PORTS    NAMES
[container_id]   ubuntu   "/bin/bash"   About a minute ago   Exited (0) 5 seconds ago             ...
...

Running containers interactively is essential for exploring container images, debugging applications inside containers, and performing administrative tasks.

Summary

In this lab, you learned how to create and stop Docker containers using the docker run and docker stop commands. You practiced pulling images with docker pull and verified container status using docker ps -a. You also created a container that exited immediately (hello-world) and one that remained running for a period (ubuntu with sleep).

You then explored different ways to restart stopped containers using the docker start command. You learned how to simply start a container, how to start and attach to its output using the -a flag, and how to start a container interactively using the -i flag, allowing you to execute commands within the running container.