# docker / practice / nginx

Build & Run an Nginx Container

Today I practiced creating a simple Docker image using Nginx and running it as an isolated container. Here is my step-by-step process, the code used, and the crucial troubleshooting lessons I learned along the way.

Target: http://localhost:88
Containershan
Base Imagenginx:latest
Port MapHost: 88 → Container: 80
StatusOnline & Serving
-d
Detached
COPY
index.html
# step-01

The Dockerfile

DockerfileDOCKERFILE
1 2 3 4
FROM nginx
WORKDIR /usr/share/nginx/html
COPY index.html /usr/share/nginx/html
EXPOSE 80

Instruction Breakdown

  • FROM

    Uses the official Nginx image from Docker Hub as the starting base image.

  • WORKDIR

    Sets the working directory inside the container where the website files will be stored natively by Nginx.

  • COPY

    Copies the local index.html file from your host system into the Nginx web directory inside the container.

  • EXPOSE

    Documents that the container application listens on port 80. (Note: this does not publish the port, it's just documentation).

# step-02

Build and Run the Container

After creating the Dockerfile, the next step is to build the image locally and execute it. We will map port 88 on our host machine to port 80 inside the container.

Run Options Explained

  • -d

    Runs the container in detached mode (in the background).

  • -p 88:80

    Port Mapping: Maps host port 88 to container port 80.

  • --name shan

    Assigns a friendly, memorable name to the running container.

  • shan

    The name of the Docker image we just built to run.

terminal
# 1. Build the Docker Image
# (Builds image using Dockerfile in current dir and tags it 'shan')
$ docker build -t shan .
Successfully built image 'shan'
# 2. Run the Docker Container
$ docker run -d -p 88:80 --name shan shan
a1b2c3d4e5f6...
# 3. Access the Website
Open your browser and navigate to:
http://localhost:88
# mistakes

Troubleshooting & Mistakes

While completing this task, I made a few mistakes. Debugging them taught me some of the most important rules of Docker operations.

MISTAKE 1

Wrong Docker Run Option: Initially, I tried to run the container using -n to set the container name. Docker does not support -n for naming; the correct option is --name.

MISTAKE 2

Option Placement: I placed options after the image name. In Docker, options/flags must be written before the image name in the command string.

MISTAKE 3

Port Mapping Mistake: I mapped port 88:88. However, Nginx natively runs on port 80 by default. I learned that mapping strictly follows: Host Port → Container Port.

MISTAKE 4

Misunderstanding EXPOSE: I thought EXPOSE 88 in the Dockerfile would change the Nginx port. I learned that EXPOSE is only for documentation and doesn't actually modify the app's internal configuration.

MISTAKE 5

File Path Confusion: I set WORKDIR to /usr/share/nginx/html but tried to copy files to /var/www/html. The file paths inside the container must exactly match the service configuration you are using (Nginx uses the former).

# takeaways

What I Learned

architecture

Image Definition

Dockerfile instructions explicitly define exactly how the image is built, layer by layer.

networking

Ports & EXPOSE

EXPOSE only documents container ports for developers. Actual port mapping is done using docker run -p.

configuration

Internal Alignment

Application ports and file paths mapped inside containers must strictly match the default configuration of the service (like Nginx) running inside.