Docker Project for DevOps Engineers

Docker Project for DevOps Engineers

·

4 min read

📜Docker

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.

📜Docker FIle

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.

Let's learn by doing a project :

  • In today's blog we'll create python project , write a docker file for the same and build a docker image and run it .

  • Create a directory for your project say docker-project using mkdir docker-project .

  • Move to the directory using cd : cd docker-project and create a python file say flask-app using vim flask-app , let's write a basic code for the flask app and save it.

      # Import the Flask class from the flask module
      from flask import Flask
    
      # Create an instance of the Flask class and give it the name of the current module (__name__)
      app = Flask(__name__)
    
      # Define a route for the root URL ("/"). When someone accesses this URL, the following function will be executed.
      @app.route('/')
      def hello():
          return "Hello, Dosto!"  # This text will be displayed in the browser when the root URL is accessed.
    
      # The following block of code will only run if this script is executed directly (not imported as a module).
      # It starts the Flask development server on IP address 0.0.0.0 (accessible from any IP) and port 5000.
      # The debug=True option enables debugging features of Flask during development.
      if __name__ == '__main__':
          app.run(host='0.0.0.0', port=5000, debug=True)
    
  • Please go through the comments included above to understand the code.

  • Let's write a dockerfile for the above flask-app, the installation and configurations is explained well here docker basics .

A Dockerfile consists of a series of instructions that are used to build a Docker image. Each instruction is a command followed by its arguments. Here's a basic outline of the Dockerfile format:

  • Choose a Base Image: The first line of the Dockerfile specifies the base image you want to use as a starting point. This is typically an existing image from the Docker Hub registry that provides a basic operating system and runtime environment for your application.

      FROM python:3.9
    
  • Define Working Directory: Set the working directory for subsequent instructions

      WORKDIR /app
    
  • Copy Files: You can copy files from your local machine into the image.

      COPY app.py .
    
  • Run Commands: You can execute commands to install software, set up dependencies, and configure the environment.

      RUN pip install flask
    
  • Expose Ports: Specify which ports the container should listen on.

      EXPOSE 80
    
  • Define Startup Command: Specify the command to run when the container starts.

      CMD ["python", "app.py"]
    
  • Final file looks like this :

      # Use the official Python base image
      FROM python:3.9
    
      # Set the working directory
      WORKDIR /app
    
      # Copy the application files into the image
      COPY app.py .
    
      # Install dependencies
      RUN pip install flask
    
      # Set the startup command
      CMD ["python", "app.py"]
    
  • Once you've written the Dockerfile, you can navigate to the directory containing it in your terminal and build the Docker image using the docker build command:

      docker build . -t flask-app 
      # -t is used for tag and ' . ' build image out of the file present in current directory
    

  • To check if the images is built type docker images

  • Now let's run / create a container by using the image :

      docker run -d -p 5000:5000 flask-app:latest
      # -d : to run in detachable more or in the background
      # -p flag maps port 5000 from your host machine to port 5000 inside the container
    
  • To check the containers running type: docker ps

  • This project is running on port 5000

This was a flask project done on AWS EC2 using docker as a containerization tool.

Happy Learning :D

https://jfrog.com/devops-tools/article/understanding-and-building-docker-images/