Managing Container Lifecycle: Start, Stop, Restart

Tutorial 3 of 5

Managing Container Lifecycle: Start, Stop, Restart

1. Introduction

Goal

This tutorial will guide you through the lifecycle of a Docker container, specifically focusing on how to start, stop, and restart Docker containers.

Learning Outcome

By the end of this tutorial, you will be able to manage the lifecycle of a Docker container, including starting, stopping, and restarting containers.

Prerequisites

To follow along with this tutorial, you will need:

  • Basic knowledge of Docker
  • Docker installed on your machine. If you don't have Docker installed, follow the official Docker installation guide for your operating system.

2. Step-by-Step Guide

Docker Container Lifecycle

A Docker container lifecycle starts when it is created, and it ends when it is removed. In between, it can be started, stopped, and restarted as required. This is important so that you can manage your application's availability and resources efficiently.

Starting a Container

To start a Docker container, you need to use the docker run command followed by the name of the image.

Stopping a Container

To stop a running Docker container, you can use the docker stop command followed by the container id or name.

Restarting a Container

Restarting a Docker container is as simple as running the docker restart command, followed by the container id or name.

Best Practices and Tips

  • It's recommended to clean up stopped containers to save disk space.
  • Use meaningful names for your containers so you can remember and manage them easily.
  • Monitor your containers using docker stats command.

3. Code Examples

Starting a Container

# Start a new container from the hello-world image
docker run hello-world

This command will pull the hello-world image from the Docker Hub and start a new container from this image. The expected output is a welcome message from Docker.

Stopping a Container

# Get the container id
docker ps

# Stop the running container
docker stop <container-id>

First, we use docker ps to get the id of the running container. Then, we use docker stop followed by the container id to stop the container. The expected output is the container id.

Restarting a Container

# Restart the container
docker restart <container-id>

We use docker restart followed by the container id to restart the container. The expected output is the container id.

4. Summary

In this tutorial, we've learned how to manage the lifecycle of a Docker container, including starting, stopping, and restarting containers. We've also covered some best practices for managing containers.

5. Practice Exercises

Here are some exercises to help you practice:

  1. Start a new container from the nginx image.
  2. Stop the running nginx container.
  3. Restart the nginx container.

Remember, the key to mastering Docker container management is practice. So, keep practicing and exploring different Docker commands and options.

For further learning, check out the official Docker documentation and other online resources.

Solution:

# Start a new container from the nginx image
docker run nginx

# Stop the running nginx container
docker stop <nginx-container-id>

# Restart the nginx container
docker restart <nginx-container-id>

Remember to replace <nginx-container-id> with your actual container id.