Skip to main content

Nginx via Docker

Desired Outcome

Be the end of this guide, you 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

  • By running Nginx inside a Docker container, it makes the install and start up process easier than dealing with various package managers and os-specific commands needed to setup Nginx.
  • Nginx upgrades can be completed by simply swapping out the container for an updated version.
  • Once you've set up the basics, launching new services inside of docker containers and using Nginx to reverse-proxy requests to those container will be fast and consistent, regardless of the application type running inside Docker.
info

Note: This guide will detail the setup on an Ubuntu machine, however I will provide as many direct documentation links as possible. Basic linux knowledge is assumed.

1. Remove previous Docker Installations (optional)

  • Docker Installation Docs
  • optionally, uninstall previous versions
  • sudo apt-get remove docker docker-engine [docker.io](http://docker.io) container runs

2. New Host Setup (optional)

Before installing Docker Engine for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.

  • Update apt index 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](https://download.docker.com/linux/ubuntu/gpg) | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  • Set Up Stable Repository (x86_64 / amd64)
    • Consult docs for other system architectures.
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] [https://download.docker.com/linux/ubuntu](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 installation by running Docker's Hello-World container
sudo docker run hello-world

4. Download and Start Nginx Container

  • Get the latest Docker Nginx image
sudo docker pull nginx
  • Start the docker container
    • Use the --name flag to give this container a name
    • Use the -p flag to map external-port:container-port
    • Use the -d flag to run the container in detached mode
sudo docker run --name my-nginx-container -p 80:80 -d nginx
info

Note: We are mapping port 80 of the machine to port 80 of the docker container running Nginx. This means that all incoming web traffic will be forwarded to this container, where Nginx can evaluate it and further forward it on to its intended service.

  • Check that your container is running
sudo docker ps -a

5. Create Nginx Server Blocks for other Services

  • Create a file to hold our nginx configuration
    • touch my-nginx-conf.confd
  • Add a server block to the file you just create
    • nano my-nginx-conf.confd
  • Move this file to a directory of your choice. We will map this directory on the local machine to the Nginx conf directory inside the Nginx container.
    • mv my-nginx-conf.confd path/to/local/directory

Serve Static Content

server {
listen 80;
server_name domain.com www.domain.com *.domain.com;
root /path/to/static/files/;

location /images {
root /path/to/images
}

}

Reverse Proxy Requests

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 Local Nginx directory to Nginx Docker Container

We need to add a -v volume flag to our command for starting the nginx container. This should map a local directory to a directory inside the container; local_directory:container_directory.

sudo docker run --name my-nginx-container -p 80:80 -d  -v  local/conf/directory:/etc/nginx/conf.d nginx

Now, when our image starts up, Nginx will look for configurations in the /etc/nginx/conf.d directory inside the container, which will read it's contents from the local directory /local/conf/directory.

7. Restart Nginx Container

Nginx does not know about our new configuration files until we restart it.

sudo docker restart <CONTAINER_NAME>

Congratulations!

You have:

  • Download and Install Docker
  • Set up an Nginx Docker Container
  • Configure Nginx to reverse proxy requests to other Docker containers

Docker Commands

DescriptionCommand
list imagesdocker images
list running containersdocker ps
list docker all containersdocker ps -a
pull image from Dockerhubdocker pull <IMAGE_NAME>
create container from an image and run itdocker run <IMAGE_NAME>
start containerdocker start <CONTAINER_NAME>
stop containerdocker stop <CONTAINER_NAME>
restart containerdocker restart <CONTAINER_NAME>
remove containerdocker rm <CONTAINER_NAME>
kill container processdocker kill <CONTAINER_NAME>
show container logsdocker logs <CONTAINER_NAME>
attach to containerdocker attach <CONTAINER_NAME>
copy files into containerdocker cp <CONTAINER_NAME>:<CONTAINER_PATH> <LOCAL_PATH>
copy files FROM containerdocker cp <LOCAL_PATH> <CONTAINER_NAME>:<CONTAINER_PATH>

Resources

How To Run Nginx in a Docker Container on Ubuntu 14.04
Docker Official Installation
Nginx Docker Official Images
Docker Commands Cheat Sheet