Containerizing Nginx with Docker for Web Serving

Creating a Docker container for Nginx involves defining the container configuration and building an image. Begin by creating a Dockerfile in a new directory with the following instructions:

FROM nginx:alpine

# Optional: Replace default configuration with custom file
COPY nginx.conf /etc/nginx/nginx.conf

# Optional: Add static content to default web root
# COPY ./web-content /usr/share/nginx/html

# Expose HTTP port
EXPOSE 80

# Launch Nginx in foreground mode
CMD ["nginx", "-g", "daemon off;"]

The base image nginx:alpine provides a lightweight Nginx installasion. Custom configuration files can be injected using the COPY instruction. Static website files can be placed in the container's document root directory. The EXPOSE directive documents the network port, while the CMD instruction specifies the process to run when the container starts.

Build the container image using the docker build command from the directory containing the Dockerfile:

docker build -t nginx-container .

The -t flag assigns a tag name to the resulting image, and the dot indicates the build context is the current directory.

Launch the containerized Nginx service with:

docker run -d -p 8080:80 nginx-container

This command runs the container in detached mode and maps port 8080 on the host to port 80 in the container. Accessing http://localhost:8080 will display the Nginx welcome page if the deploymant was succesfull.

Tags: nginx docker containerization web-server

Posted on Mon, 01 Jun 2026 16:35:48 +0000 by beemzet