Day 17 Task: Docker Project for DevOps Engineers.

Day 17 Task: Docker Project for DevOps Engineers.

Dockerfile:

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. This page describes the commands you can use in 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.

Project Details:-

Task:

  • Create a Dockerfile for a simple web application ( 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 command.

sudo apt-get update
git clone <repository_URL>

3. Create a Dockerfile:

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

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:

WORKDIR /app

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

COPY . /app

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

RUN pip install -r requirements.txt

This command releases port 8000 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: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

docker build -t <image-name>  .

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 — — name flag for assigning name to docker container.

Run the image you previously built:

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

docker ps command is used to list all the containers

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

http://ec2-instance-public-ip-address:8001

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>

Thank you for reading!