Lab: Setup Your First Docker Container

Ad Space

Lab Objective

In this beginner lab, you will:

  • Install Docker on your system
  • Pull your first Docker image
  • Run a container from the image
  • Interact with a running container
  • Stop and remove containers

Estimated Time: 30 minutes

Prerequisites

  • Linux system (Ubuntu/Debian recommended) or Docker Desktop
  • Internet connection
  • Basic command line knowledge

Architecture Overview

This lab introduces the Docker workflow:

  • Docker Image: Template for creating containers
  • Docker Container: Running instance of an image
  • Docker Hub: Registry for Docker images

Ad Space

Lab Steps

Step 1: Install Docker

On Ubuntu/Debian:

sudo apt update sudo apt install docker.io -y sudo systemctl start docker sudo systemctl enable docker

Verify installation:

docker --version

Step 2: Add User to Docker Group (Optional)

Run Docker without sudo:

sudo usermod -aG docker $USER newgrp docker

Step 3: Pull Your First Image

Pull the official Nginx image:

docker pull nginx:latest

Verify the image:

docker images

Step 4: Run Your First Container

Run a container from the Nginx image:

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

Explanation:

  • -d: Run in detached mode (background)
  • -p 8080:80: Map port 8080 on host to port 80 in container
  • --name my-nginx: Name the container
  • nginx: Image to use

Step 5: Verify Container is Running

List running containers:

docker ps

Test the web server:

curl http://localhost:8080

Or open http://localhost:8080 in a browser.

Step 6: View Container Logs

Check container logs:

docker logs my-nginx docker logs -f my-nginx  # Follow logs

Step 7: Execute Commands in Container

Run a command inside the container:

docker exec my-nginx ls /usr/share/nginx/html docker exec -it my-nginx /bin/bash

The -it flags provide an interactive terminal.

Step 8: Stop and Remove Container

Stop the container:

docker stop my-nginx

Remove the container:

docker rm my-nginx

Or remove in one command:

docker rm -f my-nginx

Validation Checklist

  • Docker is installed and running
  • Successfully pulled Nginx image
  • Container runs and is accessible on port 8080
  • Can view container logs
  • Can execute commands inside container
  • Successfully stopped and removed container

Ad Space

Additional Exercises

  • Pull and run a different image (e.g., alpine, ubuntu)
  • Run multiple containers simultaneously
  • Use docker ps -a to see all containers (including stopped)
  • Practice removing images with docker rmi

Key Takeaways

  • Docker images are templates for containers
  • Containers are isolated, portable environments
  • Port mapping allows access to container services
  • Containers can be started, stopped, and removed easily