# 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.
The Dockerfile
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.htmlfile 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).
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.
Troubleshooting & Mistakes
While completing this task, I made a few mistakes. Debugging them taught me some of the most important rules of Docker operations.
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.
Option Placement: I placed options after the image name. In Docker, options/flags must be written before the image name in the command string.
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.
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.
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).
What I Learned
Image Definition
Dockerfile instructions explicitly define exactly how the image is built, layer by layer.
Ports & EXPOSE
EXPOSE only documents container ports for developers. Actual port mapping is done using docker run -p.
Internal Alignment
Application ports and file paths mapped inside containers must strictly match the default configuration of the service (like Nginx) running inside.