0
0
Nginxdevops~5 mins

Volume mounting for configs in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to change your web server settings without rebuilding your container. Volume mounting lets you use configuration files from your computer inside the container, so you can update settings easily.
When you want to test new nginx settings without rebuilding the Docker image.
When you need to keep your configuration files outside the container for easier updates.
When running multiple containers that share the same configuration files.
When you want to keep your container lightweight by not including config files inside it.
When you want to quickly fix a config error by editing the file on your host machine.
Config File - nginx.conf
nginx.conf
events {}
http {
    server {
        listen 80;
        location / {
            add_header Content-Type text/plain;
            return 200 'Hello from mounted config!';
        }
    }
}

This is a simple nginx configuration file.

events {} is required but empty here.

http block defines the web server settings.

The server listens on port 80 and responds with a plain text message.

Commands
This command runs an nginx container named 'my-nginx'. It mounts the local nginx.conf file into the container's config path as read-only. It also maps port 8080 on your computer to port 80 in the container so you can access the server.
Terminal
docker run --name my-nginx -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro -p 8080:80 -d nginx:1.25.2
Expected OutputExpected
a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0
-v - Mounts the local config file into the container
-p - Maps container port 80 to host port 8080
-d - Runs the container in detached mode
This command checks if nginx is running and serving the content from the mounted config file by requesting the root URL.
Terminal
curl -s http://localhost:8080
Expected OutputExpected
Hello from mounted config!
This command shows the logs of the nginx container to verify it started correctly and is using the mounted config.
Terminal
docker logs my-nginx
Expected OutputExpected
2024/06/01 12:00:00 [notice] 1#1: using the configuration file: /etc/nginx/nginx.conf
This command stops and removes the nginx container to clean up after testing.
Terminal
docker stop my-nginx && docker rm my-nginx
Expected OutputExpected
my-nginx my-nginx
Key Concept

If you remember nothing else from this pattern, remember: mounting your config file as a volume lets you change nginx settings instantly without rebuilding the container.

Common Mistakes
Not using the full path to the local config file in the volume mount.
Docker won't find the file and the container will use the default config instead.
Use an absolute or correct relative path like $(pwd)/nginx.conf to ensure Docker finds your config file.
Forgetting to map the container port to a host port with -p.
You won't be able to access nginx from your browser or curl on your host machine.
Always include -p 8080:80 (or your chosen ports) to expose nginx outside the container.
Mounting the config file without :ro (read-only) flag.
The container might accidentally modify your local config file.
Add :ro to the volume mount to protect your local config file from changes.
Summary
Use the -v flag to mount your local nginx.conf file into the container at /etc/nginx/nginx.conf.
Map container port 80 to a host port with -p to access nginx from your computer.
Verify nginx serves content using curl and check container logs for config usage.
Stop and remove the container when done to keep your environment clean.