Using Docker to Containerize REST APIs

Tutorial 2 of 5

Introduction

In this tutorial, we will learn about using Docker to containerize REST APIs. The goal is to understand how to package your RESTful service into a Docker container, allowing it to be easily distributed and run on any platform that supports Docker.

We will cover the following topics:

  • What is Docker and why use it
  • How to create a Dockerfile
  • How to build a Docker image from a Dockerfile
  • How to run a Docker container from an image

Prerequisites

  • Basic understanding of REST APIs.
  • Basic knowledge of Docker.
  • Docker installed on your machine.

Step-by-Step Guide

Understanding Docker

Docker is a platform that allows you to automate the deployment, scaling, and management of applications. It does this by using containerization, which is a lightweight form of virtualization. Docker allows you to package an application with everything it needs to run, including libraries, system tools, code, and runtime.

Creating a Dockerfile

The Dockerfile is a text file that contains instructions on how to build a Docker image. Here is an example of a simple Dockerfile for a Node.js REST API:

# Use an official Node.js runtime as a parent image
FROM node:14 

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

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./ 

# Install any needed packages
RUN npm install 

# Bundle app source
COPY . . 

# Make port 8080 available to the world outside this container
EXPOSE 8080 

# Run the app when the container launches
CMD [ "node", "server.js" ] 

Building a Docker Image

To build a Docker image from a Dockerfile, you use the docker build command:

docker build -t my-api .

This command tells Docker to build an image using the Dockerfile in the current directory (.) and tag (-t) the image with the name "my-api".

Running a Docker Container

To run a Docker container from an image, you use the docker run command:

docker run -p 8080:8080 -d my-api

This command tells Docker to run a container from the "my-api" image, map port 8080 inside the container to port 8080 on the host, and run the container in the background (-d).

Code Examples

Here is a simple Node.js REST API that we will containerize:

// server.js
const express = require('express');
const app = express();
const port = 8080;

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

app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`);
});

Here's the Dockerfile for this app:

FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]

To build the image:

docker build -t my-api .

To run the container:

docker run -p 8080:8080 -d my-api

After running the container, you should be able to access the API at http://localhost:8080.

Summary

We've covered the basics of containerizing a REST API using Docker. We've discussed Dockerfiles, building Docker images, and running Docker containers. The next step would be to learn about Docker Compose, which allows you to define and run multi-container Docker applications.

Practice Exercises

  1. Create a REST API with a different programming language (like Python or Java) and containerize it using Docker.
  2. Add a database to your API (like MongoDB or PostgreSQL) and use Docker Compose to run your API and database in separate containers.
  3. Deploy your containerized API to a cloud service like AWS or Google Cloud.

Remember, the best way to learn is by doing. Have fun practicing!

Additional Resources