Skip to main content
The moon docker command provides a suite of sub-commands for working with Docker in your moon workspace. These commands help you generate optimized Dockerfiles, scaffold repository skeletons for efficient layer caching, and manage dependencies within Docker containers. :::info View the official Docker usage guide for a comprehensive guide on using Docker with moon. :::

Sub-commands

moon docker file

Generate a multi-staged Dockerfile for a project that takes full advantage of Docker’s layer caching. This is primarily for production deploys.
$ moon docker file <project>

# Generate with default options
$ moon docker file api --defaults

# Specify build and start tasks
$ moon docker file api --build-task build --start-task start

# Use a custom Docker image
$ moon docker file api --image node:20-alpine
The generated Dockerfile uses a multi-stage approach:
  • base - Installs moon for the chosen Docker image (requires Bash)
  • skeleton - Scaffolds workspace and sources using moon docker scaffold
  • build - Copies sources, installs toolchain via moon docker setup, builds the project, and optionally prunes using moon docker prune
  • start - Runs the project (typically starting a server or executing a binary)

Arguments

  • <id> - Project ID or alias to create a Dockerfile for
  • [dest] - Destination path, relative from the project root. Defaults to Dockerfile

Options

  • --defaults - Use default options instead of prompting
  • --build-task - ID of a task to build the project
  • --image - Base Docker image to use
  • --no-prune - Do not prune dependencies in the build stage
  • --no-setup - Do not setup dependencies in the build stage
  • --no-toolchain - Do not use the toolchain and instead use system binaries
  • --start-task - ID of a task to run the project
  • --template - Template path, relative from workspace root, to render the Dockerfile with

moon docker scaffold

Create multiple repository skeletons for use within Dockerfiles to effectively take advantage of Docker’s layer caching. Uses the project graph to copy only critical files like manifests, lockfiles, and configuration.
# Scaffold a skeleton to .moon/docker
$ moon docker scaffold api

# Scaffold multiple projects
$ moon docker scaffold api web mobile
This command generates 2 skeleton structures in .moon/docker (be sure to gitignore this):
  • configs - Mirrors the project folder structure 1:1, copying only files required for dependencies to install (manifests, lockfiles, configs, .moon directory)
  • sources - Contains source files of the specified project(s) and all their dependencies

Arguments

  • <...ids> - List of project IDs to copy sources for
:::warning Because scaffolding uses the project graph, it requires all projects to be configured in moon. Otherwise, moon will fail to copy all required files and builds may fail. :::

moon docker setup

Setup a Dockerfile by installing toolchains and dependencies for necessary projects. This command reads from a dockerManifest.json file created by moon docker scaffold.
$ moon docker setup
This command:
  1. Reads the dockerManifest.json from the workspace root
  2. Installs toolchains for all focused projects
  3. Installs dependencies for all focused projects
:::info This command is typically used within a Dockerfile in the build stage. :::

moon docker prune

Remove extraneous dependencies within a Dockerfile to reduce the final image size. This removes development dependencies and other artifacts not needed for production.
$ moon docker prune
This command:
  1. Reads the dockerManifest.json from the workspace root
  2. Locates dependency workspaces for focused projects
  3. Prunes development dependencies
  4. Optionally reinstalls only production dependencies
:::info This command is typically used within a Dockerfile in the build stage after building the project. :::

Configuration

Example Dockerfile

Here’s an example of a generated multi-stage Dockerfile:
# Base stage - install moon
FROM node:20-alpine AS base
RUN apk add --no-cache bash curl
RUN curl -fsSL https://moonrepo.dev/install/moon.sh | bash

# Skeleton stage - scaffold repository
FROM base AS skeleton
WORKDIR /app
COPY .moon/docker/configs .
RUN moon docker setup

# Build stage - build the project
FROM skeleton AS build
COPY .moon/docker/sources .
RUN moon run api:build
RUN moon docker prune

# Start stage - run the project
FROM base AS start
WORKDIR /app
COPY --from=build /app .
CMD moon run api:start

Build docs developers (and LLMs) love