docker fundamentals for web developers

Welcome dear reader, today you will learn the fundamentals of Docker and why it has become the standard way to run web applications. If you ever heard the phrase “it works on my machine”, Docker is the tool that ends those discussions: it packages your application together with everything it needs, so it runs identically everywhere.

At re:Software S.L., we create innovative solutions for businesses that aim to digitize their processes and routines, where virtualisation with Docker are key to satisfy our customers high traffic demands.

Table of contents

What is Docker and How Does It Work?

Docker is a platform for building and running containers. A container is an isolated environment that bundles an application with its dependencies — the PHP version, extensions, libraries — while sharing the host’s operating system kernel. Unlike a virtual machine, a container starts in milliseconds and costs almost no extra memory.

For web developers, this means your development environment matches production exactly. Laravel’s own Docker tooling, which we touched on in the guide about building your own SaaS with Laravel, is built on exactly these concepts.

Images vs Containers: What is the Difference?

An image is a read-only template — a recipe — that defines your application environment. A container is a running instance of that image. One image can spawn many containers, just like one class can spawn many objects.

You can pull ready-made images from Docker Hub, the public registry:

# Download an official image and start a container
docker run -d --name my-mysql -e MYSQL_ROOT_PASSWORD=CrazyPassword! mysql:8.0

# List running containers
docker ps

The -d flag runs the container in the background, and -e passes environment variables — here the MySQL root password, which you should keep private as always.

How to Write Your First Dockerfile

A Dockerfile describes how to build an image for your own application. Here is a minimal one for a PHP application:

# Base image with PHP installed
FROM php:8.2-fpm

# Install a PHP extension
RUN docker-php-ext-install pdo_mysql

# Copy the application source into the image
COPY . /var/www/html

# Set the working directory
WORKDIR /var/www/html

Build and run it with two commands:

# Build an image tagged "my-app" from the Dockerfile
docker build -t my-app .

# Run a container from that image
docker run -d --name my-web my-app

Each instruction creates a layer, and Docker caches layers — so rebuilds only re-execute what changed.

How to Run Multi-Container Apps with Docker Compose

Real web apps need several services: your application, a web server, a database, maybe Redis. Docker Compose lets you define them all in one docker-compose.yml file:

services:
  app:
    build: .
    ports:
      - "8080:80"
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: CrazyPassword!

Now a single command starts the whole stack:

# Start all services in the background
docker compose up -d

# Stop everything again
docker compose down

Conclusion

That’s already it for Docker fundamentals. You now know the difference between images and containers, how to write a Dockerfile, and how Docker Compose orchestrates multi-service stacks. Spin up a container for your next side project — once you work this way, you will not want to go back. Leave a comment below if you have any questions or feedback about the content of this article.

By Editorial Team

A source code library for the open world! Find snippets and examples for popular programming languages including PHP, Javascript, Typescript, Node.js, Vue.js, and more.

Leave a Reply