Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

How To Dockerize A Node - Js Application With Docker and Docker Compose

Uploaded by

Dex
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

How To Dockerize A Node - Js Application With Docker and Docker Compose

Uploaded by

Dex
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

# How to Dockerize a Node.

js Application with Docker and Docker Compose

This tutorial will guide you through containerizing a basic Node.js application
using Docker. We’ll also set up Docker Compose to streamline development and
deployment.

---

### Prerequisites

- Basic knowledge of Node.js and Docker


- Docker and Docker Compose installed on your machine

---

### Step 1: Set Up Your Node.js Application

1. Create a directory for your project and navigate into it.

```bash
mkdir docker_node_app
cd docker_node_app
```

2. Initialize a new Node.js project and install `express`.

```bash
npm init -y
npm install express
```

3. Create an `app.js` file in the root of the project directory with a simple
server configuration.

```javascript
// app.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {


res.send('Hello, Dockerized World!');
});

app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
```

---

### Step 2: Create a Dockerfile

In the root of your project directory, create a file named `Dockerfile` to define
how the application will be built and run inside the Docker container.

```dockerfile
# Dockerfile
# Step 1: Use the official Node.js image as the base image
FROM node:14

# Step 2: Set the working directory in the container


WORKDIR /usr/src/app

# Step 3: Copy package.json and package-lock.json files for dependency installation


COPY package*.json ./

# Step 4: Install dependencies


RUN npm install

# Step 5: Copy the application code into the container


COPY . .

# Step 6: Expose the port the app runs on


EXPOSE 3000

# Step 7: Define the command to run the application


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

---

### Step 3: Create a `.dockerignore` File

Add a `.dockerignore` file to specify files and directories that should be excluded
from the Docker image.

```plaintext
node_modules
npm-debug.log
```

---

### Step 4: Build the Docker Image

In the terminal, run the following command to build the Docker image for the
application.

```bash
docker build -t docker-node-app .
```

Here:
- `docker build` initiates the Docker image build process.
- `-t docker-node-app` tags the image with the name `docker-node-app`.
- `.` specifies the build context (current directory).

---

### Step 5: Run the Docker Container

To run the Docker container from the image you just created:

```bash
docker run -p 3000:3000 docker-node-app
```
This maps port 3000 of the container to port 3000 on your local machine. You can
now access the application at `http://localhost:3000`.

---

### Step 6: Set Up Docker Compose

Docker Compose allows you to manage multi-container applications more efficiently.


We’ll create a `docker-compose.yml` file to define and manage the Node.js
application container.

1. Create a file named `docker-compose.yml` in the project root.

```yaml
# docker-compose.yml
version: '3'

services:
app:
build: .
ports:
- "3000:3000"
environment:
- PORT=3000
```

2. Run the application using Docker Compose:

```bash
docker-compose up
```

This command will build and run the application in a container based on the
`docker-compose.yml` configuration.

---

### Step 7: Test the Application

With Docker Compose running, you should be able to access the application by going
to `http://localhost:3000` in your browser. You should see the message:

```
Hello, Dockerized World!
```

To stop the application, press `CTRL+C`, and then bring down the container network
using:

```bash
docker-compose down
```

---

### Step 8: Optional - Running in Detached Mode

To run the containers in the background (detached mode), add the `-d` flag:
```bash
docker-compose up -d
```

You can stop the container by running:

```bash
docker-compose down
```

---

### Conclusion

Congratulations! You’ve successfully Dockerized a Node.js application and used


Docker Compose to manage it. This setup can be expanded to add other services like
databases or cache layers, all managed through Docker Compose for ease of
deployment and scalability.

---

You might also like