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:
docker run hello-world
You'll see output like this:
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
What Just Happened?
run hello-world to the daemon.hello-world image exists locally. It didn't.Step 2: Run an Interactive Container
The hello-world container just prints and exits. Let's run something more useful — an interactive Ubuntu shell:
docker run -it ubuntu bash
You're now inside an Ubuntu container. Your prompt changes to something like root@abc123:/#. Try these commands:
# 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
-i(interactive): keeps STDIN open even if not attached-t(tty): allocates a pseudo-TTY, giving you a proper terminal- Together:
-itgives 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:
# 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:
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:
# 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.
-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
# 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
States explained
| State | What it means | Command |
|---|---|---|
| created | Container exists but not started | docker create |
| running | Container is executing | docker run / docker start |
| paused | Container is frozen (memory kept) | docker pause |
| stopped | Container exited, filesystem kept | docker stop |
| removed | Container is deleted | docker rm |
Common Mistakes
- Forgetting
-itfor interactive containers. Without-it, the container starts and exits immediately with no shell. - Not publishing ports.
docker run nginxruns nginx but you can't access it. You need-p 8080:80. - Leaving containers running. Use
docker psanddocker stopto clean up. Or add--rmto auto-remove on exit. - Forgetting that stopped containers still exist.
docker psonly shows running. Usedocker ps -ato see stopped ones.
Practical Exercise (10 minutes)
docker run hello-world — your first containerdocker run -it ubuntu bash — interactive shellcurldocker run -d -p 8080:80 --name web nginx — web serverhttp://localhost:8080 in your browserdocker ps, docker logs web, docker stop web, docker rm webMini Challenge
Run the Python official image and start a Python REPL inside a container:
# 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.-itgives you an interactive shell inside the container.-druns in the background;-p host:containerpublishes ports.--namegives the container a friendly name instead of a random one.- Containers go through: created → running → (paused) → stopped → removed.
- Use
docker psto see running containers,docker ps -afor all.
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.
Comments
Comments
Post a Comment