Day 17 DevOps Task: Hands-on Docker Project for Engineers

Syed DadapeerSyed Dadapeer
4 min read

Ever wonder how your favorite apps and websites run smoothly, no matter where you access them from? ๐ŸŒ Well, a lot of that magic happens behind the scenes with tools like Docker! Think of Docker as a little box ๐Ÿ“ฆ that holds your entire app and all the stuff it needs to runโ€”no more "it works on my machine" headaches! ๐Ÿ˜…

In this blog, we're diving into the Dockerfile ๐Ÿ“โ€”the blueprint that tells Docker how to build your appโ€™s container. And guess what? Weโ€™re not just talking about it; weโ€™re actually building something cool! ๐Ÿ’ปโœจ

Here's what weโ€™ll do:

๐Ÿ”น Create a magical file called a Dockerfile ๐Ÿ“โ€”itโ€™s like a recipe for your web app!
๐Ÿ”น Build your very own Docker image ๐Ÿ“ธ (a snapshot of your app).
๐Ÿ”น Run it in a container and see it come to life in your web browser ๐ŸŒ.
๐Ÿ”น Finally, weโ€™ll push it to a Docker Hub repository (a public or private app store for containers) for the worldโ€”or just youโ€”to use! ๐Ÿขโœจ

Sounds exciting? ๐Ÿ™Œ Grab your favorite beverage โ˜•, fire up your terminal ๐Ÿ’ป, and letโ€™s Dockerize our first app together!


๐Ÿ› ๏ธ Prerequisites

Before we start, make sure youโ€™ve got the following set up:

  1. Basic knowledge of web applications ๐ŸŒ (Donโ€™t worry, you wonโ€™t need to be a coding ninja ๐Ÿฅท)

  2. Docker installed on your machine ๐Ÿณ

    If you havenโ€™t installed Docker yet, head over to Docker Installation and get it up and running. Itโ€™s super easy!

  3. A Docker Hub Account
    If you want to share your app with the world, youโ€™ll need to push your Docker image to a repository like Docker Hub. Donโ€™t have an account? Sign up here.

Got all that? Awesome! Letโ€™s dive into the magic of Dockerfiles and containers.โœจ


TASKS

  • 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)

Step 1: Create a Dockerfile

Letโ€™s start by creating a simple Python web application using Flask (a lightweight web framework).

1.1. Project Structure ๐Ÿ—๏ธ

my-simple-app/
โ”œโ”€โ”€ app.py          # Python web app file
โ””โ”€โ”€ Dockerfile      # Our secret recipe file!

Create a folder named my-simple-app and navigate into it:

mkdir my-simple-app && cd my-simple-app

1.2. app.py (Our Web App Code) ๐Ÿง‘โ€๐Ÿ’ป

Create a file called app.py and paste this code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, World! ๐ŸŒ Welcome to my Dockerized Web App."

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

Create a requirements.txt file to list the dependencies:

Flask==3.1.0

This little Python app displays "Hello, World!" when you visit it in a web browser. ๐ŸŒ

1.3. Dockerfile (Our Recipe) ๐Ÿ“œ

Now, create a file named Dockerfile in the same folder and add this code:

# Step 1: Use an official Python image as the base
FROM python:3.9-slim

# Step 2: Set the working directory inside the container
WORKDIR /app

# Step 3: Copy the app code into the container
COPY . .

# Step 4: Install Flask (our web framework)
RUN pip install -r requirements.txt

# Step 5: Expose the port that Flask will use
EXPOSE 5000

# Step 6: Define the command to run the app
CMD ["python", "app.py"]

๐Ÿ”จ Step 2: Build the Docker Image

Think of this step as turning your recipe into a ready-to-serve dish. ๐Ÿฒ
Run the following command in the terminal inside the my-simple-app directory:

docker build -t my-web-app:1.0 .
  • docker build: Builds a Docker image.

  • -t my-web-app:1.0: Tags the image with the name my-web-app and version 1.0.

  • .: Tells Docker to look for the Dockerfile in the current directory.

โณ After a few seconds, your image will be ready! ๐ŸŽ‰


๐Ÿš€ Step 3: Run the Docker Container

Letโ€™s run our web app inside a Docker container.

docker run -d -p 5000:5000 my-web-app:1.0
  • -d: Runs the container in detached mode (in the background).

  • -p 5000:5000: Maps port 5000 on your computer to port 5000 in the container.


๐ŸŒ Step 4: Verify the App in a Browser

Open your favorite browser and go to:
๐Ÿ‘‰ http://localhost:5000

You should see:
"Hello, World! ๐ŸŒ Welcome to my Dockerized Web App." ๐ŸŽ‰


๐Ÿ“ค Step 5: Push the Image to Docker Hub

Finally, Letโ€™s share our image with the world! ๐ŸŒ

5.1. Log in to Docker Hub

docker login

Enter your Docker Hub username and password.

5.2. Tag the Image

docker tag my-web-app:1.0 <your-dockerhub-username>/my-web-app:1.0

5.3. Push the Image

docker push <your-dockerhub-username>/my-web-app:1.0

โœ… Your image is now available on Docker Hub! ๐ŸŽ‰ Anyone can pull and run it with:

docker pull <your-dockerhub-username>/my-web-app:1.0
docker run -p 5000:5000 <your-dockerhub-username>/my-web-app:1.0

๐ŸŽ‰ And Thatโ€™s a Wrap!

Youโ€™ve successfully:
โœ… Created a Dockerfile for a web app
โœ… Built a Docker image
โœ… Ran a container to host the app
โœ… Verified it in a browser
โœ… Pushed the image to Docker Hub

Now, you can confidently say: โ€œIt works on every machine!โ€ ๐Ÿ’ช

Happy Dockering! ๐Ÿณโœจ

0
Subscribe to my newsletter

Read articles from Syed Dadapeer directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Syed Dadapeer
Syed Dadapeer

Hi, I'm Syed Dadapeer! I'm an experienced ๐‚๐ข๐ญ๐ซ๐ข๐ฑ ๐€๐๐ฆ๐ข๐ง๐ข๐ฌ๐ญ๐ซ๐š๐ญ๐จ๐ซ and ๐’๐ฒ๐ฌ๐ญ๐ž๐ฆ ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ with over 2.5 years of IT experience, focusing on troubleshooting complex issues, cloud computing, virtualization, and Citrix products. I'm also ๐‘๐ž๐ ๐‡๐š๐ญ ๐œ๐ž๐ซ๐ญ๐ข๐Ÿ๐ข๐ž๐! ๐Ÿ’ก Iโ€™m passionate about learning new technologies and applying them to real-world challenges. Iโ€™m also exploring the exciting fields of ๐€๐ซ๐ญ๐ข๐Ÿ๐ข๐œ๐ข๐š๐ฅ ๐ˆ๐ง๐ญ๐ž๐ฅ๐ฅ๐ข๐ ๐ž๐ง๐œ๐ž, ๐‚๐ฅ๐จ๐ฎ๐ ๐‚๐จ๐ฆ๐ฉ๐ฎ๐ญ๐ข๐ง๐ , and ๐ƒ๐ž๐ฏ๐Ž๐ฉ๐ฌ.โ™พ๏ธ ๐Ÿš€ Letโ€™s connect! Iโ€™m always open to new opportunities and collaborations. Feel free to reach out via ๐‹๐ข๐ง๐ค๐ž๐๐ˆ๐ง or at ๐ฌ๐๐š๐๐ฎ2206@๐ ๐ฆ๐š๐ข๐ฅ.๐œ๐จ๐ฆ.