How To Run a Web App In a Dockerized Nginx Web Server (Ubuntu 18.04)

Chamsedine Mansouri
2 min readFeb 9, 2019

--

Nginx Logo

This tutorial aims clarifying how to run a dockerized Nginx web server in order to serve a sample web application.

STEP 1: Install Docker

$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

Verify Docker CE correct installation by running the following command

$ sudo docker run hello-world

it will display “Hello from docker!” if all is fine :)

STEP 2 : Pull NGINX image from DockerHub

$ sudo docker pull nginx 

Verify the correct pulling by running the following command

$ sudo docker image ls

This will list the available images cached by docker.

STEP 3 : Create a sample html document that will serve as our static index

$ mkdir -p ~/docker-nginx/html
$ cd ~/docker-nginx/html
$ vim sample.html

Just write whatever you want in your home page

<html>
<head>
<title>Docker nginx Tutorial</title>
</head>
<body>
<div class="container">
<h1>Hello Arctic</h1>
<p>This nginx page was made with love by Chams91</p>
</div>
</body>
</html>

STEP 4 : Link the container to the host html directory

As Nginx looks by default into “/usr/share/nginx/html”, we have to link the container with the directory containing our website index. This is done by using the following command

$ sudo docker run — name docker-nginx -p 8000:80 -d -v ~/docker-nginx/html:/usr/share/nginx/html nginx

So we’ve reflected the host 8000 port into nginx container default port 80 using “-p” option. “-d” option enabled running the container in detached mode (runs in the background). Finally “-v” option ensures the linking ;)

Now just verify that you can access your website from any other machine by

$ curl Your-VM-IP-@:8000

or just open a browser and visit “http:your-VM-IP-address:8000”, and.. That’s it.

In my next article, i will try to explain using Nginx as a reverse proxy for a React web application (Node.js Server)

--

--

No responses yet