Day 17 DevOps Task: Hands-on Docker Project for Engineers
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:
Basic knowledge of web applications ๐ (Donโt worry, you wonโt need to be a coding ninja ๐ฅท)
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!
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 namemy-web-app
and version1.0
..
: Tells Docker to look for theDockerfile
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:
๐ 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! ๐ณโจ