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:
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:
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>? .
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>
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.
https://ec2-instance-public-ip:8000
7. Push the image to a public or private repository (e.g. Docker Hub)
I appreciate you reading. This article is meant to be helpful, I hope. ?????