Skip to content

Docker Fundamentals for Backend Developers

flowchart LR
    A[Dockerfile] -->|docker build| B[Docker Image]
    B -->|docker run| C[Container]

The full path from source code to a running container, for a Maven-based Spring Boot app:

flowchart LR
A[Source Code] --> B[Maven Build]
B --> C[JAR]
C --> D[Docker Image]
D --> E[Docker Container]
E --> F[Kubernetes / Server]
Component Purpose Analogy
Dockerfile Instructions to build an image Recipe
Docker Image Packaged application and runtime Packed meal
Container Running instance of an image Meal being eaten

A Dockerfile is a text file containing instructions for building an image.

FROM node:20
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npx", "nodemon", "app.js"]
FROM eclipse-temurin:17-jdk-jammy
WORKDIR /app
COPY target/myapp.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

Build:

Terminal window
docker build -t node-app .

Run:

Terminal window
docker run -d --name node-app -p 3000:3000 node-app

Logs:

Terminal window
docker logs node-app

Stop:

Terminal window
docker stop node-app

Compose is used to orchestrate multiple containers.

flowchart LR
    App[Node/Spring Container] --> DB[(Database Container)]
version: "3.8"
services:
app:
build: .
ports:
- "3000:3000"
depends_on:
- postgres
postgres:
image: postgres:16
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: sampledb
Without Compose With Compose
Manual networking Automatic networking
Multiple docker run commands Single command
Hard to maintain YAML-based configuration

Run:

Terminal window
docker compose up --build

Stop:

Terminal window
docker compose down

Node.js/Spring Boot applications contain custom business logic, so they require a Dockerfile.

Databases like PostgreSQL and MySQL are standardized products maintained by their vendors, so it is best practice to use their official images.

Terminal window
docker login
docker tag node-app username/node-app:1.0
docker push username/node-app:1.0
Terminal window
docker save -o node-app.tar node-app
docker load -i node-app.tar
  • Maven is required only to build the JAR.
  • Maven is not needed in the runtime image.
  • Multi-stage Docker builds are recommended for production.
  • Dockerfile builds an image.
  • Images become containers when executed.
  • Use Docker Compose for multi-container applications.
  • Use official database images whenever possible.
  • Package only your application into custom images.