0
0
Nginxdevops~5 mins

Docker Compose with Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Running multiple services together can be tricky. Docker Compose helps you start and manage several containers at once. Using it with Nginx lets you easily set up a web server alongside other services.
When you want to run a web server and an app together on your computer without conflicts
When you need to quickly start Nginx with custom settings for testing
When you want to share your web server setup with teammates easily
When you want to keep your web server and app isolated but connected
When you want to stop and start your web server and app with one command
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  web:
    image: nginx:1.23.4
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro

This file tells Docker Compose to run an Nginx container.

version sets the Compose file format.

services defines the containers to run. Here, web is the Nginx container.

image specifies the Nginx version to use.

ports maps port 8080 on your computer to port 80 in the container, so you can access the web server at http://localhost:8080.

volumes mounts your local nginx.conf file inside the container to customize Nginx settings.

Commands
This command starts the Nginx container in the background using the docker-compose.yml file. The -d flag means detached mode, so the terminal is free for other commands.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating web ... done
-d - Run containers in the background (detached mode)
This command shows the status of containers started by Docker Compose. It helps verify that the Nginx container is running.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports ---------------------------------------------------------------------------- web nginx -g 'daemon off;' Up 0.0.0.0:8080->80/tcp
This command fetches the homepage served by Nginx on your local machine to check if the web 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> </body> </html>
This command stops and removes the containers, network, and other resources created by Docker Compose. Use it to clean up when you are done.
Terminal
docker-compose down
Expected OutputExpected
Stopping web ... done Removing web ... done Removing network default
Key Concept

If you remember nothing else from this pattern, remember: Docker Compose lets you start and manage multiple containers together easily with one simple file.

Common Mistakes
Not mapping ports correctly in docker-compose.yml
If ports are not mapped, you cannot access Nginx from your computer browser.
Always map container port 80 to a free port on your computer, like 8080:80.
Forgetting to mount the nginx.conf file
Without mounting, Nginx uses default settings and ignores your custom configuration.
Use volumes to mount your local nginx.conf to /etc/nginx/nginx.conf inside the container.
Running docker-compose up without -d and closing the terminal
The containers stop when the terminal closes if not run in detached mode.
Use docker-compose up -d to run containers in the background.
Summary
Create a docker-compose.yml file to define the Nginx service with port mapping and config file mount.
Use 'docker-compose up -d' to start the Nginx container in the background.
Check running containers with 'docker-compose ps' and test Nginx with 'curl http://localhost:8080'.
Stop and remove containers with 'docker-compose down' when finished.