Docker Fundamentals: Run Your First Container
You will install Docker and run, inspect, and persist your first containers. By the end you will understand the difference between an image and a container and keep data alive with volumes.
Learning Objectives
- Run and manage containers from public images.
- Persist data with named volumes.
- Inspect container networking and logs.
- Time: ~2 hours · Difficulty: Beginner · Prereqs: a Linux host with sudo.
Architecture Overview
graph LR
Reg[(Docker Hub registry)] -->|docker pull| Img[Image]
Img -->|docker run| Ctr[Container]
Vol[(Named volume)] -->|mount| Ctr
You[You] -->|port 8080| Ctr
Environment Setup
You will need: Docker Engine 24.x (install docs).
Before you begin: add your user to the docker group or prefix commands with sudo.
Step-by-Step Execution
01
Run a container
An image becomes a container the moment you run it.
docker run -d --name web -p 8080:80 nginx:stableRuns nginx detached, mapping host 8080 to container 80.
02
List and inspect
$ docker ps
CONTAINER ID IMAGE STATUS PORTS
a1b2c3 nginx:stable Up 5 seconds 0.0.0.0:8080->80/tcp
03
Persist data with a volume
Volumes keep data when a container is removed, the key to stateful workloads.
docker volume create webdata && docker run -d --name web2 -v webdata:/usr/share/nginx/html nginx:stable04
Read container logs
docker logs --tail 10 webProgress So Far
graph LR
A[01 Run] -->|done| B[02 Inspect]
B -->|done| C[03 Volume]
C -->|done| D[04 Logs]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
style D fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080You should see 200. If so, your container is serving traffic and you have completed the lab.
Troubleshooting
- permission denied on docker.sock: add your user to the docker group and re-login.
- port already allocated: another service uses 8080; map a different host port.
- data lost on remove: mount a named volume rather than writing inside the container layer.
Extension Ideas
- Build your own image in Docker Images & Dockerfile.
- Connect multiple containers in Container Networking.
- Orchestrate with Docker Compose.
Key Results
- Launched a working web container in under 5 minutes.
- Persisted data across container removal with a named volume.
- Inspected status, ports, and logs of running containers.
- Verified the service with an HTTP 200 response.