simple_docker

Docker for Beginners

What is Docker?

Docker is a tool that packages software into standardized units called containers. Think of a container as a lightweight, portable box that contains everything your application needs to run: the code, runtime, libraries, and settings.

The Problem Docker Solves

Have you ever experienced:

Docker solves these problems by packaging everything together so the software runs identically everywhere.

A Simple Analogy

Think of Docker like shipping containers used in global trade:

Similarly, Docker containers package software in a standard format that runs anywhere Docker is installed.

Key Concepts

Image

An image is a read-only template. It's like a recipe or blueprint. Images are created once and can be shared. Examples:

Container

A container is a running instance of an image. Like a class and an object in OOP:

You can run many containers from the same image.

Registry

A registry stores images. Docker Hub is the public registry with thousands of free images. When you "pull" an image, you download it from a registry.

Why Use Docker from Eiffel?

With simple_docker, your Eiffel applications can:

1. Run Databases Without Installation

Need PostgreSQL? Instead of installing it on your system:

create spec.make ("postgres:16-alpine")
spec.add_env ("POSTGRES_PASSWORD", "secret")
   .add_port (5432, 5432)
   .do_nothing
container := client.run_container (spec)

Your database is running in seconds. No installer, no configuration files.

2. Run Isolated Tests

Each test gets its own fresh environment:

-- Start fresh database for each test
container := client.run_container (spec)
-- Run your tests
run_database_tests
-- Clean up completely
client.remove_container (container.id, True)

3. Deploy Consistent Environments

Package your Eiffel application with all its dependencies:

create builder.make ("alpine:latest")
builder.copy_files ("my_app.exe", "/app/")
       .cmd (<<"/app/my_app.exe">>)
       .do_nothing

4. Run Third-Party Services

Redis, RabbitMQ, Elasticsearch - run any service without learning its installation:

-- Need a cache server?
create spec.make ("redis:alpine")
spec.add_port (6379, 6379).do_nothing
client.run_container (spec)

Minimal Complete Example

This example runs a container, captures its output, and cleans up:

class
    DOCKER_HELLO_WORLD

create
    make

feature {NONE}

    make
        local
            client: DOCKER_CLIENT
            spec: CONTAINER_SPEC
            container: detachable DOCKER_CONTAINER
        do
            -- Connect to Docker
            create client.make

            if client.ping then
                print ("Docker is running!%N")

                -- Create a simple container that prints a message
                create spec.make ("alpine:latest")
                spec.set_cmd (<<"echo", "Hello from Docker!">>).do_nothing

                -- Run it
                container := client.run_container (spec)

                if attached container as c then
                    -- Wait for it to finish
                    client.wait_container (c.id).do_nothing

                    -- Get the output
                    if attached client.container_logs (c.id, True, False, 100) as logs then
                        print ("Container output: " + logs + "%N")
                    end

                    -- Clean up
                    client.remove_container (c.id, True).do_nothing
                    print ("Container removed%N")
                end
            else
                print ("Docker is not running. Please start Docker Desktop.%N")
            end
        end

end

Next Steps

  1. Install Docker Desktop (free for personal use)
  2. Start Docker Desktop and wait for it to say "Docker is running"
  3. Read the User Guide for installation
  4. Try the Cookbook recipes for real-world examples

FAQ

Is Docker a virtual machine?

No. Virtual machines emulate complete computers with their own operating system. Containers share the host's operating system kernel, making them much lighter and faster to start (seconds vs. minutes).

Do I need Linux to use Docker?

No. Docker Desktop runs on Windows and macOS. It handles Linux containers seamlessly through a lightweight VM. simple_docker connects via Windows named pipes.

Is it safe?

Containers are isolated from your system. They can only access what you explicitly allow (ports, files, etc.). Always use official images from trusted sources.

What if I don't have internet?

Images only need to be downloaded once. After that, they're cached locally. You can also export/import images for offline use.