Skip to main content

Command Palette

Search for a command to run...

Docker Project for DevOps Engineers

Day 17

Published
7 min readView as Markdown
Docker Project for DevOps Engineers

Dockerfile:

A Dockerfile is a plain text configuration file used to define the steps and instructions for building a Docker container image. Docker is a popular platform that allows you to package and distribute applications along with their dependencies into lightweight, portable containers. These containers are isolated environments that can run consistently across different systems, making it easier to deploy and manage applications.

The Dockerfile contains a set of instructions that Docker uses to create a Docker image. When you build an image using a Dockerfile, Docker executes each instruction in order, resulting in a layered and reproducible image. Each instruction represents a specific action, such as copying files, installing dependencies, or setting environment variables.

A typical Dockerfile may include instructions like:

  1. FROM: Specifies the base image to use for the new image. It is the starting point for the Dockerfile.

  2. MAINTAINER (deprecated): Specifies the name and email of the image maintainer. It is recommended to use LABEL instead.

  3. LABEL: Adds metadata to the image. You can use it for adding information like version, description, author, and other details.

  4. RUN: Executes commands inside the container during the image build process. It is used for installing packages, running build scripts, etc.

  5. CMD: Specifies the default command to be executed when the container starts. It can be overridden by providing a command when running the container.

  6. ENTRYPOINT: Similar to CMD, but provides a fixed entry point for the container. Arguments passed to the container are appended to the entry point command.

  7. WORKDIR or WORKDIR: Sets the working directory for the container, where subsequent commands will be executed.

  8. COPY and ADD: Both are used to copy files and directories from the host machine into the container. ADD has some additional features, such as URL support and automatic unpacking of compressed files.

  9. ENV: Sets environment variables inside the container.

  10. EXPOSE: Declares which ports the container will listen on at runtime. It does not actually publish the ports but serves as documentation.

  11. VOLUME: Creates a mount point with the specified name for external volumes or other containers to mount.

  12. USER: Specifies the user to run the commands in the container.

  13. ARG: Defines variables that can be passed to the docker build command using the --build-arg option.

  14. ONBUILD: Adds a trigger instruction to be executed when the image is used as the base for another image.

  15. HEALTHCHECK: Defines a command to check the container's health status.

  16. SHELL: Specifies the default shell to use for RUN, CMD, and ENTRYPOINT.

    Always refer to the official Docker documentation or release notes for the most up-to-date information on Dockerfile instructions.Once you have created the Dockerfile, you can use the docker build command to build the image. The resulting image can be distributed and used to create containers across different environments, ensuring consistent and reliable deployment of your applications.\

Let's Create a Dockerfile for a Simple Web Application

We will do two projects, one for Python Flask-app and the other is node-app. We will be using an AWS EC2 Instance for this project. Let's get started

Python flask-app:

Prerequisites

  1. Launch an EC2 Instance of instance type t2.micro as it includes the free-tier.

  2. I am using Ubuntu Image for this Project. Select/Generate a key pair, On the security group use the default SSH rule only for now.

  3. Rest leave the default option and hit Launch instance, it will take a couple of seconds to get ready.

  4. Copy the Public IP of the instance and open a CMD.

  5. Now on the Cmd, cd to Downloads or where you have placed the Key. And do ssh -i "Key_Name" ubuntu@public_ip , As shown in the Image below.

  6. If you get an Error here stating Unprotected Key then make sure you change the Key file permissions using chmod 400 key_name. This command will not work in CMD or Powershell, you need to install Git Bash if you are using a Windows Machine.

Now as we have successfully connected to the Instance lets start the actual Project now.

  1. Create a Directory using mkdir docker_projects and cd to it.

  2. Create a Directory with the name of flask-app inside it.

  3. If you see such structure by typing tree command then you are good, cd to the flask-app now.

  4. Let's create a Python flask code file using vim app.py. In real-world scenarios, Developer will give this code file to you.

     from flask import Flask
    
     # Create a Flask app
     app = Flask(__name__)
    
     # Define a route for the root URL
     @app.route('/')
     def hello_world():
         return 'Hello, Dosto'
    
     # Run the app on host 0.0.0.0 (all available network interfaces) and port 5000
     if __name__ == '__main__':
         app.run(host='0.0.0.0', port=5000, debug=True)
    
  5. Now create a Dockerfile inside it using vim Dockerfile, I have already created it. Let me explain it to you.

     # base image, it will pull the python image from the DockerHub
     FROM python:3.9
    
     # setting Working directory for app
     WORKDIR app/
    
     #copy code from system to container
     COPY app.py .
    
     # install required libraries
     # RUN is used while Container creation 
     RUN pip install flask
    
     #run the application
     # CMD is used to run a command POST container creation
     CMD ["python","app.py"]
    
  6. I am assuming that Docker is already installed in your Instance, if not do checkout my previous blog Docker for Beginners. Let's create a Docker Image from this code.

    Docker Best Practices in 2022 | Harness | Harness


$ docker build -t flask-app . # docker build it used to create a docker image, 
                              #-t is used to tag the image or to name it
                              # a dot . in the last indicates the PATH of the files in host machine
  1. Now after successfully creating an Image check it with docker images command

  1. Now let's run this image using the docker run

     $ docker run -d -p 5000:5000 flask-app
       # -d means run in detached mode/ daemon mode , in the background
       # -p means publish, we want to open the port and bind the host port with the container port to make it accessible
    

  1. Now it will be accessible from the the public ip of instance. public_ip:5000.

    But actually it will not work because no port is opened in the Security Group of the instance. Go to instance security group and add an inbound rule for port 5000.

  2. Let's check now, and see if our app is running or not.

    Hurray it is working.

Node js app

I am assuming you have done all the prerequisites and you are currently in your Instance. Let's Begin, cd .. to the docker_projects directory and create a new directory with the name of node-app and cd into it.

  1. Now I am cloning the code from GitHub Repository into this folder. Using git clone repo_url , you will see node-todo-cicd folder in it, just cd into it.

You will see all the required files in it, including the Dockerfile.

  1. Let's open and check the Dockerfile

    1. Let's build it to make an image using docker build -t node-app:v1 . We have already learnt what does this command does in the previous project

    2. Now check the image using the docker images

    3. let's run it now using docker run -d -p 8000:8000 node-app:v1

    4. Now check the public port of the instance to access it, make sure to add the inbound rule for port 8000 in the Instance Security group

      Now we have created both projects successfully. Do checkout the GitHub for the code Node and java project Node-app

    5. To push the images to the docker hub first you need to login to the docker hub from cmd using the docker login

    6. Then tag the images using the docker tag image_name dockerhub_username/imagename

    7. Now do docker push dockerhub_username/imagename and Similarly, push the other flask-app image. Check your docker hub dashboard.

      Here we have completed Both Docker Projects. Don't stop here, keep going.

90daysofdevops

Part 12 of 22

90 Days of DevOps Discourse: Unveiling the Path to Efficiency and Excellence!

Up next

Mastering Docker Compose: Building Beautiful Two-Tier Projects and Beyond

Day18