Today I practiced creating a simple Docker image using Nginx and running it as a container.
1. Dockerfile
FROM nginx WORKDIR /usr/share/nginx/html COPY index.html /usr/share/nginx/html EXPOSE 80
Explanation
FROM nginx: Uses the official Nginx image from Docker Hub as the base image.WORKDIR /usr/share/nginx/html: Sets the working directory inside the container where the website files will be stored.COPY index.html /usr/share/nginx/html: Copies theindex.htmlfile from the local system to the Nginx web directory inside the container.EXPOSE 80: Documents that the container application runs on port 80.
2. Build the Docker Image
After creating the Dockerfile, the next step is to build the image.
docker build -t shan .
This command builds a Docker image using the Dockerfile in the current directory and assigns the image name shan.
3. Run the Docker Container
Once the image is built, the container can be started.
docker run -d -p 88:80 --name shan shan
Explanation of Options:
-d: Runs the container in detached mode.-p 88:80: Maps host port 88 to container port 80.--name shan: Assigns a name to the container.shan: The Docker image name.
http://localhost:88
Lessons Learned & Troubleshooting
While doing this task, I made a few mistakes and learned important Docker concepts:
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.
I placed options after the image name. In Docker, options must be written before the image name.
I mapped port 88:88. However, Nginx runs on 80 by default. I learned mapping follows: Host Port → Container Port.
I thought EXPOSE 88 would change the Nginx port. I learned EXPOSE is only for documentation and doesn't modify the app config.
I set WORKDIR to /usr/share/nginx/html but copied to /var/www/html. The paths must match the service configuration.
What I Learned
- Dockerfile instructions define how the image is built.
EXPOSEonly documents container ports.- Port mapping is done using
docker run -p. - Application ports must match container configuration.
- File paths inside containers must match the service configuration.