2.4 Lab - The Docker Lifecycle

Introduction

This hands-on lab is designed to give you practical experience with managing the Docker container lifecycle. From creating and running containers to stopping, pausing, and ultimately removing them, this lab will guide you through each stage using Docker CLI commands. By working directly in a Linux environment and utilizing the Nano editor for any file editing needs, you'll gain firsthand experience with the operational aspects of Docker containers.

Objectives

Lab Steps

Step 1: Preparing Your Environment

Step 2: Creating and Running a Container

docker pull nginx:latest
docker run -d --name my-nginx nginx:latest
docker ps

You should see output like the following:

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
a95c1ed230d6   nginx:latest   "/docker-entrypoint.…"   7 seconds ago   Up 5 seconds   80

Step 3: Inspecting a Container

docker inspect my-nginx

You will see a lot of detail about the container.

Step 4: Accessing Container Logs

docker logs my-nginx

You should see output like the following:

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/02/10 20:40:15 [notice] 1#1: using the "epoll" event method
2024/02/10 20:40:15 [notice] 1#1: nginx/1.25.3
2024/02/10 20:40:15 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 
2024/02/10 20:40:15 [notice] 1#1: OS: Linux 6.2.0-1016-aws
2024/02/10 20:40:15 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/02/10 20:40:15 [notice] 1#1: start worker processes
2024/02/10 20:40:15 [notice] 1#1: start worker process 83
2024/02/10 20:40:15 [notice] 1#1: start worker process 84

Step 5: Pausing and Resuming the Container

docker pause my-nginx
docker ps
docker unpause my-nginx
docker ps

You should see output like the following:

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
a95c1ed230d6   nginx:latest   "/docker-entrypoint.…"   7 seconds ago   Up 5 seconds   80/tcp    my-nginx

Step 6: Stopping and Removing the Container

docker stop my-nginx
docker rm my-nginx
docker ps -a

Summary

In this lab, you've successfully navigated through the key stages of the Docker container lifecycle. Starting with pulling an image and running it as a container, you've learned how to inspect containers to understand their configuration and state. You've also gained experience with managing container states by pausing and resuming a container, and you've practiced cleaning up by stopping and removing a container. These skills are foundational for anyone working with Docker, providing the ability to manage containers effectively. Through hands-on experience, this lab has reinforced the importance of understanding the Docker lifecycle for efficient container orchestration and resource management.