Day 17 Task: Docker Project for DevOps Engineers

ยท

3 min read

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.

Here are some of the most commonly used commands in a Dockerfile:

  • FROM: Specifies the base image (Python, Nodejs image....etc from Docker Hub).

  • WORKDIR: Sets the working directory inside the container.

  • COPY: Copies the current directory's content (Python,nodejs....etc web app files) to the /app directory in the container.

  • RUN: Executes commands in the container (here, it installs the dependencies listed in requirements.txt).

  • EXPOSE: Exposes a port (your port no in this case) from the container to the host.

  • ENV: Sets environment variables (here, sets NAME to "your name").

  • CMD: Specifies the command to run the application when the container starts (here, it runs app.pyor app.js).

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)

  1. Create a Simple Python Flask Web Application:

  • Create a simple Python Flask application. Let's say it's a basic server that responds with "Hello DevOps engineer" when accessed.

  • app.py (Python Flask application):

  1. Create a Dockerfile:

  1. Build the Docker Image:

  • Navigate to the directory containing app.py and Dockerfile.

  • Run the following command to build the Docker image:

docker build -t python-web-app-1.0 .

  1. Run the Container:

  • Once the image is built, run a container based on this image:
docker run -d --name python-web-app-1.0 -p 5000:5000 python-web-app-1.0:latest

  1. Verify the Application:

    • Access the application by opening a web browser and navigating to http://IP adress:5000. You should see "Hello DevOps engineer" displayed.

  1. Push the Image to a Repository (e.g., Docker Hub):

Log In to Docker Hub ๐Ÿ”‘

  • If you haven't already, you need to log in to your Docker Hub account in your terminal:
docker login

Enter your Docker Hub username and password when prompted.

  • Before pushing the image to Docker Hub, you need to tag it with your Docker Hub username and repository name. Replace <username> with your Docker Hub username and <repository> with the desired repository name:
docker tag <your_image_name> <dockerhub username>/<repository_name>:tag

Push the Image to Docker Hub ๐Ÿšข

  • Push the tagged image to Docker Hub:
docker push <dockerhub username>/<repository name>:tag
ย