Docker Project for DevOps Engineers
Today, we're diving into something really exciting: Docker. If you're a DevOps enthusiast or developer looking to streamline your workflow, Docker is an essential tool. In this post, we'll walk you through how to create a simple web application using Docker, from writing a Dockerfile
to pushing your image to a repository.
Let’s get started!
What is Docker?
Docker is a platform that makes it easier to create, deploy, and run applications by using containers. These containers package all the code, runtime, libraries, and settings your application needs to run. This ensures that your app behaves the same in any environment—whether it's on your local machine, in staging, or in production.
Imagine Docker containers as lightweight, self-sufficient ships that can sail smoothly no matter where they dock.
Step 1: Create a Simple Web Application
You can choose between Node.js or Python (Flask) for this task. Let’s go over both options, so you can pick the one that suits you best.
Option 1: Node.js App
Let’s create a basic Node.js web server using the Express
framework. Here’s what the code looks like:
// app.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World from Dockerized Node.js App!');
});
const port = 3000;
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
To manage dependencies, create a package.json
file:
{
"name": "docker-node-app",
"version": "1.0.0",
"main": "app.js",
"dependencies": {
"express": "^4.17.1"
},
"scripts": {
"start": "node app.js"
}
}
Option 2: Python Flask App
For those who prefer Python, here’s a simple web server using Flask:
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World from Dockerized Python App!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
To manage dependencies, create a requirements.txt
file:
Flask==2.0.1
Step 2: Write the Dockerfile
A Dockerfile contains instructions for Docker on how to package your application into a container.
Dockerfile for Node.js App:
# Use an official Node.js image as the base
FROM node:14
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Start the app
CMD ["npm", "start"]
Dockerfile for Python Flask App:
# Use an official Python runtime as the base image
FROM python:3.9-slim
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy requirements.txt and install dependencies
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code
COPY . .
# Expose the port the app runs on
EXPOSE 5000
# Start the app
CMD ["python", "app.py"]
Step 3: Build the Docker Image
Now, let’s build the Docker image using the Dockerfile you just created.
Navigate to the directory containing the Dockerfile and run this command:
For Node.js App:
docker build -t docker-node-app .
For Python Flask App:
docker build -t docker-python-app .
This will create a Docker image with the name docker-node-app
(for Node.js) or docker-python-app
(for Flask).
Step 4: Run the Docker Container
Once the image is built, you can run it in a container. Use the following command:
For Node.js App:
docker run -p 3000:3000 docker-node-app
For Python Flask App:
docker run -p 5000:5000 docker-python-app
This command maps the container’s internal port (3000 or 5000) to the same port on your local machine.
Open your web browser and go to:
For Node.js:
http://localhost:3000
For Python Flask:
http://localhost:5000
You should see the "Hello, World" message!
Step 5: Push the Docker Image to a Repository
To share your Docker image with others or deploy it, you'll want to push it to a repository, like Docker Hub.
- Login to Docker Hub:
docker login
- Tag your image with your Docker Hub username:
docker tag docker-node-app yourdockerhubusername/docker-node-app
Conclusion: Docker is Essential for DevOps
Docker isn’t just a tool—it’s a platform that allows you to scale, automate, and streamline your development and deployment process. Whether you’re working with microservices, deploying to the cloud, or setting up local development environments, Docker is a game-changer for DevOps engineers.
Subscribe to my newsletter
Read articles from Ammar Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by