Day 17: Docker Project for DevOps

Day 17: Docker Project for DevOps

Deploying Python-Django web app to AWS EC2 with Docker

Dockerfile:

A tool that makes it simple to execute apps in containers is called Docker. Containers act as little packages that include all the components required for a programme to function. Developers use something called a Docker file to build these containers.

A Dockerfile resembles a manual for creating containers. It instructs Docker regarding the base image to use, the commands to execute, and the files to add. For instance, the Dockerfile might instruct Docker to use an official web server image, copy the contents for your website inside the container, and start the web server when the container starts if you were creating a container for a website.


Task:

  • Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)
  • Build the image using the Dockerfile and run the container
  • Verify that the application is working as expected by accessing it in a web browser
  • Push the image to a public or private repository (e.g. Docker Hub )

Steps:

1. Start the Ubuntu EC2 instance on AWS.

2. Use the following command to clone the repository from GitHub to your Ubuntu server:

git clone <repository_URL>        

3. Create a Dockerfile:

No alt text provided for this image

The first step is to decide which image we want to base our construction on. Here, we'll make use of the python:3.9 image from Docker Hub.

FROM python:3.9        

As the working directory for your application, we now establish a directory to house the application code inside the image;

WORKDIR /app        

Copy your application's code inside the Docker image folder app using COPY instruction:

COPY . /app        

With this command, all the dependencies listed in the requirements.txt file are installed in your application's container, as follows:


RUN pip install -r requirements.txt        

This command releases port 8001 within the container, where the Django app will run:

EXPOSE 8001        

?This command starts the server and runs the application:

CMD ["python","manage.py","runserver","0.0.0.0:8001"]        

4. Build an Image using Dockerfile:

Dockerfile can be used to create an image. Run the command below when you are in the directory where your Dockerfile is located.


docker build -t <image-name>? .        
No alt text provided for this image

5. Run the image to create a container

When you execute your image with the -d flag, the container is run in detached mode and is left running in the background. The -p flag changes a public port inside the container to a private port, while the -name flag gives a name to the docker container.

Run the previously created image:


docker run -d - -name <container-name> -p 8001:8001 <image-name>        


No alt text provided for this image

6.Verify that the application is working as expected by accessing it in a web browser.

Go to your AWS security inbound group and enable port 8001

according to your project, you enable your port

this is written in requirements.txt.

No alt text provided for this image


https://ec2-instance-public-ip:8000        

7. Push the image to a public or private repository (e.g. Docker Hub)

  • Login to docker hub
  • Push docker image to docker hub using command:
  • docker push <image-name>

I appreciate you reading. This article is meant to be helpful, I hope. ?????

要查看或添加评论,请登录

Vinay Kumar的更多文章

  • DevOps Project - 4 ????

    DevOps Project - 4 ????

    Project Description The project aims to deploy a web application using Docker Swarm, a container orchestration tool…

    7 条评论
  • DevOps Project 3 ????

    DevOps Project 3 ????

    Project Description The project involves hosting a static website using an AWS S3 bucket. Amazon S3 is an object…

    3 条评论
  • DevOps Project -2 ????

    DevOps Project -2 ????

    Project Description The project is about automating the deployment process of a web application using Jenkins and its…

  • Day 80: DevOps Project 1 ????

    Day 80: DevOps Project 1 ????

    Project Description The project aims to automate the building, testing, and deployment process of a web application…

    2 条评论
  • Day 73 - Setup Grafana on AWS EC2 Instance ????

    Day 73 - Setup Grafana on AWS EC2 Instance ????

    Task: Set up grafana in your local environment on AWS EC2. Go to the AWS console and Launch an EC2 instance To enable…

  • Day 72 - Grafana ????

    Day 72 - Grafana ????

    What is Grafana? No matter where your metrics are kept, Grafana is an open-source data visualization and monitoring…

    4 条评论
  • Day71 - Terraform Interview Questions ????

    Day71 - Terraform Interview Questions ????

    1. What is Terraform and how it is different from other IaaC tools? HashiCorp's Terraform is an Infrastructure as Code…

  • Day 70 - Terraform Modules ????

    Day 70 - Terraform Modules ????

    Modules are containers for multiple resources that are used together. A module consists of a collection of .

    4 条评论
  • Day 69 - Meta-Arguments in Terraform ???

    Day 69 - Meta-Arguments in Terraform ???

    When you define a resource block in Terraform, by default, this specifies one resource that will be created. To manage…

    4 条评论
  • Day 65 - Terraform Resources ????

    Day 65 - Terraform Resources ????

    Understanding Terraform Resources A resource in Terraform represents a component of your infrastructure, such as a…

    2 条评论

社区洞察

其他会员也浏览了