Skip to main content
You can use Docker to create an image of your Alchemy app that will be deployable anywhere Docker is installed.

Create a Dockerfile

You’ll need a Dockerfile to deploy with Docker. This is a file that tells Docker how to build & run an image with your server. If you created an Alchemy app with the installer script, a Dockerfile is already included in your project. If you didn’t, here’s one that you can use.
# ================================
# Build image (Ubuntu 22.04)
# ================================
FROM swift:5.8-jammy as build

# Update system packages
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
    && apt-get -q update \
    && apt-get -q dist-upgrade -y \
    && rm -rf /var/lib/apt/lists/*

# Set up a build area
WORKDIR /build

# Resolve dependencies, creating a cached layer
COPY ./Package.* ./
RUN swift package resolve

# Copy entire project into container
COPY . .

# Build everything, with optimizations
RUN swift build -c release --static-swift-stdlib

# Set up a staging area
WORKDIR /staging

# Copy the executable
RUN mv `swift build --package-path /build -c release --show-bin-path`/app ./

# Copy Public directory, if it exists. Ensure it's contents aren't writable.
RUN [ -d /build/Public ] && { mv /build/Public /staging/Public && chmod -R a-w /staging/Public; } || true

# ================================
# Run image
# ================================
FROM ubuntu:jammy

# Update system packages and install necessary packages
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
    && apt-get -q update \
    && apt-get -q dist-upgrade -y \
    && apt-get -q install -y ca-certificates tzdata libsqlite3-dev \
    && rm -r /var/lib/apt/lists/*

# Switch to a new home directory
WORKDIR /app

# Copy over needed files
COPY --from=build /staging /app

# Start the Alchemy app on port 3000
EXPOSE 3000
ENTRYPOINT [ "./app" ]
CMD ["serve", "--host", "0.0.0.0", "--port", "3000"]

Build an Image

Once that’s done, build an image of your app. This may take a moment.
docker build -t my-app:latest .

Run a Container

Finally, run the built image in a container. The -d flag tells Docker to run your container in the background and -p 3000:3000 tells it to bind it to localhost:3000.
docker run -d -p 3000:3000 my-app:latest
Visit http://localhost:3000 in the browser and you’ll see your app running. Nicely done!

Docker Compose

Docker Compose helps you start multiple containers at once defined in a docker-compose.yml file. Again, if you created an Alchemy app with the installer script a docker-compose.yml are already included in your project. If you didn’t, here’s a simple one that you can use. It runs three services based on your project - an app that serves to localhost:3000, a scheduler, and a queue worker.
services:
  app:
    container_name: app
    image: alchemy-app:latest
    build:
      context: .
    ports:
      - '3000:3000'
    command: [ "serve", "--host", "0.0.0.0", "--port", "3000" ]
  worker:
    container_name: worker
    image: alchemy-app:latest
    command: [ "queue:work", "--workers", "1" ]
  scheduler:
    container_name: scheduler
    image: alchemy-app:latest
    command: [ "schedule" ]
Once you’ve created a docker-compose.yml file, you can easily start all three of these services in separate docker containers. Like before, the -d flag runs it in the background.
docker compose up -d
Shutting all three of them down is just as simple.
docker compose down
I