CSS

Deploying Serverless Functions with Docker and Vercel

Michael Rodriguez

Michael Rodriguez

June 1, 2026

Deploying Serverless Functions with Docker and Vercel

Deploying Serverless Functions with Docker and Vercel

Serverless functions have become a staple in modern web development, allowing you to run code without managing servers. However, deploying these functions can be tricky. In this guide, you'll learn how to containerize your serverless functions using Docker and deploy them on Vercel, a popular cloud platform for serverless applications.

Why Use Docker for Serverless Functions?

Docker provides a consistent environment for your serverless functions, ensuring that your code runs as expected in different environments. By containerizing your functions, you can easily manage dependencies and configurations.

Setting Up Your Dockerfile

Let's create a basic Dockerfile for our serverless function. We'll use Node.js as our runtime since it's widely supported by Vercel.


# Use an official Node.js runtime as a parent image
FROM node:16-alpine

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

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

# Copy the rest of the application code
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Define the command to run the application
CMD ["node", "index.js"]
  

This Dockerfile sets up a Node.js environment, installs dependencies, and runs the server on port 3000.

Building and Testing the Docker Image

Build the Docker image locally and test it to ensure it works as expected.


docker build -t my-serverless-function .
docker run -p 3000:3000 my-serverless-function
  

Open your browser and navigate to http://localhost:3000 to verify that your function is running correctly.

Deploying to Vercel

To deploy your Dockerized function to Vercel, follow these steps:

  1. Sign in to your Vercel account and create a new project.
  2. Choose the "Import Project" option and select your local project folder.
  3. Select "Docker" as the build system.
  4. Configure the environment variables if needed.
  5. Click "Deploy" to start the deployment process.

Vercel will automatically build your Docker image and deploy it to their serverless infrastructure.

Handling Triggers and Events

Serverless functions often need to handle events like HTTP requests or database triggers. For example, let's create a simple HTTP trigger function:


// index.js
const express = require('express')
const app = express()

app.get('/', (req, res) => {
  res.send('Hello from my serverless function!')
})

module.exports = app
  

To test this function, you can use a tool like Postman or cURL:


curl http://localhost:3000/
  

This should return "Hello from my serverless function!"

Best Practices and Tips

  • Keep your Dockerfile simple and lean to improve build times.
  • Use environment variables for configuration instead of hardcoding values.
  • Monitor your deployed functions for performance and errors.
  • Take advantage of Vercel's built-in features like automatic HTTPS and CDN.

Conclusion

  • You now know how to containerize and deploy serverless functions using Docker and Vercel.
  • Containerization ensures consistency across different environments.
  • Using Vercel simplifies the deployment process and leverages its powerful serverless infrastructure.

Start building your serverless applications today! Experiment with different triggers and functions to see what works best for your project.

Tags

Michael Rodriguez

Written by

Michael Rodriguez

A passionate developer sharing insights and experiences in web development, design, and modern technologies.