Skip to main content

Command Palette

Search for a command to run...

Dockerize your application

Published
3 min read
Dockerize your application
T

I am a Full stack Developer from India and Tech savvy.

Docker is a tool that allows you to create, run, and deploy applications using containers. Containers are isolated environments that contain everything your application needs to run, such as code, libraries, dependencies, and configuration files. By using Docker, you can ensure that your application runs consistently across different platforms and environments, such as development, testing, and production.

The first step to dockerize your backend application is to create a Dockerfile. A Dockerfile is a text file that contains instructions for building a Docker image. A Docker image is a snapshot of your application and its dependencies that can be used to create and run containers. To create a Dockerfile, you need to specify the base image, the commands to install the dependencies, the files to copy to the image, and the command to run the application.

Dockerize Node js application

Node js is the most popular backend runtime which helps in creating applications quicker and scale easily. Using express js framework we can create API quicker and maintainable

Let's create a Dockerfile for the express js application which is running on port 8000

FROM node:20

WORKDIR /app
COPY package*.json ./
RUN npm install && npm prune --production

COPY . .
EXPOSE 8000
CMD ["node", "index.js"]

Dockerize Java Spring Boot Maven app

Spring Boot is the most popular across enterprise applications and microservice applications. Spring Boot is used in organizations like Netflix, Uber, LinkedIn, etc. Spring Boot has gained popularity due to Dependency injection, aspect-oriented programming, and decoupling.

FROM maven:3.8.4-openjdk-17 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean install

FROM openjdk:17-jre-slim

# Set the working directory to /app
WORKDIR /app
# Copy the generated SNAPSHOT jar from build to /app
COPY --from=build /app/target/<APP>.jar app.jar

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

# Run the JAR file when the container launches
CMD ["java", "-jar", "app.jar"]

Dockerize Python Django application

Django is the most popular framework in Python web applications. Django has gained its popularity due to features like rapid development, security, and authentication. which are prebuilt are ready to use features

# Use an official Python runtime as a parent image
FROM python:3.10-slim

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Create and set the working directory
WORKDIR /app

# Copy the requirements file into the container at /app
COPY requirements.txt /app/

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

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

# Expose port 8000 to the outside world
EXPOSE 8000

# Run the Django application
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Outro ❤️

I am making this series of content to help other aspiring developers who want to start their career in Web Development. It takes a lot of effort to make the series, If you really like my work Buy me a Coffee and Follow me on Twitter techtoe and Instagram Tech Shark (@techshark_web)

Dockerize your application