Day 17: Docker Mastery: Elevate Your DevOps Game with This Project!

Day 17: Docker Mastery: Elevate Your DevOps Game with This Project!

Unlock the Power of Containers and Transform Your DevOps Journey!

ยท

3 min read

๐Ÿ”ท What is 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.

Tasks

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

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

#Create a working directory
WORKDIR /data


RUN pip install Django==4.2.2

# Copy the rest of the application code
COPY . .

# Install the required packages
RUN python manage.py migrate

#expose PORT 8000
expose 8000

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

  1. ๐Ÿ”ท Task 2: Build the Image using Dockerfile and run the container

# To build the app

docker build -t <app_name>:tag .

docker build django-app:latest .

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

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

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

#docker tag old-tag new-tag
docker tag django-app dipen06/django-app

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

Docker image Pushed to Dockerhub

Conclusion

In conclusion, Docker and DockerHub provide powerful tools for developers and organizations to streamline software development, deployment, and management processes. Docker simplifies the packaging of applications into lightweight, portable containers, while DockerHub serves as a centralized repository for sharing, storing, and managing container images. Together, they offer an efficient and scalable solution for building, shipping, and running applications across diverse environments.

Join me on my caffeinated adventure! Click 'Buy me a coffee' and let's fuel creativity together!

~Dipen : )

Did you find this article valuable?

Support Dipen Rikkaame by becoming a sponsor. Any amount is appreciated!

ย