DevOps Shack
DevOps Shack
Build Once, Deploy Anywhere: A Guide to
Containerization with Docker
Introduction
Containerization has revolutionized the way applications are built, deployed, and
managed. Docker, as one of the most popular container platforms, allows
developers to bundle applications and their dependencies into a single image.
These images can be deployed anywhere – on a developer’s machine, in the
cloud, or in production – ensuring consistency across environments. This guide
will walk you through the steps of containerizing an application using Docker.
1
DevOps Shack
Why Containerization?
• Consistency: Once built, the application runs the same way everywhere.
• Portability: You can move containers between environments (dev, staging,
production) without worrying about compatibility issues.
• Scalability: Containers can be easily scaled horizontally.
Steps for Containerization with Docker
Step 1: Install Docker
First, install Docker on your system. You can install it on your local machine or a
cloud instance.
• For Windows or macOS, download Docker Desktop from Docker Hub.
• For Linux, run the following commands to install Docker:
sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
Once Docker is installed, verify the installation:
docker --version
Step 2: Create a Simple Application
For demonstration purposes, we’ll use a simple Node.js application, but you can
containerize any application (Java, Python, etc.).
Sample Node.js Application (app.js):
// app.js
const express = require('express');
const app = express();
const port = 3000;
2
DevOps Shack
app.get('/', (req, res) => {
res.send('Hello, Docker!');
});
app.listen(port, () => {
console.log(`App running on http://localhost:${port}`);
});
Create package.json:
{
"name": "docker-demo-app",
"version": "1.0.0",
"description": "A simple Node.js app for Docker demo",
"main": "app.js",
"dependencies": {
"express": "^4.17.1"
},
"scripts": {
"start": "node app.js"
},
"author": "",
"license": "ISC"
}
3
DevOps Shack
Step 3: Write a Dockerfile
The Dockerfile is a script that defines how the Docker image is built. It specifies
the base image, dependencies, and the application code that needs to be
included.
Dockerfile:
# Step 1: Use an official Node.js runtime as a parent image
FROM node:14
# Step 2: Set the working directory inside the container
WORKDIR /usr/src/app
# Step 3: Copy the package.json and package-lock.json files
COPY package*.json ./
# Step 4: Install the app dependencies
RUN npm install
# Step 5: Copy the app source code to the working directory
COPY . .
# Step 6: Expose the app port to the outside world
EXPOSE 3000
# Step 7: Define the command to run the app
CMD ["npm", "start"]
4
DevOps Shack
Step 4: Build the Docker Image
Now that we have the Dockerfile ready, let's build the Docker image.
In your terminal, navigate to the folder containing your Dockerfile and run the
following command to build the image:
docker build -t docker-demo-app .
This command will:
• Read the Dockerfile.
• Download the base image (Node.js) from Docker Hub.
• Copy the application files and dependencies into the image.
• Create the image tagged as docker-demo-app.
Verify that the image is created by running:
docker images
You should see docker-demo-app in the list of images.
Step 5: Run the Docker Container
Now that the Docker image is built, you can run it as a container.
Run the container with the following command:
docker run -d -p 3000:3000 docker-demo-app
Explanation:
• -d: Runs the container in detached mode (in the background).
• -p 3000:3000: Maps port 3000 of the container to port 3000 on your local
machine.
The application is now running inside a Docker container, and you can access it by
visiting http://localhost:3000 in your web browser. You should see "Hello,
Docker!" displayed.
5
DevOps Shack
Step 6: Push the Image to a Docker Registry (Optional)
If you want to deploy your application to other environments (e.g., staging or
production), you can push your image to a Docker registry like Docker Hub.
First, log in to Docker Hub:
docker login
Then tag the image and push it to your repository:
docker tag docker-demo-app your-dockerhub-username/docker-demo-app
docker push your-dockerhub-username/docker-demo-app
Now your image is available on Docker Hub and can be pulled on any system.
Step 7: Deploy the Docker Image Anywhere
The Docker image can now be deployed in different environments like cloud
servers or orchestration platforms (Kubernetes, Docker Swarm).
Example: Running the Image on Another Machine
On a different machine (e.g., a production server), you can pull and run the image
directly from Docker Hub:
docker pull your-dockerhub-username/docker-demo-app
docker run -d -p 3000:3000 your-dockerhub-username/docker-demo-app
This demonstrates the concept of "Build Once, Deploy Anywhere" since the same
image can be used across multiple environments without needing changes to the
application.
Step 8: Cleanup
To clean up and stop the container, use the following command:
docker stop <container-id>
docker rm <container-id>
6
DevOps Shack
You can also remove the image if no longer needed:
docker rmi docker-demo-app
Conclusion
Docker simplifies the development and deployment of applications by providing a
consistent runtime environment. In this guide, we have containerized a simple
Node.js application and demonstrated how the Docker image can be deployed
anywhere, showcasing the flexibility and portability of containerization.
By following these steps, you can apply similar methods to containerize and
deploy your applications in any environment, ensuring consistency and reliability
across development, staging, and production.