📖 Scenario: You are setting up an nginx web server using Docker. To customize the server, you want to mount a configuration file from your local machine into the container. This way, you can easily change the server settings without rebuilding the image.
🎯 Goal: Learn how to mount a local nginx.conf file into the Docker container using a volume mount. This will let the container use your custom configuration.
📋 What You'll Learn
Create a local nginx.conf file with basic server settings
Write a Docker run command that mounts the local nginx.conf into the container's /etc/nginx/nginx.conf
Use the official nginx image from Docker Hub
Verify the container uses the mounted configuration
💡 Why This Matters
🌍 Real World
Developers and system administrators often customize server settings by mounting config files into containers. This makes updates fast and safe.
💼 Career
Understanding volume mounts is essential for managing containerized applications in real environments, a key skill for DevOps roles.
Progress0 / 4 steps
1
Create a local nginx configuration file
Create a file named nginx.conf in your current directory with this exact content:
This file tells nginx to listen on port 8080 and respond with a simple message.
2
Write the Docker run command with volume mount
Write a Docker command to run the official nginx image, mounting your local nginx.conf file into the container at /etc/nginx/nginx.conf. Use the option -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf in the command.
Nginx
Hint
The -v option mounts your local file into the container. $(pwd) gets your current directory path.
3
Run the container and check the server
Run the Docker command you wrote. Then use curl http://localhost:8080 to check the server response. The response should be Hello from custom config!.
Nginx
Hint
The curl command fetches the page from your running nginx server.
4
Display the server response
Print the output of curl http://localhost:8080. It should exactly show Hello from custom config!.
Nginx
Hint
If you see this message, your volume mount worked and nginx used your config.
Practice
(1/5)
1. What does volume mounting with the -v flag in Docker allow you to do with nginx configuration files?
easy
A. Run nginx without any configuration files
B. Automatically update nginx to the latest version
C. Disable nginx logging inside the container
D. Use your own config files inside the container without rebuilding the image
Solution
Step 1: Understand volume mounting purpose
Volume mounting lets you link files or folders from your computer into the container.
Step 2: Apply to nginx config files
Mounting nginx config files lets nginx inside the container use your custom settings without rebuilding the image.
Final Answer:
Use your own config files inside the container without rebuilding the image -> Option D
Quick Check:
Volume mounting = Use custom configs [OK]
Hint: Mount with -v to use your config files directly [OK]
Common Mistakes:
Thinking volume mounting updates nginx version
Assuming nginx runs without config files
Confusing volume mounting with disabling logs
2. Which of the following is the correct syntax to mount a local nginx config file /home/user/nginx.conf into the container's config path /etc/nginx/nginx.conf as read-only?
easy
A. docker run -v /home/user/nginx.conf:/etc/nginx/nginx.conf nginx
B. docker run -v /home/user/nginx.conf:/etc/nginx/nginx.conf:ro nginx
C. docker run -v /etc/nginx/nginx.conf:/home/user/nginx.conf:ro nginx
D. docker run -v /home/user/nginx.conf:/etc/nginx/nginx.conf:rw nginx
Solution
Step 1: Identify correct volume mount syntax
The syntax is -v local_path:container_path:options. To make it read-only, use :ro.
Step 2: Match local and container paths
Local file is /home/user/nginx.conf, container path is /etc/nginx/nginx.conf, and mount should be read-only.
Final Answer:
docker run -v /home/user/nginx.conf:/etc/nginx/nginx.conf:ro nginx -> Option B
Quick Check:
Correct syntax with :ro = docker run -v /home/user/nginx.conf:/etc/nginx/nginx.conf:ro nginx [OK]
Hint: Use -v local:container:ro for read-only mounts [OK]
Common Mistakes:
Swapping local and container paths
Forgetting the :ro for read-only
Using :rw which is default but not read-only
3. Given the command: docker run -v /configs/nginx.conf:/etc/nginx/nginx.conf:ro nginx What will happen if you try to edit /etc/nginx/nginx.conf inside the running container?
medium
A. The file will be read-only and edits will fail
B. The file will be editable and changes saved
C. The file will be deleted after editing
D. The container will crash immediately
Solution
Step 1: Understand :ro flag effect
The :ro flag mounts the file as read-only inside the container.
Step 2: Predict editing behavior
Trying to edit a read-only file will fail because the container cannot write to it.
Final Answer:
The file will be read-only and edits will fail -> Option A
Quick Check:
:ro means read-only = edits fail [OK]
Hint: :ro means no write allowed inside container [OK]
Common Mistakes:
Assuming edits save despite :ro
Thinking file gets deleted on edit
Believing container crashes on edit
4. You ran this command: docker run -v /myconfigs/nginx.conf:/etc/nginx/nginx.conf nginx But nginx inside the container is still using the default config. What is the most likely mistake?
medium
A. The container must be run with --privileged to mount volumes
B. You forgot to add :ro to the volume mount
C. The local file path /myconfigs/nginx.conf does not exist or is incorrect
D. You need to rebuild the nginx image after mounting
Solution
Step 1: Check local file existence
If the local file path is wrong or missing, Docker mounts an empty or default file, so nginx uses default config.
Step 2: Understand volume mount behavior
Mounting requires the local file to exist. Missing :ro is not mandatory. Rebuilding image or privileged mode is unrelated.
Final Answer:
The local file path /myconfigs/nginx.conf does not exist or is incorrect -> Option C
Quick Check:
Missing local file = default config used [OK]
Hint: Check local file path exists before mounting [OK]
Common Mistakes:
Assuming :ro is required
Thinking image rebuild is needed
Believing privileged mode is necessary
5. You want to run nginx in Docker using a custom config directory /home/user/nginx-config with multiple config files. Which command correctly mounts the entire directory as read-only inside the container at /etc/nginx/conf.d?
hard
A. docker run -v /home/user/nginx-config:/etc/nginx/conf.d:ro nginx
B. docker run -v /home/user/nginx-config/nginx.conf:/etc/nginx/conf.d:ro nginx
C. docker run -v /home/user/nginx-config:/etc/nginx/nginx.conf:ro nginx
D. docker run -v /home/user/nginx-config:/etc/nginx/conf.d nginx
Solution
Step 1: Identify directory vs file mount
To mount multiple config files, mount the whole directory, not a single file.
Step 2: Apply read-only flag and correct paths
Mount /home/user/nginx-config to /etc/nginx/conf.d with :ro for safety.
Final Answer:
docker run -v /home/user/nginx-config:/etc/nginx/conf.d:ro nginx -> Option A
Quick Check:
Mount directory with :ro = docker run -v /home/user/nginx-config:/etc/nginx/conf.d:ro nginx [OK]
Hint: Mount whole config folder with :ro for multiple files [OK]
Common Mistakes:
Mounting a single file instead of directory
Mounting to wrong container path
Forgetting :ro for read-only safety
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.