Run Nginx as a Docker Container

The days of working with FTP and simple operating system virtualizations are over… Modern web developers use Docker + Terminal to run & manage their services, and on this article we will explore a simple and fast way to run Nginx as a Docker container so you can start working with the fantastic Nginx dockerized.

Docker containers changed the life of all web developers, it’s a an easy and fast way to setup web apps and services in just minutes. It’s scalable, fast, and easy to manage to all those who are looking for a new way to deploy modern web applications.

nginx as a docker container

Deploy Nginx as a Docker container step by step

What are the requirements for me to setup Nginx + Docker? How can I deploy Nginx as a Docker container? Let’s see what’s the way to setup Nginx and Docker on Linux systems.

Setting up a docker container is not difficult at all, what can be a little bit challenging is to run the container so it can be reachable from your local network, but hey, follow this step by step guide and we will make this task easy for you.

Requirements

  • Docker installed and working normally.
  • Ay Linux distribution like CentOS / RHEL 6.x / 7.x  or Ubuntu / Debian

Let’s start deploying Nginx as a Docker container, step by sstep.

Get the official Nginx Docker image

Login into your server or local computer as root in your favourite terminal client, after that we will pull the Nginx docker image using the following command:

docker pull nginx

Add your system user to the Docker group by running:

sudo gpasswd -a ${USER} docker

Close all your terminal sessions, and try to login back again, start docker.

Alright, now that you pulled the nginx docker image and added the linux system user into docker group, let’s see how to make your port available to your network.

Configure the Nginx Docker container on your network

The Nginx image on Docker will be listening at a specific port, but it is not yet attached to the network port, in order to do so, you will have to run the following command. This will help you to expose your Nginx Docker port to all your network:

docker run --name ngx-docker -p 80:80 -d nginx

By executing “docker run” we are telling Docker that we are running an image as container. “docker-nginx” is just the name of the container, and the magic is done by specifying -p 80 (network port) and :80 (container port), finally “-d nginx” at the end of the command specifies what image should be used for this container, and that should be run in detached mode.

If you are already using your port 80 on another docker container or apache/nginx instance, you can setup the network port to another different than 80, let’s say 7070 or 8080.

Testing the Docker + Nginx container

Open up a browser, and type the IP address of the server that is hosting the Nginx docker container, should see the Nginx welcome image:

nginx welcome page on Docker

If you changed your port to be 8080, do the same, but instead of using just the IP address, add :8080 at the end.

Access your Nginx container

We will now run docker to get access to our Nginx docker container so you can tweak it as you need, let’s run the following command:

docker exec -it CONTAINER_ID bash

If you don’t know your container ID you can get it by running:

docker ps -aqf "name=ngx-docker"

Replace “ngx-docker” with your real docker container name.

Explanation of the command

  • exec is used to run a command inside a container.
  • -it is the parameter used to run an interactive pseudo-tty.
  • CONTAINER_ID is the ID of your container
  • bash is the type of shell you will run.

All done, now you are ready to work with your nginx container the same as if it was done locally.

Installing system tools inside the Docker container

You will probably need to install some system tools in order to work properly with your Nginx container, in order to do so, you will need to run the following command to avoid displaying errors to configure the TERM environment variable to xterm variable as you see below:

docker run --name ngx-docker-new -p 80:80 -e TERM=xterm -d nginx

After that you should be able to install any system tool like nano, vim, emacs, etc, as you do always:

For Debian and Ubuntu users:

apt-get install nano

or for CentOS Linux:

yum install nano

Conclusion

That’s all, at this time you should be your Nginx+Docker container running easily. As you see, deploying Nginx as a Docker container takes 5 minutes while you download the official Nginx docker image and run it from your terminal.

About the Author: Martin Keler

Leave a Reply

Your email address will not be published. Required fields are marked *