Your First Container
Estimated time: 15 minutes
You will run a real web server in a container, view it running, then stop and remove it. This is the basic lifecycle you will repeat constantly.
Before You Build Your Own Image
Building your own image (for your Next.js app) comes in the next section. Before that, it helps to practice the basic commands using an existing public image — so the commands feel familiar once you start building your own.
We will use nginx, a popular web server, as this practice image.
Running a Container
Start nginx
docker run -d -p 8080:80 --name my-first-container nginxHere is what each part means:
docker run— create and start a container from an image-d— run in the background (“detached”), so your terminal stays free-p 8080:80— connect port8080on your machine to port80inside the container (this is how your browser reaches it)--name my-first-container— give the container a name you can reference laternginx— the image to use (Docker pulls it from Docker Hub automatically if you do not have it yet)
Open It in a Browser
Go to http://localhost:8080 . You should see the nginx welcome page.
This page is being served from inside a container, not from a program installed on your machine. If you stop the container, the page disappears.
Viewing Running Containers
To see what containers are currently running:
docker psOutput looks like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx "/docker-entrypoint..." 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp my-first-containerThe real output is wide and often wraps in a normal terminal — that is expected, not an error. The columns that matter most for now are STATUS (is it running?) and NAMES (which container is it?).
docker ps only shows running containers. To see all containers, including stopped ones, add -a: docker ps -a
Stopping the Container
Stopping a container ends the running process, but does not delete it.
docker stop my-first-containerRun docker ps -a again — the container is still listed, just no longer running.
Removing the Container
Once you are done with a container, you can remove it completely:
docker rm my-first-containerdocker rm deletes the container permanently. Any data that only existed inside that container is gone. The image itself (nginx) is untouched — you can create a new container from it anytime.
The Lifecycle So Far
docker run → container created and running
docker ps → see it running
docker stop → container stopped, still exists
docker rm → container deleted completelyHands-on Task
Do this yourself before moving on:
- Run an nginx container named
practice-containeron port9090instead of8080. - Confirm it works by opening
http://localhost:9090in your browser. - Check it is listed with
docker ps. - Stop it, confirm with
docker ps -athat it still exists but is stopped. - Remove it.
Quick Check
- Can you run a container and reach it in your browser?
- Do you understand the difference between
stopandrm? - Can you explain what
-p 8080:80does?
Next → Essential Commands