Explore plans starting at ₹699/mo →
DevOps

Docker Containers Explained for Application Developers

S
ServerRaja
9 min read
#Infrastructure#Tutorial#DevOps#Guide#Docker#Containers
Docker Containers Explained for Application Developers

Docker packages applications with their dependencies into portable containers that run consistently across different environments. For developers, Docker eliminates the it works on my machine problem and simplifies both development and deployment.

Images and Containers

A Docker image is a read-only template containing the application code, runtime, libraries, and configuration. Think of it as a snapshot of a server.

A container is a running instance of an image. You can run multiple containers from the same image, each isolated from the others.

Dockerfile

A Dockerfile defines how to build an image:

FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --production COPY . . EXPOSE 3000 CMD ["node", "server.js"]

Each instruction creates a layer in the image. Docker caches layers for faster rebuilds.

Building and Running

Build: docker build -t my-app:latest . Run: docker run -d -p 3000:3000 my-app:latest List: docker ps Logs: docker logs container-id Stop: docker stop container-id

Volumes

Containers are ephemeral: data inside a container is lost when it is removed. Volumes persist data outside the container:

docker run -v /host/path:/container/path my-app

Named volumes (managed by Docker) and bind mounts (host directory mapping) are the two approaches.

Networking

Containers can communicate with each other through Docker networks:

docker network create my-network docker run --network my-network --name app my-app docker run --network my-network --name db postgres

Containers on the same network can reach each other by name.

Docker Compose

Docker Compose defines multi-container applications:

version: '3' services: app: build: . ports: ["3000:3000"] depends_on: [db] db: image: postgres:16 volumes: ["pgdata:/var/lib/postgresql/data"] volumes: pgdata:

Start all services: docker compose up -d

Development Workflow

1. Write code locally 2. Build the Docker image 3. Run the container to test 4. Use Docker Compose for multi-service testing 5. Push the image to a registry for deployment

Best Practices

  • Use multi-stage builds to reduce image size
  • Use .dockerignore to exclude unnecessary files
  • Do not run containers as root
  • Use specific image tags (not :latest) for reproducibility
  • Keep images small (use Alpine-based images)
  • Use health checks in production

Docker simplifies development, testing, and deployment by providing consistent environments across all stages of the software lifecycle.

Key Takeaways

  • Containers package an application with its dependencies into a portable, reproducible unit that runs identically on any system with Docker installed
  • Dockerfile instructions create cached layers — ordering instructions from least to most frequently changing optimizes rebuild speed
  • Volumes persist data outside containers; without them, all data is lost when a container is removed or replaced
  • Docker Compose defines multi-service applications declaratively, making local development environments reproducible with a single command
  • Use multi-stage builds to minimize image size, avoid running containers as root, and pin specific image tags rather than relying on `:latest`
Docker Containers: A Developers Guide | ServerRaja