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 dockerVerify installation:
docker --versionStep 2: Add User to Docker Group (Optional)
Run Docker without sudo:
sudo usermod -aG docker $USER newgrp dockerStep 3: Pull Your First Image
Pull the official Nginx image:
docker pull nginx:latestVerify the image:
docker imagesStep 4: Run Your First Container
Run a container from the Nginx image:
docker run -d -p 8080:80 --name my-nginx nginxExplanation:
-d: Run in detached mode (background)-p 8080:80: Map port 8080 on host to port 80 in container--name my-nginx: Name the containernginx: Image to use
Step 5: Verify Container is Running
List running containers:
docker psTest the web server:
curl http://localhost:8080Or 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 logsStep 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/bashThe -it flags provide an interactive terminal.
Step 8: Stop and Remove Container
Stop the container:
docker stop my-nginxRemove the container:
docker rm my-nginxOr remove in one command:
docker rm -f my-nginxValidation 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 -ato 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