Day 17: Docker Project for DevOps Engineers

90DaysOfDevOps

·

3 min read

Day 17: Docker Project for DevOps Engineers

In this blog, let’s learn about what Docker File is and build the project through Docker.

Dockerfile

A Dockerfile is a script or you can say a set of instructions that we used to create a Docker image, which is a lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including the code, runtime, libraries, and system tools.

Dockerfile Commands

Certainly! Here are some common Dockerfile commands and their explanations:

  1. FROM: Specifies the base image for your Docker image. It's the starting point for building your image.

    Example: FROM ubuntu:20.04

  2. RUN: Executes a command in the container during the build process. It's used to install packages, set up software, and perform other actions.

    Example: RUN apt-get update && apt-get install -y curl

  3. ENV: Sets environment variables in the container.

    Example: ENV NODE_ENV=production

  4. WORKDIR: Sets the working directory inside the container for subsequent commands.

    Example: WORKDIR /app

  5. CMD: Specifies the default command to run when the container starts. Only the last CMD instruction in the Dockerfile is used.

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

  6. ENTRYPOINT: Specifies the executable that will be run when the container starts

    Example: ENTRYPOINT ["java", "-jar", "app.jar"]

  7. EXPOSE: Declares the ports that the container will listen on at runtime.

    Example: EXPOSE 8080

  8. VOLUME: Creates a mount point for a volume that can be used to share data between the host and the container

    Example: VOLUME /data

  9. COPY: Copies files or directories from the host machine to the container's image.

    Example: COPY app.py /app/

  10. ADD: is used to copy files or directories from your local machine (the host) into the Docker image and also copy TAR files and unzip them on the container directory

    Example: ADD myfile.txt /app/

    ADD myarchive.tar.gz /app/

Here's a basic example of a Dockerfile for a simple Python web application using Flask:

# Use an official Python runtime as the base image
FROM python:3.8-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

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 )

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

I have used the following repository for the task: github.com/Azure-Samples/nodejs-docs-hello-..

Make a directory and clone the repository in your system using command git clone github.com/Azure-Samples/nodejs-docs-hello-..

Build the image using the Dockerfile and run the container

Here is the docker file below:

Once the image is built, run the container using the following command:

docker run -p 3000:3000 first_docker_application

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 )

To push the image to a repository, you need an account on Docker Hub.

In your CLI, log in to the docker hub using the following command and enter your username and password.

Once you have logged in, tag the image with the repository’s name and push it using the following commands:

docker tag <image_name> <username>/<repository>:<tag>
docker push <username>/<repository>:<tag>

Now you can see that my docker image is successfully pushed into the docker hub

Thank you for reading this article!