new-dockerfile
is a CLI tool and Go package automatically generates a configurable Dockerfile
based on your project source code. It supports a wide range of languages and frameworks, including Next.js,
Node.js, Python, Ruby, Java/Spring Boot, Go, Elixir/Phoenix, and more.
See the FlexStack Documentation page for FlexStack-specific documentation related to this tool.
- Automatically detect the runtime and framework used by your project
- Use version managers like asdf, nvm, rbenv, and pyenv to install the correct version of the runtime
- Make a best effort to detect any install, build, and start commands
- Generate a Dockerfile with sensible defaults that are configurable via Docker Build Args
- Support for a wide range of the most popular languages and frameworks including Next.js, Phoenix, Spring Boot, Django, and more
- Use Debian Slim as the runtime image for a smaller image size and better security, while still supporting the most common dependencies and avoiding deployment headaches caused by Alpine Linux gotchas
- Includes
wget
in the runtime image for adding health checks to services, e.g.wget -nv -t1 --spider 'http://localhost:8080/healthz' || exit 1
- Includes
ca-certificates
in the runtime image to allow secure HTTPS connections - Use multi-stage builds to reduce the size of the final image
- Run the application as a non-root user for better security
- Supports multi-platform images that run on both x86 and ARM CPU architectures
- C#/.NET
- C++
- Scala
- Zig
Consider contributing to add support for these or any other runtimes!
curl -fsSL https://flexstack.com/install/new-dockerfile | bash
go get github.com/flexstack/new-dockerfile
new-dockerfile [options]
--path
- Path to the project source code (default:.
)--write
- Write the generated Dockerfile to the project at the specified path (default:false
)--runtime
- Force a specific runtime, e.g.node
(default:auto
)--quiet
- Disable all logging except for errors (default:false
)--help
- Show help
Print the generated Dockerfile to the console:
new-dockerfile
Write a Dockerfile to the current directory:
new-dockerfile --write
Write a Dockerfile to a specific directory:
new-dockerfile > path/to/Dockerfile
Force a specific runtime:
new-dockerfile --runtime next.js
List the supported runtimes:
new-dockerfile --runtime list
In the CI use case, you might need a very common step for generating a Dockerfile
. You can create a config file for the
CLI options. The default config file name is new-dockerfile.yaml
, and it should be in the root directory of your git
repository. Especially, there are multiple kinds of files, new-dockerfile
might not be able to it a correct one.
runtime: go
And, the CLI option will overwrite the values from config file.
The tool searches for common files and directories in your project to determine the runtime and framework.
For example, if it finds a package.json
file, it will assume the project is a Node.js project unless
a next.config.js
file is present, in which case it will assume the project is a Next.js project.
From there, it will read any .tool-versions
or other version manager files to determine the correct version
of the runtime to install. It will then make a best effort to detect any install, build, and start commands.
For example, a serve
, start
, start:prod
command in a package.json
file will be used as the start command.
Runtimes are matched against in the order they appear when you run new-dockerfile --runtime list
.
Read on to see runtime-specific examples and how to configure the generated Dockerfile.
Bun is a fast JavaScript all-in-one toolkit
bun.lockb
bun.lock
bunfig.toml
.tool-versions
-bun {VERSION}
.mise.toml
-bun = "{VERSION}"
oven/bun:${VERSION}-slim
VERSION
- The version of Bun to install (default:1
)INSTALL_CMD
- The command to install dependencies (default:bun install
)BUILD_CMD
- The command to build the project (default: detected frompackage.json
)START_CMD
- The command to start the project (default: detected frompackage.json
)
Detected in order of precedence:
package.json
scripts:"build:prod", "build:production", "build-prod", "build-production", "build"
Detected in order of precedence:
package.json
scripts:"serve", "start:prod", "start:production", "start-prod", "start-production", "preview", "start"
package.json
main/module file:bun run ${mainFile}
Deno is a secure runtime for JavaScript with native TypeScript and JSX support
deno.jsonc
deno.json
deno.lock
deps.ts
mod.ts
.tool-versions
-deno {VERSION}
.mise.toml
-deno = "{VERSION}"
debian:stable-slim
VERSION
- The version of Deno to install (default:latest
)INSTALL_CMD
- The command to install dependencies (default: detected fromdeno.jsonc
and source code)START_CMD
- The command to start the project (default: detected fromdeno.jsonc
and source code)
Detected in order of precedence:
deno.jsonc
tasks:"cache"
- Main/module file:
deno cache ["mod.ts", "src/mod.ts", "main.ts", "src/main.ts", "index.ts", "src/index.ts]"
Detected in order of precedence:
deno.jsonc
tasks:"serve", "start:prod", "start:production", "start-prod", "start-production", "preview", "start"
- Main/module file:
deno run ["mod.ts", "src/mod.ts", "main.ts", "src/main.ts", "index.ts", "src/index.ts]"
Elixir is a dynamic, functional language designed for building scalable and maintainable applications.
mix.exs
.tool-versions
-elixir {VERSION}
.tool-versions
-erlang {VERSION}
.elixir-version
-{VERSION}
.erlang-version
-{VERSION}
.mise.toml
-erlang = "{VERSION}"
debian:stable-slim
VERSION
- The version of Elixir to install (default:1.12
)OTP_VERSION
- The version of Erlang to install (default:26.2.5
)BIN_NAME
- The name of the release binary (default: detected via app name inmix.exs
)
/app/bin/{BIN_NAME} start
Go is an open-source programming language that makes it easy to build simple, reliable, and efficient software.
go.mod
main.go
.tool-versions
-golang {VERSION}
.mise.toml
-go = "{VERSION}"
go.mod
-go {VERSION}
debian:stable-slim
VERSION
- The version of Go to install (default:1.17
)TARGETOS
- The target OS for the build (default:linux
)TARGETARCH
- The target architecture for the build (default:amd64
)CGO_ENABLED
- Enable CGO for the build (default:0
)GOPROXY
- The Go module proxy to use (default:direct
)PACKAGE
- The package to compile e.g../cmd/http
(default: detected viacmd
directory ormain.go
)
- Find the directory in
cmd
with a.go
file main.go
file in the root directory
if [ -f go.mod ]; then go mod download; fi
CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -trimpath -ldflags="-s -w" -o /go/bin/app "${PACKAGE}"
["/app/app"]
Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.
pom.{xml,atom,clj,groovy,rb,scala,yml,yaml}
JDK version:
.tool-versions
-java {VERSION}
Maven version:.tool-versions
-maven {VERSION}
eclipse-temurin:${VERSION}-jdk
VERSION
- The version of the JDK to install (default:17
)MAVEN_VERSION
- The version of Maven to install (default:3
)JAVA_OPTS
- The Java options to pass to the JVM (default:-Xmx512m -Xms256m
)BUILD_CMD
- The command to build the project (default: best guess via source code)START_CMD
- The command to start the project (default: detected via source code)
- If Maven:
mvn install
- If Maven:
mvn -DoutputFile=target/mvn-dependency-list.log -B -DskipTests clean dependency:list install
- Default:
java $JAVA_OPTS -jar target/*jar
- If Spring Boot:
java -Dserver.port=${PORT} $JAVA_OPTS -jar target/*jar
Next.js is a React framework that enables functionality such as server-side rendering and generating static websites.
next.config.{js,mjs,cjs,ts,mts}
next-env.d.ts
.next/
.tool-versions
-nodejs {VERSION}
.nvmrc
-v{VERSION}
.node-version
-v{VERSION}
.mise.toml
-node = "{VERSION}"
package.json
-"engines": {"node": "{VERSION}"}
node:${VERSION}-slim
VERSION
- The version of Node.js to install (default:lts
)
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f bun.lockb ]; then npm i -g bun && bun install; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f bun.lockb ]; then npm i -g bun && bun run build; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi
- If
"output" :"standalone"
innext.config.js
:HOSTNAME="0.0.0.0" node server.js
- Otherwise
["node_modules/.bin/next", "start", "-H", "0.0.0.0"]
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.
yarn.lock
package-lock.json
pnpm-lock.yaml
.tool-versions
-nodejs {VERSION}
.nvmrc
-v{VERSION}
.node-version
-v{VERSION}
node:${VERSION}-slim
VERSION
- The version of Node.js to install (default:lts
)INSTALL_CMD
- The command to install dependencies (default: detected from source code)BUILD_CMD
- The command to build the project (default: detected from source code)START_CMD
- The command to start the project (default: detected from source code)
- If Yarn:
yarn --frozen-lockfile
- If npm:
npm ci
- If pnpm:
corepack enable pnpm && pnpm i --frozen-lockfile
In order of precedence:
package.json
scripts:"build:prod", "build:production", "build-prod", "build-production", "build"
In order of precedence:
package.json
scripts:"serve", "start:prod", "start:production", "start-prod", "start-production", "preview", "start"
package.json
scripts search for regex matching:^.*?\b(ts-)?node(mon)?\b.*?(index|main|server|client)\.([cm]?[tj]s)\b
package.json
main/module file:node ${mainFile}
PHP is a popular general-purpose scripting language that is especially suited to web development.
composer.json
index.php
.tool-versions
-php {VERSION}
composer.json
-"php": "{VERSION}"
php:${VERSION}-apache
VERSION
- The version of PHP to install (default:8.3
)INSTALL_CMD
- The command to install dependencies (default: detected via source code)BUILD_CMD
- The command to build the project (default: detected via source code)START_CMD
- The command to start the project (default:apache2-foreground
)
- If Composer:
composer update && composer install --prefer-dist --no-dev --optimize-autoloader --no-interaction
- If
package.json
exists: composer install command + see Node.js install command
- If
package.json
exists: see Node.js build command
apache2-foreground
Python is a high-level, interpreted programming language that is known for its readability and simplicity.
requirements.txt
uv.lock
poetry.lock
Pipefile.lock
pyproject.toml
pdm.lock
main.py
app.py
application.py
app/__init__.py
filepath.Join(filepath.Base(path), "app.py")
filepath.Join(filepath.Base(path), "application.py")
filepath.Join(filepath.Base(path), "main.py")
filepath.Join(filepath.Base(path), "__init__.py")
.tool-versions
-python {VERSION}
.python-version
-{VERSION}
.mise.toml
-python = "{VERSION}"
runtime.txt
-python-{VERSION}
python:${VERSION}-slim
VERSION
- The version of Python to install (default:3.10
)INSTALL_CMD
- The command to install dependencies (default: detected from source code)START_CMD
- The command to start the project (default: detected from source code)
- If Poetry:
pip install poetry && poetry install --no-dev --no-ansi --no-root
- If Pipenv:
pipenv install --dev --system --deploy
- If uv:
pip install uv && uv sync --python-preference=only-system --no-cache --no-dev
- If PDM:
pip install pdm && pdm install --prod
- If
pyproject.toml
exists:pip install --upgrade build setuptools && pip install .
- If
requirements.txt
exists:pip install -r requirements.txt
- If Django is detected:
python manage.py runserver 0.0.0.0:${PORT}
- If FastAPI is detected:
fastapi run [main.py, app.py, application.py, app/main.py, app/application.py, app/__init__.py] --port ${PORT}
- If
pyproject.toml
exists:python -m ${projectName}
- Otherwise:
python [main.py, app.py, application.py, app/main.py, app/application.py, app/__init__.py]
Ruby is a dynamic, open-source programming language with a focus on simplicity and productivity.
Gemfile
Gemfile.lock
config.ru
Rakefile
config/environment.rb
.tool-versions
-ruby {VERSION}
.ruby-version
-{VERSION}
.mise.toml
-ruby = "{VERSION}"
Gemfile
-ruby '{VERSION}'
ruby:${VERSION}-slim
VERSION
- The version of Ruby to install (default:3.0
)INSTALL_CMD
- The command to install dependencies (default: detected from source code)BUILD_CMD
- The command to build the project (default: detected from source code)START_CMD
- The command to start the project (default: detected from source code)
bundle install
- If
package.json
exists:bundle install && [package manager install command]
- If Rails:
bundle exec rake assets:precompile
- If Rails:
bundle exec rails server -b 0.0.0.0 -p ${PORT}
- If
config.ru
exists:bundle exec rackup config.ru -p ${PORT}
- If
config/environment.rb
exists:bundle exec rails server -b
- If
Rakefile
exists:bundle exec rake
Rust is a systems programming language that is known for its speed, memory safety, and parallelism.
Cargo.toml
debian:stable-slim
TARGETOS
- The target OS for the build (default:linux
)TARGETARCH
- The target architecture for the build (default:amd64
)BIN_NAME
- The name of the release binary (default: detected viaCargo.toml
)
if [ "${TARGETARCH}" = "amd64" ]; then rustup target add x86_64-unknown-linux-gnu; else rustup target add aarch64-unknown-linux-gnu; fi
if [ "${TARGETARCH}" = "amd64" ]; then cargo zigbuild --release --target x86_64-unknown-linux-gnu; else cargo zigbuild --release --target aarch64-unknown-linux-gnu; fi
Determined by the binary name in the Cargo.toml
file
["/app/app"]
Static Web Server is a cross-platform, high-performance & asynchronous web server for static files serving. It is nearly as fast as Nginx and Lighttpd, but is easily configurable with environment variables.
public/
static/
dist/
index.html
joseluisq/static-web-server:${VERSION}-debian
VERSION
- The version of the static web server to install (default:2
)SERVER_ROOT
- The root directory of the server (default: detected from source code)
- FlexStack - A platform that simplifies the deployment of containerized applications to AWS. FlexStack uses this tool to automatically detect the runtime and framework used by your project, so you can just bring your code and deploy it with confidence.
- Your project here - If you're using this tool in your project, let us know! We'd love to feature you here.
Read the CONTRIBUTING.md guide to learn how to contribute to this project.
This project is licensed under the MIT License - see the LICENSE file for details.