Using a Docker for deployments on Coolify
5 May 2025 at 00:51
I have Coolify building my site and rsync
-ing the build output to a separate LAMP server. There was a recent bug in a release that broke compatibility with Nixpacks that broke my previous deployment configuration. To the Coolify team's credit they pushed out a fix within hours (and noted that Nixpacks often has issues where it may break).
To work around this and make my deploys more reliable, I removed my dependence on Nixpacks and updated my site deployment implementation to be handled by Docker directly.
The full Dockerfile is before and β in brief β it:
- Uses the
Node.js
version 21 container. - Installs system and PHP build dependencies and cleans up the
apt
cache - Sets up the working directory.
- Clones the site source.
- Switches to the app directory the source was cloned to.
- Clears the
npm
cache and installsNode.js
dependencies. - Installs Composer, my PHP dependencies and optimizes said dependencies.
- Builds the application.
- Creates the
~/.ssh
directory in the Docker container, writes the SSH key for the host server to~/.ssh/id_rsa
, sets permissions, adds the destination server to known hosts andrsync
s my build output over. - Finally,
tail -f /dev/null
keeps the container running to satisfy Coolify's health check for the build resource.
This process is repeated hourly to refresh the site by a cron job on the host server that makes a post request to the build webhook for the resource.
FROM node:21
# install system dependencies
RUN apt-get update && apt-get install -y \
git \
openssh-client \
rsync \
curl \
php-cli \
php-mbstring \
php-xml \
jq \
&& rm -rf /var/lib/apt/lists/*
# set working directory
WORKDIR /workdir
# build time args
ARG GIT_REPO
ARG GIT_BRANCH=main
# clone source
RUN git clone --depth 1 --branch ${GIT_BRANCH} ${GIT_REPO} app
# move into app directory
WORKDIR /workdir/app
# build-time env vars
ARG POSTGREST_API_KEY
ARG POSTGREST_URL
# export vars for build staps
ENV POSTGREST_API_KEY=${POSTGREST_API_KEY}
ENV POSTGREST_URL=${POSTGREST_URL}
# clean npm cache
RUN npm cache clean --force
# install node deps
RUN npm install
# install php deps
RUN curl -sS https://getcomposer.org/installer | php && \
mv composer.phar /usr/bin/composer && \
chmod +x /usr/bin/composer
RUN composer install --no-dev --optimize-autoloader
# build
RUN npm run build
# set runtime env vars
ARG SERVER_IP
ENV SERVER_IP=${SERVER_IP}
# deploy and manage container healthcheck
CMD bash -c "\
mkdir -p ~/.ssh && \
echo \"${SSH_PRIVATE_KEY}\" > ~/.ssh/id_rsa && \
chmod 600 ~/.ssh/id_rsa && \
ssh-keyscan -H \"${SERVER_IP}\" >> ~/.ssh/known_hosts && \
rsync -avz --delete dist/ root@\"${SERVER_IP}\":/var/www/coryd.dev/ && \
echo \"β
Deployed successfully\" && \
tail -f /dev/null"