Run your first web server with Docker

Run NGINX on your laptop with Docker, then follow a browser request into the container. Build your own image and run two versions side by side to see how a file change reaches the browser.

Open a browser tab and type http://127.0.0.1:8080. On a laptop with nothing listening there, the browser reports a connection error. By the end of this walkthrough, that same tab will display a page served by NGINX, a web server running on your own computer. The only software you need to install is Docker.

If you write backend code, there’s a second experiment worth trying. You’ll add one file to the server, package it, and see exactly when a change on your laptop reaches the browser. That distinction matters the first time a deployment serves yesterday’s code.

What Docker gives you

A web server is a program that waits for requests and sends back responses. To run one directly on your laptop, you usually install a version for your operating system and work out where it expects its files. Docker is a tool for packaging and running software in isolated environments called containers. With Docker installed, you can download a packaged copy of NGINX and run it with one command.

An image is a package. The NGINX image includes the server program, its libraries, a default configuration, and a welcome page. The image can sit on your disk while the server is off. A container is an instance Docker creates from the image. When you start that container, the web server runs inside it. You can create two containers from one image and stop either one independently. Docker’s introduction to containers

One image can start two containers Read left to right. Two separate containers start from the same NGINX image. Stopping the first leaves the second running. One image can start two containers NGINX image Stored on disk Container A Stopped Container B Running

Start the server

Follow the Docker installation guide for your operating system. If you installed Docker Desktop, open it and wait for the engine to start. The engine is the part that actually runs containers. On Windows, use Linux containers for this example.

Open Terminal on macOS or Linux, or PowerShell on Windows. Type the following command and press Enter.

docker version
Bash

Look for both Client and Server in the output. The client is the tool you just typed a command into. The server section tells you it reached the engine. If only the client appears alongside a connection error, start Docker Desktop or your Linux Docker Engine and try again.

Now run the following line. The first run needs internet access to download the image.

docker run --name first-web -d -p 127.0.0.1:8080:80 nginx:stable-alpine
Bash

Docker looks for the image on your computer. If it’s missing, Docker downloads it from Docker Hub, a registry where images are stored. Docker then creates and starts a container. A long string of letters and numbers is the container ID. Open http://127.0.0.1:8080 in your browser. You should see Welcome to nginx!

Read the command back against what just happened. nginx:stable-alpine selects the image named nginx and a variant identified by the tag stable-alpine. --name first-web gives the container a memorable name. The -d flag runs it in the background, so you can keep using the terminal while the browser talks to the server. Official NGINX image and Docker run reference

Why the browser uses port 8080

A port is a number that identifies where a program listens for network traffic. Here, NGINX listens on port 80 inside the container. Your browser connects to port 8080 on your laptop. The -p option in the command connects those two places.

How a browser request reaches NGINX Read left to right. The browser connects to port 8080 on the laptop. Docker forwards the request to NGINX on port 80 inside the container. How a browser request reaches NGINX Laptop 127.0.0.1:8080 Container NGINX on port 80

Read 127.0.0.1:8080:80 from left to right. The address 127.0.0.1 means this computer. The first port, 8080, belongs to your laptop. Docker forwards requests arriving there to the second port, 80, inside the container. Choosing the loopback address keeps this exercise local. Docker port publishing

If Docker says the port is already allocated, another program is using 8080. Retry the run command with --name first-web-alt and 127.0.0.1:8081:80, then visit http://127.0.0.1:8081. Use the alternate name for the remaining commands about this container. A failed start can leave a container behind. If docker ps -a shows the original first-web in the Created state from this attempt, docker rm first-web removes it.

Watch one request arrive

Refresh the welcome page, then run these two commands, one line at a time.

docker ps
docker logs --tail 10 first-web
Bash

In the first command’s output, find the row named first-web and look at its PORTS column. In the second, look for a line containing GET / and 200. That’s the browser asking for the home page and NGINX recording a successful response. You’ve now confirmed that Docker sees a running process and that NGINX handled your request. Those are separate checks when you’re debugging a service.

Try stopping it.

docker stop first-web
docker ps -a
Bash

The container now shows Exited. A fresh request to the page should fail. Start that same container again with docker start first-web and refresh. The page returns. This is the difference between start, which reuses an existing container, and run, which creates a new one.

Build an image with your own response

Suppose you want a teammate to open /status.json and see which release they’re running. You can put that file in an image alongside NGINX. This small example lets you practice the same build-and-run steps you’d use for a backend service.

In your terminal, create an empty project folder and enter it.

mkdir docker-intro
cd docker-intro
Bash

Using a plain-text editor, save two files in that folder. Name the first one status.json and give it this content. JSON is a text format that pairs names with values. Here, the service has a name and a release number.

{"service": "first-web", "release": 1}
JSON

Name the second file Dockerfile, with a capital D and no file extension. A Dockerfile describes how to build an image.

FROM nginx:stable-alpine
COPY status.json /usr/share/nginx/html/status.json

FROM starts with the same image you just ran. COPY puts your file into the directory from which NGINX serves web pages. Back in your terminal, run these commands from the folder containing both files.

docker build -t local-status:1 .
docker run --name status-one -d -p 127.0.0.1:9090:80 local-status:1
Bash

The dot tells Docker to use the current folder as the source of files for the build. The -t option gives the resulting image the name and tag local-status:1. Open http://127.0.0.1:9090/status.json. The response should contain "release": 1. Both servers can listen on port 80 inside their own containers because you gave them different ports on your laptop. Dockerfile reference

Now change the release number in your local file to 2, save it, and refresh the page. You’ll still see 1. The Dockerfile copied the file during the build. The running server reads that copy. Build a second image and give its container a different port so you can compare both releases side by side.

docker build -t local-status:2 .
docker run --name status-two -d -p 127.0.0.1:9091:80 local-status:2
Bash

Open http://127.0.0.1:9091/status.json. Port 9091 now serves release 2 while 9090 still serves release 1. If you’ve ever wondered why restarting a service didn’t pick up a code change, these two tabs show one reason. Each container is still using the image it was created from.

Clean up and take the idea back to your service

If you followed both exercises, stop and remove the three containers you created. If you stopped after the welcome page, use just first-web on each line. Substitute your alternate container name if you used one.

docker stop first-web status-one status-two
docker rm first-web status-one status-two
Bash

The containers are gone. Your two project files and the images are still available for the next run. Files written only inside a container are discarded when you remove it. For something like database records that need to survive replacement, Docker provides storage with its own lifecycle. Docker volumes

For a backend team, the two browser tabs capture the reason to package a service this way. You can build an image once, test that exact image, and deploy it with the configuration for each environment. When someone reports an unexpected response, check which image started the container and which port the request reached.

Share
Edit Template

Get new engineering deep dives

Scala, JVM, AI, and backend systems. I send practical articles when there is something worth reading.

Join the newsletter