DevOps8 min read

Docker for Beginners: How to Containerise Your Web Application

Docker eliminates the classic 'it works on my machine' problem. Once you understand containers, you will never want to deploy software without them again.

Back to Blog
Docker for Beginners: How to Containerise Your Web Application

What is Docker and Why Should You Care?

Docker packages your application and everything it needs to run — the runtime, libraries, configuration, and dependencies — into a single portable unit called a container. This container runs identically on your laptop, your colleague's Windows PC, your CI/CD pipeline, and your production Linux server. No more environment-specific bugs. No more three-hour onboarding sessions setting up a development environment. A new developer clones your repo, runs 'docker compose up', and has a fully working environment in under 5 minutes.

Containers vs Virtual Machines

A virtual machine (VM) emulates an entire computer including the operating system. A container shares the host OS kernel and only contains the application and its dependencies. This makes containers dramatically lighter — a Docker container starts in seconds and uses megabytes of RAM, while a VM takes minutes to boot and uses gigabytes. You can run dozens of containers on a machine that would support only 2–3 VMs. Containers are not a replacement for VMs — they serve different purposes — but for application deployment, containers are almost always the better choice.

Writing Your First Dockerfile

A Dockerfile is a text file that defines how to build your container image. For a Next.js application:

```

FROM node:22-alpine

WORKDIR /app

COPY package*.json ./

RUN npm ci --only=production

COPY . .

RUN npm run build

EXPOSE 3000

CMD ["node", "server.js"]

```

Each line is a layer. 'FROM' sets the base image. 'WORKDIR' sets the working directory. 'COPY' copies files. 'RUN' executes commands. 'EXPOSE' documents which port the app uses. 'CMD' specifies the startup command. Build this with 'docker build -t my-app .' and run it with 'docker run -p 3000:3000 my-app'.

Docker Compose for Multi-Service Apps

Most real applications have multiple services — a web server, a database, a cache. Docker Compose manages them together with a single YAML file. A basic compose file for a Next.js app with PostgreSQL and Redis: define three services (app, db, redis), specify their images or build contexts, set environment variables, mount volumes for database persistence, and define the network. Running 'docker compose up' starts all three services, creates the network between them, and manages their lifecycle together. 'docker compose down' stops and removes everything cleanly.

Essential Docker Commands Every Developer Needs

'docker build -t name .' builds an image from the current directory's Dockerfile. 'docker run -p 8080:3000 name' runs a container, mapping port 8080 on your machine to port 3000 in the container. 'docker ps' lists running containers. 'docker logs container-id' shows a container's stdout output. 'docker exec -it container-id sh' opens an interactive shell inside a running container — invaluable for debugging. 'docker compose up -d' starts compose services in the background. 'docker system prune' cleans up unused images, containers, and volumes when disk space is running low.

Docker in Production: Key Considerations

In production, you typically use Docker with an orchestrator. For small deployments, Docker Compose on a single VPS works well. For larger, multi-server deployments, Kubernetes manages container scheduling, scaling, and self-healing. Managed Kubernetes services — AWS EKS, Google GKE, DigitalOcean Kubernetes — remove the operational complexity of managing Kubernetes itself. Always push your images to a private registry (AWS ECR, Docker Hub private repos) rather than building on the production server. Use Docker secrets or environment variable injection for sensitive configuration — never bake passwords into your Dockerfile.

Enjoyed reading? Leave us a review

Your feedback helps us grow and helps others discover our services.

Review on GoogleReview on Trustpilot

Let's Build Your Next Project

From hosting to full-stack development — webzworld has the expertise to scale your business.