By the end of this guide, you will know how to:
- Download and install Docker
- Set up an Nginx Docker container
- Configure Nginx to reverse proxy requests to other Docker containers
Benefits of Running Nginx Inside Docker
- Running Nginx inside a Docker container makes installation and startup simpler than dealing with package managers and OS-specific commands.
- Nginx upgrades can be completed by swapping the container for an updated version.
- Once the basics are set up, launching services in Docker and using Nginx to reverse proxy requests to them becomes fast and consistent.
Note: This guide covers setup on Ubuntu. Basic Linux knowledge is assumed, and the linked documentation is the source of truth for OS-specific changes.
1. Remove Previous Docker Installations
- Review the Docker installation docs.
- Optionally uninstall previous versions.
- Run
sudo apt-get remove docker docker-engine docker.io containerd runc.
2. New Host Setup
Before installing Docker Engine for the first time on a new host, set up the Docker repository.
- Update the
aptindex and install dependencies.
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release
- Add Docker’s official GPG key.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
- Set up the stable repository for
x86_64/amd64. - Consult the Ubuntu install docs for other architectures.
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
3. Install Docker
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
- Test the installation by running Docker’s hello-world container.
sudo docker run hello-world
4. Download and Start an Nginx Container
- Pull the latest Nginx image.
sudo docker pull nginx
- Start the container.
- Use
--nameto name it. - Use
-pto mapexternal-port:container-port. - Use
-dto run in detached mode.
sudo docker run --name my-nginx-container -p 80:80 -d nginx
Note: Mapping port
80on the host to port80in the container means incoming web traffic reaches the Nginx container first, where Nginx can evaluate and forward it.
- Check that the container is running.
sudo docker ps -a
5. Create Nginx Server Blocks for Other Services
- Create a file for the Nginx configuration.
- Run
touch my-nginx-conf.confd. - Edit the file.
- Run
nano my-nginx-conf.confd. - Move the file to a directory of your choice so it can be mounted into the container.
- Run
mv my-nginx-conf.confd path/to/local/directory.
server {
listen 80;
server_name domain.com www.domain.com *.domain.com;
root /path/to/static/files/;
location /images {
root /path/to/images
}
}
server {
listen 80;
server_name domain.com www.domain.com *.domain.com;
location / {
proxy_pass http://127.0.0.1:3001;
}
location /api {
proxy_pass http://127.0.0.1:3002;
}
}
6. Map a Local Nginx Directory into the Container
Add a -v volume flag when starting the Nginx container. The format is local_directory:container_directory.
sudo docker run --name my-nginx-container -p 80:80 -d -v local/conf/directory:/etc/nginx/conf.d nginx
When the container starts, Nginx will read configuration files from /etc/nginx/conf.d inside the container, backed by the mounted local directory.
7. Restart the Nginx Container
Nginx will not pick up the new configuration until the container is restarted.
sudo docker restart <CONTAINER_NAME>
Conclusion
You have now:
- Installed Docker
- Started an Nginx Docker container
- Configured Nginx to reverse proxy requests to other containers
Docker Commands
| Description | Command |
|---|---|
| List images | docker images |
| List running containers | docker ps |
| List all containers | docker ps -a |
| Pull an image from Docker Hub | docker pull <IMAGE_NAME> |
| Create and run a container from an image | docker run <IMAGE_NAME> |
| Start a container | docker start <CONTAINER_NAME> |
| Stop a container | docker stop <CONTAINER_NAME> |
| Restart a container | docker restart <CONTAINER_NAME> |
| Remove a container | docker rm <CONTAINER_NAME> |
| Kill a container process | docker kill <CONTAINER_NAME> |
| Show container logs | docker logs <CONTAINER_NAME> |
| Attach to a container | docker attach <CONTAINER_NAME> |
| Copy files into a container | docker cp <CONTAINER_NAME>:<CONTAINER_PATH> <LOCAL_PATH> |
| Copy files from a container | docker cp <LOCAL_PATH> <CONTAINER_NAME>:<CONTAINER_PATH> |