Book for Beginner Web Developers in Docker
Content Index
- 1. Introduction to Docker and its Fundamental Concepts
- What is Docker and why use it?
- Images: Your Application Templates
- Containers: The Running Instances
- 2. Architecture and Essential Docker Commands
- The Client (docker) and the Daemon (dockerd)
- Essential Commands for Management
- 3. Creating Custom Images with Dockerfile
- The Dockerfile
- 4. Orchestrating Multiple Containers with Docker Compose
- What is Docker Compose?
- The docker-compose.yml File
- 5. Optimizing the Development Workflow
- Volumes for Data Persistence
- Docker Compose Watch: Real-Time Synchronization
This book offers a comprehensive guide to getting started in the world of Docker, covering everything from the most elementary concepts to the orchestration of complex applications with Docker Compose, with a practical focus for web developers; that is, you will be able to learn how to use Docker to develop your web apps with Laravel, CodeIgniter, Flask, FastAPI, Django and, ultimately, any web app that you can run with Docker or in production.
1. Introduction to Docker and its Fundamental Concepts
Before diving into commands, it is crucial to understand the philosophy behind Docker and its main components.
What is Docker and why use it?
Docker is a platform that allows you to package applications along with all their dependencies (libraries, configurations, etc.) into standardized units called containers. The primary goal is to ensure that an application runs consistently in any environment, whether it is a developer's machine, a testing server, or the cloud.
The need for Docker is best understood through analogies. Just as virtual environments in Python (venv, pipenv) isolate a project's dependencies to avoid version conflicts (for example, having one project with Django 4 and another with Django 7 on the same machine), Docker encapsulates the entire application and its environment, preventing compatibility issues with the host operating system and other projects.
Images: Your Application Templates
A Docker image is an immutable, read-only template that contains everything needed to run an application: the file system, code, libraries, and environment variables. You can think of an image as an .exe file or a class in object-oriented programming: it is a static package that does nothing on its own until it is executed. Examples of popular images include python, ubuntu, nginx, and postgres.
Containers: The Running Instances
A container is a running instance of an image. If the image is the blueprint, the container is the house built from that blueprint. When launching a container, it runs as an isolated process with its own file system, network, and environment, but sharing the host operating system's kernel, which makes it much lighter than a full virtual machine. Containers are portable, self-contained, and can be created, started, stopped, and deleted independently.
2. Architecture and Essential Docker Commands
To interact with Docker, two main components work together.
The Client (docker) and the Daemon (dockerd)
- Docker Client (docker): It is the command-line tool (CLI) that the user interacts with. When you type a command like docker run, you are using the client.
- Docker Daemon (dockerd): It is the Docker engine, a process that runs in the background and is responsible for managing images, containers, networks, and volumes. The client sends instructions to the daemon, and the daemon executes them.
Essential Commands for Management
- docker images: Lists all the images downloaded to your system.
- docker ps: Shows the containers that are currently running.
- docker ps -a: Shows all containers, both active and stopped.
- docker run <image>: Creates and starts a new container from an image.
- docker stop <id/name>: Stops a running container.
- docker rm <id/name>: Deletes a stopped container.
- docker rmi <id/name>: Deletes an image.
- docker logs <id/name>: Shows the logs of a container, useful for debugging.
- docker exec -it <id/name> bash: Allows access to an interactive terminal inside a container that is already running.
3. Creating Custom Images with Dockerfile
The true power of Docker for developers lies in the ability to create their own images for their projects.
The Dockerfile
A Dockerfile is a text file without an extension that contains a set of sequential instructions to build a custom image. Its typical structure includes:
- FROM: Defines the base image on which the new image will be built (e.g., FROM python:3.12-slim).
- WORKDIR: Sets the working directory inside the container (e.g., WORKDIR /app).
- COPY: Copies files from the host system to the container (e.g., COPY requirements.txt .).
- RUN: Executes commands during the build process, such as installing dependencies (e.g., RUN pip install -r requirements.txt).
- EXPOSE: Informs Docker about the ports the container will expose (e.g., EXPOSE 5050).
- CMD: Specifies the default command to be executed when the container starts (e.g., CMD ["python", "app.py"]).
Once the Dockerfile is defined, the image is built with the command docker build -t my-app ..
4. Orchestrating Multiple Containers with Docker Compose
For applications that require multiple services (such as a web application, a database, and a cache server), Docker Compose simplifies management.
What is Docker Compose?
It is a tool that allows you to define and run multi-container applications through a single configuration file called docker-compose.yml. In this file, the "services" that make up the application are defined, where each service is a container.
The docker-compose.yml File
This YAML file defines services, networks, and volumes. For example, you can define a web service for a Flask application and a redis service for the cache. Compose takes care of creating and connecting the containers automatically. The main commands are docker compose up to start the application and docker compose down to stop it.
5. Optimizing the Development Workflow
For Docker to be an efficient development tool, it is essential that code changes are reflected in real-time without the need to constantly rebuild the image.
Volumes for Data Persistence
Volumes allow data to persist beyond the lifecycle of a container. They function like an external hard drive, mounting a directory from the host system inside the container. This is key for development, as the source code can live on the local machine and be "synchronized" with the container.
Docker Compose Watch: Real-Time Synchronization
Docker Compose includes a watch functionality that monitors changes in local files and automatically synchronizes them with the container. This is configured in the docker-compose.yml file within a develop section. By running docker compose up --watch, code changes are instantly reflected in the application running inside the container, creating an agile and efficient development workflow.
I agree to receive announcements of interest about this Blog.
With this book, you can learn how to use Docker to develop your web projects with Laravel, CodeIgniter, Flask, FastAPI, Django, and ultimately, any web app you can build with Docker.
Algunas recomendaciones:
Benjamin Huizar Barajas
Laravel Legacy - Ya había tomado este curso pero era cuando estaba la versión 7 u 8. Ahora con la ac...
Andrés Rolán Torres
Laravel Legacy - Cumple de sobras con su propósito. Se nota el grandísimo esfuerzo puesto en este cu...
Cristian Semeria Cortes
Laravel Legacy - El curso la verdad esta muy bueno, por error compre este cuando ya estaba la versi...
Bryan Montes
Laravel Legacy - Hasta el momento el profesor es muy claro en cuanto al proceso de enseñanza y se pu...
José Nephtali Frías Cortés
Fllask 3 - Hasta el momento, están muy claras las expectativas del curso