Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Docker Docker & Containers: From Zero to Production Docker Basics hands-on Module 1 — Container Fundamentals

Your First Container — Running hello-world and Beyond

Reviewed & accurate
AI Summary

What You'll Learn

Beginner

  • How to run your first Docker container
  • What happens behind the scenes when you run docker run
  • How to run containers interactively
  • How to pull images without running them

Step 1: Run hello-world

Open your terminal and run:

Terminalbash
docker run hello-world

You'll see output like this:

Outputbash
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image...
 4. The Docker daemon streamed that output to the Docker client...

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Congratulations!
You just ran your first Docker container. The message itself explains what Docker did — read it carefully.

What Just Happened?

Docker client sent the command run hello-world to the daemon.
Daemon checked if the hello-world image exists locally. It didn't.
Daemon pulled the image from Docker Hub (~13 KB).
Daemon created a container from the image.
Daemon started the container. The container's program ran, printed the message, and exited.
Daemon streamed the output back to your terminal.

Step 2: Run an Interactive Container

The hello-world container just prints and exits. Let's run something more useful — an interactive Ubuntu shell:

Terminalbash
docker run -it ubuntu bash

You're now inside an Ubuntu container. Your prompt changes to something like root@abc123:/#. Try these commands:

Inside the containerbash
# See what OS you're running
cat /etc/os-release

# List files
ls /

# Check who you are
whoami

# Install something
apt-get update && apt-get install -y curl

# Use it
curl https://example.com

# Leave the container
exit
What does -it mean?
  • -i (interactive): keeps STDIN open even if not attached
  • -t (tty): allocates a pseudo-TTY, giving you a proper terminal
  • Together: -it gives you an interactive shell inside the container

Step 3: Pull an Image Without Running

You don't have to run an image to download it. Use docker pull to pre-fetch:

Terminalbash
# Pull the Alpine Linux image (tiny — only 7 MB)
docker pull alpine

# Pull a specific version
docker pull ubuntu:22.04

# Pull the nginx web server
docker pull nginx

Now run docker images to see what you have locally:

Outputbash
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
alpine        latest    a606584aa9aa   2 weeks ago    7.8MB
nginx         latest    2b7d6430f78d   2 weeks ago    192MB
ubuntu        22.04     2dc39ba059dc   3 weeks ago    77.9MB
ubuntu        latest    2dc39ba059dc   3 weeks ago    77.9MB

Step 4: Run a Web Server

Let's run a real web server and access it from your browser:

Terminalbash
# Run nginx on port 8080
docker run -d -p 8080:80 --name my-web nginx

Now open your browser and go to http://localhost:8080. You'll see the nginx welcome page — served from inside a Docker container.

What do the flags mean?
  • -d (detached): runs in the background
  • -p 8080:80 (publish): maps host port 8080 to container port 80
  • --name my-web: gives the container a name (instead of a random one)

Step 5: Manage Your Container

Terminalbash
# See running containers
docker ps

# See all containers (including stopped)
docker ps -a

# Stop the web server
docker stop my-web

# Start it again
docker start my-web

# View logs
docker logs my-web

# Remove the container (must stop first)
docker rm my-web

The Container Lifecycle

created docker create start running docker run/start pause paused stop stopped docker stop rm removed docker rm restart (docker start) Container Lifecycle States
Container lifecycle — created → running → (paused) → stopped → removed

States explained

StateWhat it meansCommand
createdContainer exists but not starteddocker create
runningContainer is executingdocker run / docker start
pausedContainer is frozen (memory kept)docker pause
stoppedContainer exited, filesystem keptdocker stop
removedContainer is deleteddocker rm

Common Mistakes

Avoid these
  • Forgetting -it for interactive containers. Without -it, the container starts and exits immediately with no shell.
  • Not publishing ports. docker run nginx runs nginx but you can't access it. You need -p 8080:80.
  • Leaving containers running. Use docker ps and docker stop to clean up. Or add --rm to auto-remove on exit.
  • Forgetting that stopped containers still exist. docker ps only shows running. Use docker ps -a to see stopped ones.

Practical Exercise (10 minutes)

Run docker run hello-world — your first container
Run docker run -it ubuntu bash — interactive shell
Inside the container, install and use curl
Run docker run -d -p 8080:80 --name web nginx — web server
Open http://localhost:8080 in your browser
Run docker ps, docker logs web, docker stop web, docker rm web

Mini Challenge

Run the Python official image and start a Python REPL inside a container:

Challengebash
# Hint: use the python image with -it
docker run -it python python3

# Inside the Python REPL, try:
>>> print("Hello from inside a container!")
>>> import sys
>>> print(sys.version)

# Exit with Ctrl+D or exit()

Key Takeaways

  • docker run <image> = pull (if needed) + create + start a container.
  • -it gives you an interactive shell inside the container.
  • -d runs in the background; -p host:container publishes ports.
  • --name gives the container a friendly name instead of a random one.
  • Containers go through: created → running → (paused) → stopped → removed.
  • Use docker ps to see running containers, docker ps -a for all.
Previously: Lesson 05 installed Docker.
Today: You ran your first containers — hello-world, interactive Ubuntu, and an nginx web server.
Next: Lesson 07 covers the essential Docker CLI commands you'll use daily.

FAQ

Why does my container exit immediately?

Containers run as long as their main process (PID 1) is alive. If the process exits, the container stops. For an interactive shell, use -it. For a web server, the server process keeps running. For hello-world, the program prints and exits — so the container exits.

What does the --rm flag do?

--rm automatically removes the container when it exits. Useful for one-off commands: docker run --rm alpine echo hello. Prevents stopped containers from piling up.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments