Using Docker Compose to Deploy Express.js in a Container [Deployment 101]

Posted by

BE Course – Express.js แบบ Container ด้วย Docker Compose [Deployment 101]

BE Course – Express.js แบบ Container ด้วย Docker Compose [Deployment 101]

Express.js is a popular web application framework for Node.js. It allows you to build scalable and efficient web applications and APIs. In this article, we will explore how to deploy an Express.js application as a container using Docker Compose.

What is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define the services that make up your app in a simple YAML file, and then use that file to create and start all the services with just a single command.

Setting up the Express.js Application

First, you will need to have an Express.js application that you want to deploy. If you don’t have one yet, you can create a new Express.js app by running the following commands:


$ mkdir my-express-app
$ cd my-express-app
$ npm init -y
$ npm install express

Once you have your Express.js application ready, you can create a Dockerfile to define the image for your app. Here’s an example Dockerfile for an Express.js app:


FROM node:14

WORKDIR /app

COPY package.json .
COPY package-lock.json .

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "app.js"]

Creating a Docker Compose File

Now that you have your Express.js app and Dockerfile ready, you can create a Docker Compose file to define the services for your app. Create a file called docker-compose.yml in the root of your project and add the following content:


version: '3'
services:
web:
build: .
ports:
- "3000:3000"

In this file, we define a service called web, which uses the Dockerfile in the current directory to build the image for our app. We also map port 3000 on the host to port 3000 in the container, so that we can access our app from the host machine.

Deploying the Express.js App

Once you have your Docker Compose file ready, you can deploy your Express.js app as a container by running the following command in the root of your project:


$ docker-compose up -d

This command will start all the services defined in the docker-compose.yml file in detached mode, meaning that they will run in the background. You can then access your Express.js app by navigating to http://localhost:3000 in your web browser.

Conclusion

Deploying an Express.js application as a container using Docker Compose is a simple and efficient way to manage your app’s infrastructure. With just a few lines of configuration, you can define and run all the services that make up your app, making it easy to scale and maintain your application.

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@ylnw
6 months ago

typescript ต้องมาละครับ

@johntdc4856
6 months ago

ขอโทดครับYTไม่แจ้งเคือนผมลบเม้นทิ้งแต่ผมอ่านในที่แจ้งเตือนแล้วนะครับ อยากบอกว่ากำลังไล่ดูคริบพี่อยู่เป็นการอธิบายที่ผมดูแล้วผมชอบครับ ตอนดู letcode ก้สหนุกมาก ขอบคุณครับที่ทำคริบดีๆมาให้ดูผมก้โชดดีมากนะเนี่ยที่เห็นวีดีโอจากช่องพี่เด้งมาฮาๆ