In today’s fast-paced development world, containerization has become a crucial skill for developers and DevOps professionals alike. In this tutorial, we’ll walk through the process of creating a simple web application, containerizing it with Docker, and preparing it for deployment on a cloud platform.
Project Overview
Our project will consist of four main steps:
- Creating a simple web application
- Writing a Dockerfile
- Building and running the Docker container
- Preparing for deployment on a cloud platform
Let’s dive in!
Step 1: Create a Simple Web App
For this tutorial, we’ll use Node.js and Express to create a basic web application. If you don’t have Node.js installed, you can download it from the official website.
First, initialize a new Node.js project:
mkdir dockerized-web-app
cd dockerized-web-app
sudo apt update
sudo apt install nodejs npm
npm init -y
npm install express
Now, create a file named app.js
with the following content:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, Docker!');
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
This creates a simple Express server that responds with “Hello, Docker!” when you visit the root URL.
Step 2: Write a Dockerfile
Next, we’ll create a Dockerfile to containerize our application. Create a file named Dockerfile in your project directory with the following content
# Base image
FROM node:14
# Set working directory
WORKDIR /app
# Copy app files
COPY . .
# Install dependencies
RUN npm install
# Expose port
EXPOSE 3000
# Start the app
CMD ["node", "app.js"]
This Dockerfile does the following:
- Uses Node.js 14 as the base image
- Sets the working directory to /app
- Copies our application files into the container
- Installs dependencies
- Exposes port 3000
- Specifies the command to start our application
Step 3: Build and Run the Container
Now that we have our Dockerfile, let’s build and run our container:
# Build the Docker image
docker build -t my-web-app .
# Run the container
docker run -p 3000:3000 my-web-app
If everything went well, you should be able to visit http://localhost:3000 your browser and see "Hello, Docker!".
Step 4: Prepare for Cloud Deployment
The final step is to prepare our application for deployment on a cloud platform. For this example, we’ll use AWS Elastic Container Service (ECS), but the process is similar for other cloud providers.
- Push your image to Docker Hub
docker tag my-web-app:latest akhlab/my-web-app:latest
docker push akhlab/my-web-app:latest
- Create an ECS cluster in the AWS Console.
- Create a task definition using your Docker Hub image.
- Create a service in your ECS cluster using the task definition.
The exact steps for cloud deployment can vary based on your specific requirements and the cloud provider you’re using. Be sure to consult your cloud provider’s documentation for detailed instructions.
Conclusion
In this tutorial, we’ve walked through the process of creating a simple web application, containerizing it with Docker, and preparing it for cloud deployment. This is just the beginning of what you can do with Docker and containerization.
Some next steps you might consider:
- Add more complex functionality to your web application
- Implement a CI/CD pipeline for automated testing and deployment
- Explore Docker Compose for multi-container applications
Happy learning, and may your containers always be light and your deployments smooth!
Top comments (1)
Useful i just started learning docker