0
0
Nginxdevops~5 mins

Official Nginx Docker image - Commands & Configuration

Choose your learning style9 modes available
Introduction
Running web servers can be complex because of setup and dependencies. The official Nginx Docker image lets you run Nginx easily inside a container, so you get a ready-to-use web server without manual installation.
When you want to quickly serve static web pages without installing software on your computer.
When you need a lightweight web server to test your website locally.
When you want to run multiple web servers on the same machine without conflicts.
When you want to deploy a web server in a consistent environment across different computers.
When you want to isolate your web server from other applications for security.
Config File - default.conf
default.conf
server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

This file configures Nginx to listen on port 80 and serve files from the default web directory inside the container. It also defines a simple error page for server errors.

Commands
This command downloads the official Nginx Docker image version 1.25.2 from Docker Hub to your local machine.
Terminal
docker pull nginx:1.25.2
Expected OutputExpected
1.25.2: Pulling from library/nginx Digest: sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Status: Downloaded newer image for nginx:1.25.2 docker.io/library/nginx:1.25.2
This command starts a new container named 'my-nginx' from the Nginx image, runs it in the background, and maps port 8080 on your computer to port 80 inside the container so you can access the web server.
Terminal
docker run --name my-nginx -d -p 8080:80 nginx:1.25.2
Expected OutputExpected
a1b2c3d4e5f678901234567890abcdef1234567890abcdef1234567890abcdef
--name - Assigns a name to the container for easy reference.
-d - Runs the container in detached mode (in the background).
-p - Maps a port on the host to a port in the container.
This command lists all running containers so you can verify that your Nginx container is running.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1b2c3d4e5f6 nginx:1.25.2 "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp my-nginx
This command fetches the default web page served by Nginx on your local machine to confirm the server is working.
Terminal
curl http://localhost:8080
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working.</p> </body> </html>
Key Concept

If you remember nothing else from this pattern, remember: the official Nginx Docker image lets you run a ready web server quickly and consistently inside a container.

Common Mistakes
Not mapping the container port to a host port with -p flag.
Without port mapping, you cannot access the web server from your computer browser.
Always use -p host_port:container_port to expose Nginx outside the container.
Running the container without -d flag and expecting it to run in background.
Without -d, the container runs in the foreground and blocks your terminal.
Use -d to run the container detached so you can continue using the terminal.
Using 'latest' tag instead of a specific version.
The 'latest' tag can change unexpectedly causing inconsistent environments.
Use a specific stable version like 'nginx:1.25.2' for predictable results.
Summary
Pull the official Nginx image with a specific version to ensure stability.
Run the container with port mapping and detached mode to serve web pages accessible on your computer.
Verify the container is running and test the web server by fetching the default page.