Sitemap

Introduction

2 min readJul 3, 2025

--

Docker has revolutionized the way applications are deployed, making it easier to manage, scale, and run applications consistently across different environments. Migrating your application to Docker can lead to improved efficiency and reliability, but it requires careful planning and execution. This guide will walk you through the essential steps of migrating an application to Docker, including practical TypeScript examples for Node.js applications.

Understanding Docker

Before diving into the migration process, it’s crucial to understand what Docker is. Docker is a platform that uses OS-level virtualization to deliver software in containers, which are lightweight, portable, and consistent across environments.

Step 1: Assess Your Application

Start by evaluating your existing application to understand its dependencies and architecture. Check for:

  • Dependencies: List all external libraries and services.
  • Configuration: Identify environment-specific configurations.
  • State Management: Determine how your application handles state, as containers are ephemeral.

Step 2: Write a Dockerfile

A Dockerfile is a text document that contains all the commands necessary to assemble an image.

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

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install application dependencies
RUN npm install

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

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

This Dockerfile uses a Node.js base image, copies application files, installs dependencies, and starts the application.

Step 3: Create Docker Compose File

Docker Compose simplifies the process of managing multi-container applications. Here’s a basic example:

version: '3'
services:
web:
build: .
ports:
- "8080:8080"
environment:
NODE_ENV: development
volumes:
- .:/app

Get @rnab’s stories in your inbox

Join Medium for free to get updates from this writer.

The Docker Compose file defines a service for the web application, maps the local port 8080 to the container’s port 8080, and sets an environment variable.

Step 4: Containerize Your Node.js Application

Assuming you’re working with a TypeScript project, here’s how to bundle and run it inside a Docker container:

  1. Install TypeScript dependencies:
  • npm install --save-dev typescript ts-node
  1. Add a TypeScript configuration file (tsconfig.json):
  • { "compilerOptions": { "target": "ES6", "module": "commonjs", "rootDir": "src", "outDir": "dist", "strict": true }, "include": ["src"] }
  1. Build the TypeScript project:
  • npx tsc
  1. Adjust Dockerfile for TypeScript:
  • # Install TypeScript globally RUN npm install -g typescript # Copy dist files COPY ./dist /app/dist # Start the built JavaScript CMD ["node", "dist/index.js"]

Step 5: Test and Validate

Run the Docker container and ensure that your application works as expected:

docker-compose up --build

Visit http://localhost:8080 in your browser to verify.

Conclusion

Migrating an application to Docker involves understanding your application’s architecture, writing Docker-specific configuration files, and testing thoroughly. While the focus here is on a Node.js application, the general principles apply to other languages and frameworks as well. With Docker, you ensure that your application runs consistently in any environment, paving the way for efficient development and deployment processes.

--

--

@rnab
@rnab

Written by @rnab

Typescript, Devops, Kubernetes, AWS, AI/ML, Algo Trading