Leanpub Header

Skip to main content

Book for Beginner Web Developers in Docker

Learn how to use Docker in your web projects using Laravel, FastAPI, Django, CodeIgniter, and more

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.

This book is a translation into English of Libro para Desarrolladores Web Principiantes en Docker which was originally written in Spanish

Minimum price

$3.00

$20.00

You pay

$20.00

Author earns

$16.00
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
About

About

About the Book

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:

  1. FROM: Defines the base image on which the new image will be built (e.g., FROM python:3.12-slim).
  2. WORKDIR: Sets the working directory inside the container (e.g., WORKDIR /app).
  3. COPY: Copies files from the host system to the container (e.g., COPY requirements.txt .).
  4. RUN: Executes commands during the build process, such as installing dependencies (e.g., RUN pip install -r requirements.txt).
  5. EXPOSE: Informs Docker about the ports the container will expose (e.g., EXPOSE 5050).
  6. 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.

Share this book

Categories

This book is a translation into English of Libro para Desarrolladores Web Principiantes en Docker which was originally written in Spanish

Author

About the Author

Andrés Cruz Yoris

Andrés Cruz Yoris, Licenciado en Computación, con más de 10 años de experiencia en el desarrollo de aplicaciones web en general; trabajo con PHP, Python y tecnologías del lado del cliente como HTML, JavaScript, CSS, Vue entre otras; y del lado del servidor como Laravel, Flask, Django y Codeigniter. También desarrollo con Android Studio, xCode y Flutter para la creación de aplicaciones nativas para Android e IOS.

Pongo a tú disposición parte de mi aprendizaje, reflejado en cada una de las palabras que componen mis libros sobre el desarrollo de aplicaciones en general.

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub