close
close
docker how to run nodejs express app with https

docker how to run nodejs express app with https

3 min read 06-09-2024
docker how to run nodejs express app with https

If you're looking to secure your Node.js Express application using HTTPS in a Docker container, you’re in the right place. In this guide, we'll walk you through the steps necessary to achieve this, using simple explanations and examples to make the process clear and straightforward.

Table of Contents

  1. Prerequisites
  2. Creating a Simple Node.js Express Application
  3. Setting Up HTTPS
  4. Dockerizing the Application
  5. Running Your Docker Container
  6. Conclusion

Prerequisites

Before you start, ensure you have the following installed on your machine:

  • Node.js: This will allow you to run JavaScript server-side.
  • Docker: A platform to automate the deployment of applications in containers.
  • OpenSSL: To generate SSL certificates for HTTPS.

Creating a Simple Node.js Express Application

First, let's create a basic Node.js Express application. Open your terminal and follow these steps:

# Create a new directory for your app
mkdir my-express-app
cd my-express-app

# Initialize a new Node.js project
npm init -y

# Install Express
npm install express

Now create a file named server.js:

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

app.get('/', (req, res) => {
    res.send('Hello, Secure World!');
});

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

Explanation

In this script, we set up a simple Express server that listens on port 3000 and responds with "Hello, Secure World!" when accessed.

Setting Up HTTPS

To run your Express app over HTTPS, you need to create an SSL certificate. You can use OpenSSL to generate a self-signed certificate. Here’s how:

# Generate a self-signed SSL certificate
openssl req -nodes -new -x509 -keyout server.key -out server.cert

You will be prompted to enter some information; you can fill it out or leave it blank.

Updating server.js for HTTPS

Modify your server.js file to use HTTPS:

// server.js
const express = require('express');
const https = require('https');
const fs = require('fs');
const app = express();
const PORT = process.env.PORT || 3000;

// SSL certificate
const options = {
    key: fs.readFileSync('server.key'),
    cert: fs.readFileSync('server.cert')
};

app.get('/', (req, res) => {
    res.send('Hello, Secure World!');
});

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

Explanation

We’ve now included the necessary code to create an HTTPS server using our SSL certificate files.

Dockerizing the Application

Now, let’s create a Dockerfile to containerize our application. Create a file named Dockerfile in the project root:

# Dockerfile
FROM node:14

# Set the working directory
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the application code
COPY . .

# Expose the port
EXPOSE 3000

# Command to run the application
CMD ["node", "server.js"]

Explanation

  • FROM node:14: We’re using the official Node.js image.
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies your package.json and application code into the container.
  • RUN npm install: Installs the necessary dependencies.
  • EXPOSE 3000: Exposes port 3000 for outside access.
  • CMD: The command to start the application.

Running Your Docker Container

Now that everything is set up, you can build and run your Docker container:

# Build the Docker image
docker build -t my-express-app .

# Run the Docker container
docker run -p 3000:3000 my-express-app

Explanation

  • docker build: Builds the image using the Dockerfile.
  • docker run: Runs the container and maps port 3000 on your host to port 3000 on the container.

Conclusion

Congratulations! You've successfully set up a Node.js Express application with HTTPS, running inside a Docker container. You can now navigate to https://localhost:3000 in your web browser to see your app in action (you may need to accept the self-signed certificate).

Additional Resources

For more information on Node.js and Docker, check out:

By following these steps, you’ll not only enhance your app’s security but also gain valuable experience working with Docker. Happy coding!

Related Posts


Popular Posts