# Day 17 Task: Docker Project for DevOps Engineers.

## Deploying Python-Django web app to AWS EC2 with Docker

## Dockerfile:

Docker is a tool that makes it easy to run applications in containers. Containers are like small packages that hold everything an application needs to run. To create these containers, developers use something called a Dockerfile.

A Dockerfile is like a set of instructions for making a container. It tells Docker what base image to use, what commands to run, and what files to include. For example, if you were making a container for a website, the Dockerfile might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts.

### 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. Launch AWS ubuntu ec2 instance.**

**2\. Clone the repository from GitHub to your Ubuntu server using the command:**

```plaintext
  git clone <repository_URL>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689936436465/8dc30375-cfb9-4075-8642-5dd02cab93ff.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689937470662/2fa7e59e-c670-47ff-b682-e477ea81928c.png align="center")

### **3\. Create a Dockerfile:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689937535293/dc4c8753-f76c-436d-b4a8-efc0d611d757.png align="center")

The first thing we need to do is define from which image we want to build. Here we will use the python:3.9 image available from the Docker Hub.

```plaintext
FROM python:3.9
```

Next, we create a directory to hold the application code inside the image, this will be the working directory for your application:

```plaintext
WORKDIR /app
```

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

```plaintext
COPY . /app
```

This command installs all the dependencies defined in the requirements.txt file into your application within the container:

```plaintext
RUN pip install -r requirements.txt
```

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

```plaintext
EXPOSE 8000
```

This command starts the server and runs the application:

```plaintext
CMD ["python","manage.py","runserver","0.0.0.0:8000"]
```

### **4\. Build an Image using Dockerfile:**

To build an image using Dockerfile, Go to the directory that has your Dockerfile and run the following command

```plaintext
docker build -t <image-name>  .
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689937641854/9eb680a6-d655-4232-bbb9-f6d7390b5a7c.png align="center")

By using the `docker images` command we can see the list of images.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689937678951/8f9d7cc6-ec48-471a-a7b8-4f5ca0ecb084.png align="center")

### **5\. Run the image to create a container**

Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container and the - - name flag for assigning a name to the docker container.

Run the image you previously built:

```plaintext
docker run -d - -name <container-name> -p 8000:8000 <image-name>
```

The `docker ps` command is used to list all the containers.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689937761631/c2a24903-a640-45ef-b6cd-651bab08226e.png align="center")

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

```plaintext
http://ec2-instance-public-ip:8001
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689938228889/acde58e9-94a0-42d0-b42f-767ea09b3378.png align="center")

### **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 &lt;image-name&gt;
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689940003688/c990d8fe-a55e-4a2d-9093-e12a9cb28edc.png align="center")
    

### **Thank you for reading :)**
