liveBook · Manning

Container

As explained in Rust Servers, Services, and Apps

An execution environment in which an application runs inside an isolated, portable software container.

A container runs an application with its dependencies in an isolated, portable environment. Docker containers have their own processes, network interfaces, and volume mounts, but share the host’s operating-system kernel. Unlike a virtual machine, a container does not include a full operating system. section 12.1.2

A Docker image is the template; a container is an instance started from it. To check the distinction in practice, pull the `hello-world` image and run it with `docker run hello-world`. section 12.2.1

# Use the main rust Docker image
FROM rust
 
# copy app into docker image
COPY . /app
 
# Set the workdirectory
WORKDIR /app
 
# build the app
RUN cargo build --release
 
# start the application
CMD ["./target/release/docker-rust"]

This first Dockerfile copies a Rust web service into an image, builds a release binary, and sets that binary as the container’s startup command. section 12.2.2

Build and run the service, then address what the first container leaves unresolved:

  • Build with `docker build -f Dockerfile-basic . -t docker-rust-basic`, then run with `docker run -p 8080:8080 -t docker-rust-basic`. Check both HTTP routes in a browser. section 12.2.2
  • The initial image is 1.32 GB because it retains the Rust compiler and build artifacts. A multistage build can keep only the final binary in the runtime image. section 12.2.2 section 12.6
  • Run Postgres in a separate container. Put its data in a Docker volume so records survive stopping and restarting the container. section 12.3.1 section 12.3.2
version: '3'
services:
  db:                                         #1
    container_name: tutor-postgres
    restart: always
    image: postgres:latest
    environment:
     - POSTGRES_USER=postgres
     - POSTGRES_PASSWORD=postgres
     - POSTGRES_DB=ezytutors
    volumes:
     - tutor-data:/var/lib/postgresql/data
     - ./c12-data/initdb.sql:/docker-entrypoint-initdb.d/initdb.sql
     - ./c12-data/init-tables.sql:/docker-entrypoint-initdb.d/init-tables.sql
    ports:
     - 5432:5432
    networks:
     - tutor-network
  api:                                       #2
    restart: on-failure
    container_name: tutor-webservice
    build:                                   #3
      context: ./
      dockerfile: Dockerfile-tutor-webservice
      network: host
    environment:                             #4
     - DATABASE_URL=${DATABASE_URL}
     - HOST_PORT=${HOST_PORT}
    depends_on:
     - db                                    #5
    ports:
     - ":3000:3000"                          #6
    networks:
     - tutor-network                         #7
volumes:
        tutor-data:
networks:
        tutor-network:

The Compose configuration shows separate database and API services, with image or build settings, environment variables, storage, ports, and networking. section 12.5

Docker Compose builds, starts, and stops the application’s containers as a group. Connect the web service and Postgres through a common Docker network, set the database URL for their connection, and test the running API with `curl localhost:3000/tutors/`. Container packaging does not itself settle production concerns such as secrets, security, monitoring, or backups. section 12.5 section 12.0

How Rust Servers, Services, and Apps covers it

How deep it goes
Works through image creation, container execution, persistence, and orchestration; leaves broader production security and monitoring outside the walkthrough.
Examples
Uses a hello-world container, an Actix Web server, and separate API and Postgres containers tested through HTTP requests and database restarts.
What it assumes
Assumes a working Rust web service while demonstrating Docker installation checks and a first image-to-container run.
What you can do after
Build it from scratch with a Dockerfile, then operate and test a web-service-and-database stack with Docker Compose.
How it connects
Connects Docker images, Dockerfiles, multistage builds, Docker Compose, networks, volumes, and Postgres.

Also explained in

Compare how 27 books explain container →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.