Deploying a Web Service
A backend web service requires server side compute resources to execute business and other logic on the backend.
Some popular technologies for building backend services are:
- Python
- Golang
- Java
- Rust
- NodeJS
- Ruby on Rails
Easily deploy a backend API or a Web service with one click.
Steps
-
Click on the
Web servicebutton in the dropdown to create a deployment.
-
Choose Build and deploy from a Git repository and click Next. (Deploy an existing Docker image from a registry deploys a prebuilt image instead.)
-
Select a repository you want to deploy.
-
Input details for the web service deployment and deploy the web service
Fill in Name, Branch, Root directory, Dockerfile Path, Docker build arguments, Build secrets, Port number, Health check path, Pre-deploy command, CPU and memory and Environment, then click Deploy web service. Deployment.io builds the service from the repository's Dockerfile (at the root unless you set Dockerfile Path).
Build secrets
Build secrets are tokens and keys your Dockerfile needs at build time — for example a private npm registry token or a GitHub token for private dependencies. A build secret is available only to the build steps that ask for it; it isn't stored in the image's layers, history or configuration. Build secrets are for web services and private services built from a repository; services deployed from an image don't have them.
Docker build arguments are for values that aren't secret: they're stored in the image. Put tokens and keys in Build secrets instead.
The Build secrets field is optional. It appears on web services and private services built from a repository, when you create one and on the service's Settings tab, right after Docker build arguments.
Adding one
Click Add build secret and enter a name and a value. Names start with a letter or underscore and use only letters, digits and underscores (for example NPM_TOKEN). A service can have at most 50 build secrets, and each value can be up to 64 KiB.
Values are write-only: they're stored encrypted and never shown again. A saved build secret shows its name and a masked value. To replace the value, click Change and enter a new one, or click Keep current value to leave it as it is. To remove a build secret, click the remove icon next to it.
On the Settings tab, build secrets have their own Save build secrets button, separate from the page's Save; it shows "Build secrets saved". A changed build secret is used from the next deploy on — click Deploy latest commit to use it right away (Restart doesn't rebuild the image, so it doesn't pick up a change). When you create a service, its build secrets are saved with it.
Reading one in your Dockerfile
RUN --mount=type=secret,id=NPM_TOKEN,env=NPM_TOKEN npm ci
The id is the build secret's name. env=NPM_TOKEN makes it an environment variable for that one RUN step only. Without env=…, the value is a file at /run/secrets/NPM_TOKEN for that step. Add required=true (--mount=type=secret,id=NPM_TOKEN,env=NPM_TOKEN,required=true) to make the build fail when the secret isn't set.
A build step can still write the value into the image itself — for example RUN echo "$NPM_TOKEN" > .npmrc leaves it in that layer. Use the value within the same RUN step, and if a tool needs a file, create it and delete it in that same step.
In the build logs
The build lists the build secrets' names ("Building docker image with BuildKit (build secrets: NPM_TOKEN)"), never their values. If a build step prints a value, the logs show <NPM_TOKEN> in its place. For a value with several lines, such as a private key, each line of 8 or more characters is replaced on its own, so it's hidden even when the build prints it line by line. Don't print secrets on purpose, though: this is a safety net, not a guarantee — a shorter line, or a value the build changes before printing it (encoded, escaped or re-wrapped), can get through.
How these builds differ
A service with at least one build secret is built with BuildKit; services without build secrets are built as before. With BuildKit:
- your
.dockerignorefile is applied, so files it lists aren't available to the build (other builds don't apply it); - the build logs look different (numbered steps such as
#5 [build 3/8] RUN …); - builds can take longer.
Each service has its own build cache, so one service's build never reuses another service's steps — including steps that used that service's build secrets. After you change a build secret, the next deploy builds without cached steps and the logs say "Build secrets changed since this service's last build on this runner: building without the BuildKit cache". (The build cache only lasts while the runner is running, so a later deploy may also build from scratch.)
Previews
Task previews never get these build secrets. A preview whose Dockerfile reads a build secret gets the service's own preview build secrets, set on its Previews tab — see Preview build secrets.
Pre-deploy command
The Pre-deploy command field is optional. It appears on web services and private services, when you create one and on the service's Settings tab. Its placeholder is "ex. npx prisma migrate deploy".
The command runs once on each deploy of new code, before the new version receives traffic. Use it for steps like database migrations — for example npx prisma migrate deploy, bin/rails db:migrate or python manage.py migrate. It runs on manual deploys, on automatic deploys on git push, and on a Task's deploy after merge.
It doesn't run on Restart or on a rollback — rolling back to an earlier build (Rollback to this build on the Builds tab), or Roll back production on a Task.
How it runs
The command runs as a one-off task in the new version's image, with the same environment variables, CPU and memory, network and AWS permissions as the service.
It runs with sh -c in place of the image's entrypoint and command, so setup done in an entrypoint script doesn't run first. The image needs a shell: an image without sh — for example one built FROM scratch, or a distroless image — can't run it.
The command has 30 minutes to finish.
A pre-deploy command runs as a one-off task, outside the names services in an environment use to call each other. So it can't use the address a service's Domains tab shows as "Your services in the same environment can communicate internally with this service on: …". To reach a private service in the same environment — for example a database you deploy as a private service — use the address on that private service's Domains tab under "Pre-deploy commands and one-off tasks can reach it on: …". The line appears once the private service has been deployed. If a private service doesn't show it, redeploy it.
Logs
The command's output appears in the build logs, after the line "Running the pre-deploy command". When the command ran to the end, it is followed by "Pre-deploy command finished (exit code <n>) in <duration>".
Deployment.io doesn't write the command itself to the logs; whatever the command prints is shown as it is.
When it fails
If the command exits with anything but 0, the deploy stops. The error is one of:
- "The pre-deploy command failed with exit code <n>."
- "The pre-deploy command couldn't start: <reason>."
- "The pre-deploy command couldn't run: <reason>."
- "The pre-deploy command didn't finish within 30 minutes and was stopped."
It is followed by "The deploy was stopped; the previous version keeps running." when the service was already running, or "Nothing was deployed." on its first deploy.
Changing or removing it
The command can be at most 1000 characters. To remove it, clear the field.