Run Your First Container (and Meet nginx)¶
In the last lesson you ran docker run hello-world and saw a friendly message. That was your first container - but it ran for a split second and then vanished. In this lesson we'll slow down, understand what actually happened, and then run a real web server you can open in your browser. 🌐
What we will do (in very simple steps)¶
- Understand what
hello-worldactually did - Run a real web server called nginx
- Open it in your browser
- Check on it, then clean it up
Step 1: What did hello-world actually do?¶
When you ran that command, Docker quietly did four things:
- Looked for the
hello-worldimage on your computer - Downloaded it (because you didn't have it yet)
- Started a container from that image
- The container printed its message and immediately finished
Remember our kitchen analogy? The image is the packaged meal (a template, sitting on a shelf). A container is that meal actually being served. The hello-world meal is tiny - it says hello and it's done, like a single bite. 🍬
Most real containers aren't like that. They stay running and keep doing a job - which is exactly what we'll see next.
Step 2: Run a real web server¶
We're going to run nginx (say it "engine-x"), a popular web server. Unlike hello-world, it stays running and waits to serve web pages.
Run this:
That's a few new pieces, so here's each one in plain English:
docker run- start a new container-d- "detached": run it in the background so you get your prompt back-p 8080:80- connect port 8080 on your computer to port 80 inside the container (think of it as running a hallway between your door and the container's door)--name my-web- give the container a friendly name so it's easy to refer to laternginx- the image to run
Docker will download nginx the first time (a few seconds), then print a long string of letters and numbers. That's the container's ID, and it means nginx is now running in the background. ✅
💡 Why port 8080 and not 80? Port 80 is often already in use or needs special permissions. 8080 is a safe, free choice for local practice.
Step 3: See it in your browser¶
Open your web browser and go to:
You should see a page that says "Welcome to nginx!" 🎉
Take a second to appreciate this: a fully working web server is running on your machine, and you never installed nginx directly. It came fully packaged inside the container.
Step 4: Check on your container¶
Back in your terminal, run:
ps lists your running containers. You'll see something like this (it's wide, so it may wrap on your screen):
That confirms your my-web container is Up (running) and shows the port hallway we built: 8080->80.
Step 5: Stop and clean up¶
Let's switch it off tidily. First stop it:
Then remove it completely:
- stop turns the container off (like switching off the appliance)
- rm removes it entirely (throws the switched-off container away)
Run docker ps again and the list will be empty - your container is gone. Don't worry, we'll go much deeper into stopping, starting, and removing in the Container Lifecycle lesson. This was just tidying up after ourselves.
✅ Checkpoint¶
You've finished this lesson if:
docker run -d -p 8080:80 --name my-web nginxstarted without errors- http://localhost:8080 showed the "Welcome to nginx!" page
docker pslistedmy-webas Updocker stop my-webanddocker rm my-webleftdocker psempty
🩹 Common hiccups¶
- "port is already allocated": something is already using 8080. Either stop that program, or pick another port, e.g.
-p 8081:80, and visithttp://localhost:8081. - "The container name '/my-web' is already in use": you still have a container with that name from a previous try. Remove it with
docker rm -f my-web, then run the command again. - Browser page won't load: check
docker psshows the container as Up. If it's missing, the container may have failed to start - re-run thedocker runcommand and read any error message.
Next up: Understanding Images vs Containers - the single most useful mental model in all of Docker.