Project II: Deploying a Containerized Application with Docker: A Step-by-Step Guide
- WeeklyTechReview

- Dec 23, 2024
- 4 min read
Introduction to Docker and Its Benefits
Docker is a powerful platform that automates the deployment, scaling, and management of applications inside lightweight containers. It allows developers to package applications along with all their dependencies into a single, portable container image. This ensures that the application runs consistently across different environments, whether it’s on a developer’s local machine or in production on a cloud platform.
Why use Docker?
Portability: Docker containers encapsulate the application and its environment, making them highly portable across different systems.
Consistency: Containers eliminate the “it works on my machine” problem, as the environment remains consistent across various stages.
Efficiency: Docker containers share the host operating system’s kernel, allowing them to be lightweight and efficient.
Scalability: Docker makes it easy to scale applications, whether you’re running them locally, in a cloud environment, or orchestrating them with tools like Kubernetes.
In this blog, let's walk through creating a containerized version of a simple application using Docker. We will explore the entire process from writing a Docker file to running the container locally, and finally deploying it on a cloud platform like AWS or Azure.
Step 1: Writing the Docker file and Creating an Image
To containerize an application, we first need to define how it should be packaged. This is done by writing a Docker file, which is a script that contains instructions on how to build a Docker image. The Docker image is the blueprint for the container.
Example Application
Let’s consider a simple Node.js web application. Below is the code for the app and how to create a Docker file.
Create the Node.js Application
First, create a directory for your project and navigate into it:
mkdir my-app
cd my-appInside the my-app directory, create the app.js file with the following code:
// app.js
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, Docker World!');
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});Create the package.json File: Next, initialize a package.json file to manage your application dependencies.
npm init -y
npm install expressThis file will look something like this:
{
"name": "my-app",
"version": "1.0.0",
"main": "app.js",
"dependencies": {
"express": "^4.17.1"
}
}

3. Writing the Docker file The Docker file will specify how the Docker image for this application should be built. Create a new file named Docker file in the root of the project directory.
# Use the official Node.js image from Docker Hub as the base image
FROM node:14
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy the package.json and package-lock.json files into the container
COPY package*.json ./
# Install the app dependencies inside the container
RUN npm install
# Copy the rest of the application files into the container
COPY . .
# Expose the port the app will run on
EXPOSE 3000
# Command to run the application
CMD ["node", "app.js"]Step 2: Building the Docker Image
Once the Docker file is ready, the next step is to build the Docker image. This can be done using the docker build command.
Build the Docker Image: Run the following command from the root of your project directory:
docker build -t my-node-app .This command will build the Docker image with the tag my-node-app. Docker will read the Docker file, install dependencies, and set up the container as specified.
Verify the Image: Once the build process is complete, you can verify the image by listing all available images:
docker run -p 3000:3000 my-node-appThis command tells Docker to:
Run the my-node-app image.
Map port 3000 on the container to port 3000 on the host, so you can access the application via localhost:3000.
Access the Application: Open your browser and navigate to http://localhost:3000. You should see the message: Hello, Docker World!.

Step 4: Deploying the Container to the Cloud
Once you’ve successfully run the container locally, you can deploy it to a cloud platform. For this example, let’s use Docker Hub to host the image, and then deploy it on AWS EC2 or Azure.
Push the Image to Docker Hub:
First, log in to Docker Hub:
docker loginTag the image:
docker tag my-node-app <dockerhub-username>/my-node-appPush the image:
docker push <dockerhub-username>/my-node-appDeploy on AWS EC2:
Launch an EC2 instance with Docker installed.
SSH into the instance and pull the image from Docker Hub:
docker pull <dockerhub-username>/my-node-appRun the container on the EC2 instance:
Run the container on the EC2 instance:Access the Application: You can now access your application on the public IP of the EC2 instance, with port 80.
Common Errors and Troubleshooting
As you work with Docker, you may encounter some common issues. Here are a few and how to resolve them:
Error: Port already in use:
If you see an error that port 3000 is already in use, you can either stop the process using the port or run the container on a different port using the -p flag (e.g., docker run -p 4000:3000).
Error: Docker file build fails:
Ensure that all paths in the COPY commands are correct. If the build fails, check the error message for details.
Missing CMD or ENTRYPOINT instructions:
If your container doesn’t start as expected, check that the CMD or ENTRYPOINT instruction in the Docker file is correctly defined to run your application.
Application not accessible:
Make sure that the container's exposed port is correctly mapped to the host system. Double-check the docker run -p syntax.










Comments