-
Notifications
You must be signed in to change notification settings - Fork 33
/
Dockerfile.try
144 lines (120 loc) · 5.88 KB
/
Dockerfile.try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Example of how to build and run this Dockerfile:
# docker build -f Dockerfile.try -t tcdi/try-plrust . # Build the container
# docker run -it tcdi/try-plrust # Run the container
FROM postgres:15-bullseye
SHELL ["/bin/bash", "-c"]
# Install just enough to set up the official Postgres debian repository,
# then install everything else needed for Rust and plrust
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections && \
apt-get update && \
apt-get install -y --no-install-recommends \
ca-certificates \
gnupg \
lsb-release \
wget && \
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null && \
apt-get update -y -qq --fix-missing && \
apt-get install -y --no-install-recommends \
build-essential \
clang \
clang-11 \
gcc \
git \
jq \
libssl-dev \
llvm-11 \
make \
ruby \
postgresql-server-dev-15 \
pkg-config && \
rm -rf /var/lib/apt/lists/*
# Set up permissions so that the postgres user can install the plrust plugin
RUN chmod a+rwx `$(which pg_config) --pkglibdir` `$(which pg_config) --sharedir`/extension
# Install fpm for the creation of the .deb file,
# and install toml so TOML files can be parsed later
RUN gem install --no-document fpm toml
# The 'postgres' user is the default user that the official postgres:15-bullseye image sets up
USER postgres
ENV USER postgres
# Copy in plrust source
COPY --chown=${USER} . src/
WORKDIR /src
# Obtain the toolchain version from rust-toolchain.toml and store that into a file.
RUN ruby <<EOF
require 'toml'
toml=TOML.load_file('/src/rust-toolchain.toml')
if ver=toml['toolchain']['channel']
File.open('/tmp/.toolchain-ver', 'w') { |file| file.write(ver) }
else
raise 'Could not determine toolchain channel version. Is rust-toolchain.toml missing or malformed?'
end
EOF
# Install Rust
RUN TOOLCHAIN_VER=$(</tmp/.toolchain-ver) && wget -qO- https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain=$TOOLCHAIN_VER
ENV PATH="/var/lib/postgresql/.cargo/bin:${PATH}"
# Build/install/remove all that is necessary in one step as to keep the resulting layer as small as possible.
RUN PGRX_VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[]|select(.name=="pgrx")|.version') && \
cargo install cargo-pgrx --locked --force --version "$PGRX_VERSION" && \
rustup component add llvm-tools-preview rustc-dev && \
cd /src/plrustc && ./build.sh && cp ../build/bin/plrustc ~/.cargo/bin && \
cargo pgrx init --pg15 $(which pg_config) && \
cd /src/plrust && STD_TARGETS="$(uname -m)-postgres-linux-gnu" ./build && \
cargo pgrx install --release --features trusted && \
cd /src && find . -type d -name target | xargs rm -r && \
rustup component remove llvm-tools-preview rustc-dev
# Reset the permissions of files/directories that was created or touched by the postgres user.
# Switching to the root user here temporarily is easier than installing and setting up sudo
USER root
RUN chmod 0755 `$(which pg_config) --pkglibdir` && \
cd `$(which pg_config) --pkglibdir` && \
chown root:root *.so && \
chmod 0644 *.so && \
chmod 755 `$(which pg_config) --sharedir`/extension && \
cd `$(which pg_config) --sharedir`/extension && \
chown root:root *
USER postgres
# Create initial main database
RUN pg_createcluster 15 main
# NOTE: Heredocs do not work in older versions of Docker, so multiple 'echo' lines
# will be used in various places in this Dockerfile.
# Set up host based auth. *DO NOT* use this in production!
RUN echo 'local all all trust' > /etc/postgresql/15/main/pg_hba.conf && \
echo 'host all all 0.0.0.0/0 trust' >> /etc/postgresql/15/main/pg_hba.conf
# Set up plrust configuration options
RUN echo "shared_preload_libraries='plrust'" >> /etc/postgresql/15/main/postgresql.conf && \
echo "plrust.work_dir='/tmp'" >>/etc/postgresql/15/main/postgresql.conf
# Set up custom prompt
RUN echo "\set PROMPT1 '%/(plrust)=# '" >> ~/.psqlrc && \
echo "\set PROMPT2 '%/(plrust)-# '" >> ~/.psqlrc
# Create startup/CMD scripts
RUN mkdir ~/scripts
ENV PATH="~/scripts:${PATH}"
# Set up script that launches both postgresql server (background) and psql client (foreground)
RUN echo 'echo "Starting Postgresql..."' >> ~/scripts/all && \
echo 'service postgresql start' >> ~/scripts/all && \
echo 'echo ""' >> ~/scripts/all && \
echo 'echo "Installing plrust"' >> ~/scripts/all && \
echo 'psql -c "CREATE EXTENSION IF NOT EXISTS plrust;"' >> ~/scripts/all && \
echo 'echo ""' >> ~/scripts/all && \
echo 'echo "Starting psql"' >> ~/scripts/all && \
echo 'echo ""' >> ~/scripts/all && \
echo 'psql' >> ~/scripts/all
RUN chmod +x ~/scripts/all
# Set up script that launches Postgresql in the foreground. Need to launch it as a service first so that
# plrust can be installed.
RUN echo 'echo "Starting Postgresql service..."' >> ~/scripts/server && \
echo 'service postgresql start' >> ~/scripts/server && \
echo 'echo ""' >> ~/scripts/server && \
echo 'echo "Installing plrust"' >> ~/scripts/server && \
echo 'psql -c "CREATE EXTENSION IF NOT EXISTS plrust;"' >> ~/scripts/server && \
echo 'echo ""' >> ~/scripts/server && \
echo 'echo "Stopping service"' >> ~/scripts/server && \
echo 'service postgresql stop' >> ~/scripts/server && \
echo 'echo "Starting Postgresql in the foreground"' >> ~/scripts/server && \
echo 'echo ""' >> ~/scripts/server && \
echo 'postgres -D /etc/postgresql/15/main/' >> ~/scripts/server
RUN chmod +x ~/scripts/server
# By default, run the "all" script, which will start up the postgres server in the background,
# and the psql cli in the foreground
CMD [ "all" ]