Day 17: DevOps Docker Project

Day 17: DevOps Docker Project

ยท

3 min read

Table of contents

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.

Project

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

    Here's the Dockerfile for a simple Python app.

     FROM python:3.8-slim-buster
    
     WORKDIR /app
    
     COPY . /app
    
     RUN pip install --trusted-host pypi.python.org -r requirements.txt
    
     CMD ["python", "app.py"]
    

    The Dockerfile:

    • We use the base image of Python version 3.8 and slim-buster for the small size. You can check it on dockerhub.

    • Set the working directory inside the container to ' /app '.

    • Copies the current directory's content into the container at ' /app '.

    • Install the required libraries for the Python app written in the 'requirements.txt' file.

    • Ensure that the ' app.py ' (contains the code for the Python app) should be executed when the container launches.

  2. Build the image using the Dockerfile and run the container.

    • Build the docker image using the docker build. Use your actual ' image_name:tag ' e.g.

        docker build -t my-python-app:latest .
      
    • Once the image is built, you can run a container from it using the docker run command:

        docker run -d -p 5000:5000 my-python-app:latest
      

      This command exposes the port for your Python app, so you can run it on your browser.

    • You can check the running container for your Python app using the docker ps command.

  3. Verify that the application is working as expected by accessing it in a web browser.

    • Ensure that your container is active and running. You can check it with the docker ps command and if it's not running. You can run it using the docker run command as mentioned above.

    • In the address bar of your browser, enter the following URL:

        http://localhost:5000
      

      Or you can use

        http://127.0.0.1:5000
      
    • This will ensure that your application is running.

  4. Push the image to a public or private repository (e.g. Docker Hub)

    • To push the image to the docker hub, first, you need to tag the image with your docker hub username using the docker tag command:

        docker tag my-python-app:latest <your_dockerhub_name>/my-python-app:latest
      
    • After tagging the image, you need to login the docker hub in your terminal or command prompt:

        docker login
      

      This will ask for your username and password for the docker hub to login.

    • Then, you can push the image to your docker hub using the docker push command:

        docker push <your_dockerhub_name>/my-python-app:latest
      

      Wait for the push to complete.

    • After the push is completed, go to your docker hub and the image will be there successfully.

<That's all for today. FOLLOW to join me in the journey of DevOps>

ย