Docker Fundamentals for Backend Developers
Dockerfile, Image and Container
Section titled “Dockerfile, Image and Container”Relationship
Section titled “Relationship”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 |
Dockerfile
Section titled “Dockerfile”A Dockerfile is a text file containing instructions for building an image.
Node.js Example
Section titled “Node.js Example”FROM node:20
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npx", "nodemon", "app.js"]Spring Boot Example
Section titled “Spring Boot Example”FROM eclipse-temurin:17-jdk-jammy
WORKDIR /app
COPY target/myapp.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]Building and Running Images
Section titled “Building and Running Images”Build:
docker build -t node-app .Run:
docker run -d --name node-app -p 3000:3000 node-appLogs:
docker logs node-appStop:
docker stop node-appDocker Compose
Section titled “Docker Compose”Compose is used to orchestrate multiple containers.
flowchart LR
App[Node/Spring Container] --> DB[(Database Container)]
Example
Section titled “Example”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: sampledbWhy Compose?
Section titled “Why Compose?”| Without Compose | With Compose |
|---|---|
| Manual networking | Automatic networking |
| Multiple docker run commands | Single command |
| Hard to maintain | YAML-based configuration |
Run:
docker compose up --buildStop:
docker compose downWhy the Database Uses an Official Image
Section titled “Why the Database Uses an Official Image”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.
Sharing Images
Section titled “Sharing Images”Docker Hub
Section titled “Docker Hub”docker logindocker tag node-app username/node-app:1.0docker push username/node-app:1.0Offline
Section titled “Offline”docker save -o node-app.tar node-appdocker load -i node-app.tarSpring Boot Docker Notes
Section titled “Spring Boot Docker Notes”- Maven is required only to build the JAR.
- Maven is not needed in the runtime image.
- Multi-stage Docker builds are recommended for production.
Key Takeaways
Section titled “Key Takeaways”- 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.