Day 17 Task: Docker Project for DevOps Engineers.

Day 17 Task: Docker Project for DevOps Engineers.

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.

Basic Commands

FROM – Defines the base image to use and starts the build process.

RUN – It takes the command and its arguments to run it from the image.

CMD – Similar function as a RUN command, but it gets executed only after the container is instantiated.

ENTRYPOINT – It targets your default application in the image when the container is created.

ADD – It copies the files from source to destination (inside the container).

ENV – Sets environment variables.

Task :

Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)

make a directory and Clone the repository in your system using the command git clone <remote url>

Create a Docker file.

Docker file fields:

FROM python:3.9

It tells docker, which base image you want to base your image from. In our example, we are creating an image from the Python image.

WORKDIR app

As the name suggests, WORKDIR sets the directory that the container starts in. Its main purpose is to set the working directory for all future Dockerfile commands.

COPY . /app

copy your application code inside the docker image.

RUN pip install -r requirement.txt

The command is used for all dependencies in the requirement file.

EXPOSE 8001

commands used for release port 8001 within the container.

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

The command starts the server and runs the application.

Build the image using the Dockerfile and run the container

docker images: We can see all docker images.

  1. Run the image to create a container:

sudo docker run -d --name <container_name> -p 8001:8001 <iamge-name>

  1. 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 )

      Create an account in Docker Hub.

    • login to docker and enter your username & password of your docker hub

      Check the docker images and Push that image to the docker hub using the docker push <image_name> command.

      Day 17 task completed.

      Thank you for reading!!!!!!!!!!