Docker NGINX – Dockerize NGINX with Web Application

Docker NGINX

Docker NGINX Tutorial – We shall learn to run NGINX in a Docker Container on Ubuntu. Following is a step by step guide to dockerize NGINX :

  1. Install Docker Engine

    Docker is the prerequisite.
    Follow Install Docker on Ubuntu , to install docker on your computer with Ubuntu.

  2. Pull NGINX from Docker Hub

    Run the following command to pull NGINX from Docker Hub to Docker Host.

    $ sudo docker pull nginx
     
  3. Run Docker with nginx

    Run the following command as root user

    $ docker run –name docker-nginx -p 80:80 -d nginx

    –name docker-nginx : Name given to the container that is run is docker-nginx
    -p 80:80 : the port we are exposing and mapping from local machine port number to that of container, in the format local_machine_port:container_port
    -d : Detached mode – Runs the container in background

    Now the NGINX is running in its default configuration.
    If you open a browser and hit the url 0.0.0.0:80, you would get following welcome page from NGINX Web Server.

    Docker NGINX

  4. Include a static Web Application in the Docker with NGINX

    To include our static Web Application into the Docker Image with NGINX, we shall create a Dockerfile (including commands to build image) and an html file with name index.html (acting as our web application) in a directory named nginx-app.

    Run the following command in Terminal from directory – nginx-app, to create image file.

    $ docker build -t nginx-application .
    Image file is created successfully.
    Run the following command in Terminal from directory, to run the image file in a container.

    $ docker run –name docker-nginx -p 80:80 -d nginx-application

    nginx-application – Docker Image file name.

    The container docker-nginx is running.

    Hit the url 0.0.0.0:80/index.html in any of your browser.

    Docker NGINX Application

Conclusion :

In this NGINX Tutorial – Docker NGINX, we have learnt to deploy NGINX with Docker