Container Management

Tutorial 1 of 4

Container Management Tutorial

1. Introduction

Goal of the Tutorial

This tutorial aims to guide you through the basics of managing Docker containers. By the end of this tutorial, you should understand the lifecycle of a Docker container, learn how to interact with containers, and manage them effectively.

What You Will Learn

In this tutorial, you will learn about:
* Creating Docker containers
* Starting, stopping and restarting Docker containers
* Deleting Docker containers
* Interacting with running Docker containers

Prerequisites

To follow this tutorial you should have:
* A basic understanding of Docker and its fundamentals
* Docker installed on your local machine

2. Step-by-Step Guide

Docker Containers Lifecycle

A Docker container's lifecycle begins with its creation and ends when it's deleted. The typical stages are:
* Creation
* Running
* Paused or Stopped
* Deletion

Creating Docker Containers

To create a Docker container, you will need to pull an image from Docker Hub or use an existing one on your local machine. The command to create a Docker container is:

docker run -d -p 8080:80 --name myContainer nginx

Starting, Stopping, and Restarting Docker Containers

To start, stop, or restart a Docker container, you can use the following commands respectively:

docker start myContainer
docker stop myContainer
docker restart myContainer

Deleting Docker Containers

To delete a Docker container, you need to stop it first then use the rm command:

docker stop myContainer
docker rm myContainer

3. Code Examples

Example 1: Creating and Running a Docker Container

# Pull the nginx image from Docker Hub
docker pull nginx

# Create and run a Docker container named 'myContainer'
docker run -d -p 8080:80 --name myContainer nginx

In this example, the pull command fetches the nginx image from Docker Hub. The run command creates a new Docker container named 'myContainer' from the nginx image.

Example 2: Stopping and Deleting a Docker Container

# Stop the Docker container
docker stop myContainer

# Delete the Docker container
docker rm myContainer

In this example, the stop command stops the 'myContainer' Docker container, and the rm command deletes it.

4. Summary

In this tutorial, you have learned how to manage Docker containers effectively. You now know how to create, start, stop, restart, and delete Docker containers. The next step for you is to learn about Docker images, Dockerfile, and Docker Compose.

5. Practice Exercises

Exercise 1:

Pull the httpd image from Docker Hub and create a Docker container named 'myHttpdContainer'.

Solution:

docker pull httpd
docker run -d -p 8081:80 --name myHttpdContainer httpd

Exercise 2:

Stop and delete the 'myHttpdContainer' Docker container.

Solution:

docker stop myHttpdContainer
docker rm myHttpdContainer

Exercise 3:

Create a Docker container using the alpine image and run the echo "Hello, World!" command.

Solution:

docker run alpine echo "Hello, World!"

In the third exercise, the run command creates a new Docker container from the alpine image and executes the echo "Hello, World!" command.

To practice more, try creating, starting, stopping, and deleting Docker containers using different images.