Day 17 Task: Docker Project for DevOps Engineers.

Day 17 Task: Docker Project for DevOps Engineers.

Crafting Container Environments: A Guide to Dockerfile Creation

ยท

3 min read

๐Ÿ”ถ 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.

The Dockerfile provides a set of instructions that Docker follows to build an image for your application. Here are the commands commonly used in a Dockerfile:

  1. FROM: Specifies the base image to build upon.

    Example: FROM python:3.8

  2. WORKDIR: Sets the working directory inside the container where the application will be placed.

    Example: WORKDIR /usr/src/app

  3. COPY: Copies files or directories from your local machine to the container.

    Example: COPY requirements.txt ./

  4. RUN: Executes a command during the image build process. Commonly used for package installations.

    Example: RUN pip install --no-cache-dir -r requirements.txt

  5. EXPOSE: Exposes a port that the container will listen on.

    Example: EXPOSE 5000

  6. CMD: Specifies the command to run when the container starts.

    Example: CMD [ "python", "app.py" ]

  7. ENTRYPOINT: Similar to CMD, but it's not overridden by command-line arguments.

  8. ENV: Sets environment variables inside the container.

  9. VOLUME: Creates a mount point for a volume.

  10. USER: Sets the user that the container process runs as.

  11. WORKDIR: Sets the working directory within the container.

๐Ÿ”ถ Task-1: Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)

Dockerfile:

# Use an official Python runtime as the base image
FROM python:3

RUN pip install Django==4.2.2

# Copy the rest of the application code
COPY . .

# Install the required packages
RUN python manage.py migrate

# Define the command to run the application
CMD ["python", "manage.py", "runserver", "0.0.0.0:8001"]

๐Ÿ”ถ Task-2: Build the image using the Dockerfile and run the container.

#To build the image
docker build . -t todo-app

#To run the container
docker run -d -p 8001:8001 todo-app

๐Ÿ”ถ Task-3: Verify that the application is working as expected by accessing it in a web browser

๐Ÿ”ถ Task-4: Push the image to a public or private repository (e.g. Docker Hub )

#docker tag old-tag new-tag
docker tag todo-app chandreshpatle28/todo-app

#To login docker hub
docker login
#to push docker image
sudo docker push imagename

Docker image pushed

In conclusion, Docker and Docker Hub together provide a powerful ecosystem for containerization and application deployment. The Dockerfile serves as the blueprint for creating Docker images, while Docker Hub acts as a repository for storing, sharing, and distributing those images.

Stay in the loop with my latest insights and articles on cloud โ˜๏ธ and DevOps โ™พ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

GitHub Repo for the above task https://github.com/Chandreshpatle28/django-todo

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker

Did you find this article valuable?

Support Chandresh Patle's Blog by becoming a sponsor. Any amount is appreciated!

ย