Day 17 Task: Docker  Project for DevOps Engineers.

Day 17 Task: Docker Project for DevOps Engineers.

·

4 min read

Dockerfile:

A Dockerfile is a text file that contains a set of instructions for building a Docker image. These instructions specify the environment and dependencies required for running an application or service.

Project Details:

Objective:

The objective of this project is to set up a Dockerized web hosting environment on AWS using Nginx.

A separate EC2 instance is created for running a Docker container that hosts the website using a Docker image containing Nginx

Task:

  1. Launched an EC2 instance on AWS and installed Ubuntu.

  2. Copied dummy website content to the Ubuntu instance.

  3. Installed Nginx on the Ubuntu instance and configured it to host the website.

  4. Created a Dockerfile with instructions to install Nginx and other dependencies.

  5. Built a Docker image using the Dockerfile.

  6. Run a Docker container using the Docker image created to host the website.

  7. Again create an image of that container and Push it on Docker-hub

Step-1 Launch an EC2 instance and install docker into it.

How to Serve an Angular App with nginx in Docker | TypeOfNaN

Update the Local System

apt-get update

Install Docker into local system

sudo apt-get install docker.io -y

Step-2 Create a web hosting block and also copy the content of the Website into it.

Create a file name aman and add content into it :

server {
    listen 80;
    server_name 65.1.135.192;
    root /etc/webcontent;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

here, server name has my ip address of the instance and my website content that is written in the root directory /etc/web content.

here we can see, here is my website content which I will try to host it:

Step-3 Create a Dockerfile to host this website using nginx :

At first, create a file name Dockerfile:

vi Dockerfile

#Pull base image
FROM ubuntu

# Install nginx and create website configuration
RUN apt-get update && \
    apt-get install -y nginx && \
        apt-get install -y vim && \
    rm -rf /etc/nginx/sites-available/default && \
    rm -rf /etc/nginx/sites-enabled/default && \
    echo "daemon off;" >> /etc/nginx/nginx.conf

# Copy website files and create website configuration
COPY webcontent /etc/webcontent/
COPY aman /etc/nginx/sites-available/
RUN ln -s /etc/nginx/sites-available/aman /etc/nginx/sites-enabled/

# Expose port 80
EXPOSE 80

# Start nginx when container starts
CMD ["nginx"]

here is what I have done :

FROM: create a base image of Ubuntu

RUN: install nginx onto the container, delete default inside sites-available and sites-enabled

COPY: copy my web content onto a folder name 'web content in the/etc folder

COPY: copy my hosting block to sites available inside nginx

RUN: here I create a symbolic link of site-available to sites-enable

EXPOSE: Expose to port 80 where nginx needs to run.

CMD: it executes nginx when I create a container.

Step-4 Run the docker file create an image and then create a container from the image:

CloudSigma Tutorial: Installing Nginx on Ubuntu 18.04

a) Create an image after running this docker file:

docker build -t nginx .

verify images with :

docker images

Step-5 Create a container and mapped the port 80:80 from this docker image.

COPY

docker run -it -p 80:80 --name Abhijeet_cont nginx /bin/bash

Step-6 Enter into the container and verify all things which you have added to the docker file:

verify whether file1 inside sites-available, web content in /etc

Step-7 Restart the Nginx service put the instance IP address into the browser check whether this website getting hosted or not:

Successful:

Step-8 Now again create an image of this container and then push it anywhere you want:

docker commit <container_name> <image name>

here I have created an image of my container.

Step-9 Push this image on the Docker hub:

docker login

Give a tag to this image:

docker tag <image_name> <username/<image_name>:latest

Push the Docker image to Docker Hub using the docker push command.

docker push <docker_id>/<image_name>:<tag_name>

Now verify it with your docker hub account:

+++++++++++++++++++++++++THANK YOU++++++++++++++++++++++++++