Skip to content

Commit

Permalink
Add docker support (#8)
Browse files Browse the repository at this point in the history
Update readme

add Dockerfile

add scripts to build and run the docker based development shell.
  • Loading branch information
urso authored Feb 22, 2024
1 parent 9d6f335 commit 7e8d914
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
37 changes: 23 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,32 +145,41 @@ pgzx is currently under heavy development by the [Xata](https://xata.io) team. I

### Develpment shell and local installation

We use Nix to provide a local development shell. But you should be able to run
the following scripts without the development shell.
We use Nix to provide a local development shell.
This ensures that we have a stable environment with all dependencies available
in the expected versions. This is especially important with Zig, which is still
in development.

For this purpose it is possible do use this project as input in downstream
flake files as well.

The tools we use also require some environment variables set, which are already
pre-configured in the develpment shell.

We would recommend the [nix-installer from DeterminateSystems](https://github.com/DeterminateSystems/nix-installer). The
installer enables Nix Flakes (used by this project) out of the box and also
provides an uninstaller.

TODO: test the scripts without development shell

TODO: support to create develpment shell in docker container

Still, we would recommend to use the development shell as we already provide
all compatible dependencies that you will need for developing within this
repository.
The tools we use also require some environment variables set, which are already
pre-configured in the develpment shell.
If you want to try out the project without having to install Nix on your
system, you can do so using Docker. You can build the docker image by running
the `dev/docker/build.sh` script. The docker image is names `pgzx:latest`.

To enter the develpment shell run:

```
$ nix develop
```

If you want to use the docker instead, run:

```
$ ./dev/docker/run.sh
```


NOTE:
We also provide an `.envrc` file to automatically enter the development shell when entering
the projects folder.
the projects folder. If you use direnv you can enable the environment via `direnv allow`.

The nix configuration already installs PostgresSQL, but for testing we want to
have a local postgres installation where we can install our test extensions in.
Expand All @@ -187,14 +196,14 @@ $ ls out

The `out/default` folder is a symlink to the postgres installation currently in use.

Having a local installation we want to create a local database and user that we can run:
Having a local installation we want to create a local database and user:

```
$ pginit
...
```

This did create a local database names and `postgres` user. The script allows us to configure an alternative name for the cluster, database or user. This allows us to create multiple clusters within our current installation.
This did create a local database named `postgres`. The script allows us to configure an alternative name for the cluster, database or user. This allows us to create multiple clusters within our current installation.

We can start and stop the database using `pgstart` and `pgstop`. Let's test our current setup:

Expand Down
41 changes: 41 additions & 0 deletions dev/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
FROM ubuntu:latest
RUN apt update -y
RUN apt install curl git sudo xz-utils -y

# Create a non-root user
ARG USERNAME=dev
ARG USER_UID=1000
ARG USER_GID=$USER_UID

RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME \
&& mkdir /run/postgresql \
&& chown -R $USER_UID:$USER_GID /run/postgresql

# Install Nix

RUN mkdir -p /etc/nix && \
mkdir -p /nix && chown $USERNAME /nix && \
echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf

USER $USERNAME

RUN curl -L https://nixos.org/nix/install -o /nix/install-nix && \
chmod a+x /nix/install-nix && \
/nix/install-nix --no-daemon --no-channel-add && \
rm /nix/install-nix

ENV PATH="$PATH:/home/dev/.nix-profile/bin:/home/dev/.nix-profile/bin/nix"

# Prebuild development environment

RUN mkdir -p /home/dev/workdir && chown -R $USERNAME /home/dev/workdir
WORKDIR /home/dev/workdir
COPY *.nix .
COPY *.lock .
COPY nix nix
RUN nix develop -c true

ENTRYPOINT ["nix", "develop"]
11 changes: 11 additions & 0 deletions dev/docker/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -e
set -x

SCRIPT_DIR=$(cd $(dirname $0); pwd)
PRJ_ROOT=${PRJ_ROOT:-$(realpath $SCRIPT_DIR/../..)}
DOCKERFILE=${DOCKERFILE:-$PRJ_ROOT/dev/docker/Dockerfile}

cd $PRJ_ROOT
docker build -t pgzx:latest -f $DOCKERFILE .
13 changes: 13 additions & 0 deletions dev/docker/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -e
set -x

SCRIPT_DIR=$(cd $(dirname $0); pwd)
PRJ_ROOT=${PRJ_ROOT:-$(realpath $SCRIPT_DIR/../..)}

docker run -i -t --rm \
-v $PRJ_ROOT:/home/dev/workdir \
-w /home/dev/workdir \
pgzx:latest \
$@

0 comments on commit 7e8d914

Please sign in to comment.