Deploy a Dockerfile

Deploy a Dockerfile

To deploy a Dockerfile with a custom domain and HTTPS on a cloud server (almost any cloud provider will work), we will walk through the entire process step-by-step. This guide will utilize a simple Node.js application as an example.

Prerequisites

Before we begin, ensure you have the following:

  1. A VPS: A virtual private server with a static IP address.
  2. Domain Name: A registered domain name that you can configure to point to your VPS.
  3. Docker and Docker Compose: Installed on your VPS.
  4. Basic Knowledge of Node.js: Understanding how to create and run a simple Node.js application.

Step 1: Setting Up Your Node.js Application

First, create a simple Node.js application. For this example, we will create an Express server that serves a static HTML file.

Create the Project Structure

Create a directory for your project:

mkdir my-node-app
cd my-node-app

Inside this directory, create the following files:

  • server.js
  • Dockerfile
  • package.json
  • index.html

Sample Code for Each File

package.json

{
  "name": "my-node-app",
  "version": "1.0.0",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}

server.js

const express = require('express');
const path = require('path');
const app = express();

app.use(express.static('public'));

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

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

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Node App</title>
</head>
<body>
    <h1>Hello from My Node App!</h1>
</body>
</html>

Dockerfile

# Use the official Node.js image as the base image
FROM node:14-alpine

# Set the working directory
WORKDIR /app

# Copy package.json and install dependencies
COPY package.json ./
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the port on which the app runs
EXPOSE 3000

# Start the application
CMD ["npm", "start"]

Build and Run Your Node.js Application

To build and run your application in Docker, execute the following commands:

docker build -t my-node-app .
docker run -d -p 3000:3000 --name node-app my-node-app

Step 2: Setting Up Caddy as a Reverse Proxy

Caddy is an excellent choice for a reverse proxy because it automatically manages SSL certificates using Let's Encrypt.

Create Caddy Configuration

Create a new file named Caddyfile in your project directory:

yourdomain.com {
    reverse_proxy node-app:3000
}

Replace yourdomain.com with your actual domain name.

Create Docker Compose File

Next, create a docker-compose.yml file in the same directory to define your services:

version: '3.7'

services:
  node-app:
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped

  caddy:
    image: caddy:latest
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - caddy_data:/data
      - caddy_config:/config

volumes:
  caddy_data:
  caddy_config:

Run Docker Compose

Now you can start both your Node.js application and Caddy using Docker Compose:

docker-compose up -d --build

Step 3: Pointing Your Domain to Your VPS

You need to point your domain to your VPS's static IP address. This is done through your domain registrar's DNS settings:

  1. Log in to your domain registrar's control panel.
  2. Find the DNS settings for your domain.
  3. Create an A record that points to your VPS's IP address.

For example:

  • Type: A
  • Name: @
  • Value: [Your VPS IP Address]
  • TTL: Automatic or 3600 seconds

Step 4: Accessing Your Application

Once everything is set up and running, you should be able to access your application by navigating to https://yourdomain.com. Caddy will automatically handle SSL certification through Let's Encrypt.

Step 5: Verifying HTTPS Setup

To verify that HTTPS is correctly set up:

  1. Open your browser and go to https://yourdomain.com.
  2. You should see a padlock icon in the address bar indicating that the connection is secure.

Troubleshooting Common Issues

  • Caddy Doesn't Start: Check the logs by running docker-compose logs caddy to see if there are any configuration errors.
  • Application Not Accessible: Ensure that your firewall allows traffic on ports 80 and 443.
  • DNS Propagation Delay: Changes to DNS records can take some time to propagate; wait for up to 48 hours if you don’t see immediate results.

Conclusion

In this tutorial, you learned how to deploy a simple Node.js application using Docker with Caddy as a reverse proxy for HTTPS support on a VPS with a custom domain. This setup not only provides you with secure access but also simplifies future deployments due to Docker's containerization capabilities.

By following these steps, you can easily host any web application while ensuring it is secure and accessible via HTTPS without manual certificate management.