Docs / Containers & Docker / Docker Init: Auto-Generate Dockerfiles

Docker Init: Auto-Generate Dockerfiles

By Admin · Mar 15, 2026 · Updated Apr 23, 2026 · 402 views · 3 min read

Docker Init (docker init) is an official Docker CLI plugin that automatically generates Dockerfiles, Compose files, and .dockerignore for your projects. It detects your project's language and framework, then creates production-ready container configurations with best practices built in. This guide covers using Docker Init for various project types.

Installation

# Docker Init is included with Docker Desktop 4.18+
# For Docker Engine on Linux, install the init plugin
docker init --version

# If not available, update Docker to the latest version
sudo apt update && sudo apt upgrade docker-ce docker-ce-cli

Basic Usage

# Navigate to your project directory
cd /path/to/your/project

# Run docker init
docker init

# Interactive prompts:
# ? What application platform does your project use? [Go, Node, Python, Rust, ASP.NET, PHP, Java, Other]
# ? What version of Go/Node/Python? [detected version]
# ? What port does your server listen on? [detected or 8080]
# ? What is the command to run your app? [detected]

Generated Files

Node.js Project

# Generated Dockerfile
# syntax=docker/dockerfile:1
ARG NODE_VERSION=20.11.0

FROM node:${NODE_VERSION}-alpine as base
WORKDIR /usr/src/app
EXPOSE 3000

FROM base as deps
RUN --mount=type=bind,source=package.json,target=package.json \
    --mount=type=bind,source=package-lock.json,target=package-lock.json \
    --mount=type=cache,target=/root/.npm \
    npm ci --omit=dev

FROM base as build
RUN --mount=type=bind,source=package.json,target=package.json \
    --mount=type=bind,source=package-lock.json,target=package-lock.json \
    --mount=type=cache,target=/root/.npm \
    npm ci
COPY . .
RUN npm run build

FROM base as final
ENV NODE_ENV=production
USER node
COPY package.json .
COPY --from=deps /usr/src/app/node_modules ./node_modules
COPY --from=build /usr/src/app/dist ./dist
CMD ["node", "dist/index.js"]

Python Project

# Generated Dockerfile for Python
ARG PYTHON_VERSION=3.12.0
FROM python:${PYTHON_VERSION}-slim as base
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /app

ARG UID=10001
RUN adduser --disabled-password --gecos "" --home "/nonexistent" \
    --shell "/sbin/nologin" --no-create-home --uid "${UID}" appuser

RUN --mount=type=cache,target=/root/.cache/pip \
    --mount=type=bind,source=requirements.txt,target=requirements.txt \
    pip install -r requirements.txt

USER appuser
COPY . .
EXPOSE 8000
CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:8000"]

Generated Docker Compose

# Generated compose.yaml
services:
  server:
    build:
      context: .
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: production
    develop:
      watch:
        - action: rebuild
          path: ./package.json
        - action: sync
          path: ./src
          target: /usr/src/app/src

Customizing Generated Files

# After generation, customize for your needs:
# 1. Add environment variables
# 2. Add health checks
# 3. Add database services to compose.yaml
# 4. Adjust build caching strategy
# 5. Add security scanning stages

Supported Platforms

  • Go, Node.js, Python, Rust, PHP, Java, ASP.NET, Ruby
  • Auto-detects version from go.mod, package.json, requirements.txt, Cargo.toml, etc.
  • "Other" option for custom Dockerfiles with manual configuration

Best Practices

  • Use docker init as a starting point — always review and customize the generated files
  • The generated Dockerfiles follow best practices: multi-stage builds, non-root users, cache mounts
  • Add HEALTHCHECK instructions for production deployments
  • Review the generated .dockerignore and add project-specific exclusions
  • Commit all generated files to version control

Was this article helpful?