Skip to content

Commit

Permalink
update dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed Sep 6, 2024
1 parent 412f026 commit 851ca5c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 50 deletions.
150 changes: 100 additions & 50 deletions .gitpod.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,67 +1,117 @@
FROM gitpod/workspace-postgres:latest
# https://github.com/gitpod-io/workspace-images/blob/main/chunks/tool-postgresql/Dockerfile
# FROM gitpod/workspace-base:latest
from gitpod/workspace-python:latest

# Dazzle does not rebuild a layer until one of its lines are changed. Increase this counter to rebuild this layer.
ENV TRIGGER_REBUILD=4
ENV PGWORKSPACE="/workspace/.pgsql"
ENV PGDATA="$PGWORKSPACE/data"

# Install PostgreSQL
RUN sudo 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://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - && \
echo "deb https://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-18 main" | sudo tee /etc/apt/sources.list.d/llvm.list && \
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - && \
sudo install-packages postgresql-16 postgresql-contrib-16 redis-server

# Setup PostgreSQL server for user gitpod
ENV PATH="/usr/lib/postgresql/16/bin:$PATH"

SHELL ["/usr/bin/bash", "-c"]
RUN PGDATA="${PGDATA//\/workspace/$HOME}" \
&& mkdir -p ~/.pg_ctl/bin ~/.pg_ctl/sockets $PGDATA \
&& initdb -D $PGDATA \
&& printf '#!/bin/bash\npg_ctl -D $PGDATA -l ~/.pg_ctl/log -o "-k ~/.pg_ctl/sockets" start\n' > ~/.pg_ctl/bin/pg_start \
&& printf '#!/bin/bash\npg_ctl -D $PGDATA -l ~/.pg_ctl/log -o "-k ~/.pg_ctl/sockets" stop\n' > ~/.pg_ctl/bin/pg_stop \
&& chmod +x ~/.pg_ctl/bin/*
ENV PATH="$HOME/.pg_ctl/bin:$PATH"
ENV DATABASE_URL="postgresql://gitpod@localhost"
ENV PGHOSTADDR="127.0.0.1"
ENV PGDATABASE="postgres"
COPY --chown=gitpod:gitpod postgresql-hook.bash $HOME/.bashrc.d/200-postgresql-launch

# # RUN pyenv update && pyenv install 3.12.3 && pyenv global 3.12.3
# RUN pyenv install 3.12.3 && pyenv global 3.12.3
# RUN pip install pipenv

SHELL ["/bin/bash", "-c"]
USER gitpod

RUN sudo 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 | sudo apt-key add -
RUN if ! grep -q "export PIP_USER=no" "$HOME/.bashrc"; then printf '%s\n' "export PIP_USER=no" >> "$HOME/.bashrc"; fi
RUN echo "" >> $HOME/.bashrc
RUN echo "unset DATABASE_URL" >> $HOME/.bashrc
RUN echo "export DATABASE_URL" >> $HOME/.bashrc

# Add LLVM repository and key
RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - \
&& echo "deb https://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main" | sudo tee /etc/apt/sources.list.d/llvm.list

RUN sudo apt-get update \
&& sudo apt-get install -y redis-server postgresql \
&& sudo apt-get clean \
&& sudo rm -rf /var/cache/apt/* /var/lib/apt/lists/* /tmp/*
####

# RUN sudo update-alternatives --set postgresql /usr/lib/postgresql/16/bin/postgres

# RUN pyenv update && pyenv install 3.12.3 && pyenv global 3.12.3
RUN pyenv install 3.12.3 && pyenv global 3.12.3
RUN pip install pipenv
# FROM gitpod/workspace-postgres:latest

# Set up PostgreSQL 16
USER postgres
RUN /usr/lib/postgresql/16/bin/initdb -D /var/lib/postgresql/16/data
RUN echo "host all all all md5" >> /var/lib/postgresql/16/data/pg_hba.conf
RUN echo "listen_addresses='*'" >> /var/lib/postgresql/16/data/postgresql.conf
RUN /usr/lib/postgresql/16/bin/pg_ctl -D /var/lib/postgresql/16/data start && \
/usr/lib/postgresql/16/bin/psql -c "CREATE USER gitpod WITH PASSWORD 'gitpod';" && \
/usr/lib/postgresql/16/bin/psql -c "ALTER USER gitpod WITH SUPERUSER;" && \
/usr/lib/postgresql/16/bin/psql -c "CREATE DATABASE gitpod OWNER gitpod;" && \
/usr/lib/postgresql/16/bin/pg_ctl -D /var/lib/postgresql/16/data stop
# SHELL ["/bin/bash", "-c"]

USER root
# RUN sudo 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 | sudo apt-key add -

# remove postgresql 12
RUN rm /usr/lib/postgresql/12 -rf
# # Add LLVM repository and key
# RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - \
# && echo "deb https://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main" | sudo tee /etc/apt/sources.list.d/llvm.list

# backup postgresql 16 settings
# RUN mkdir /tmp/databk
# RUN cp /workspace/.pgsql/data/postgresql.conf /tmp/databk/postgresql.conf
# RUN cp /workspace/.pgsql/data/pg_hba.conf /tmp/databk/pg_hba.conf
# # RUN sudo apt-get update \
# # && sudo apt-get install -y redis-server postgresql \
# # && sudo apt-get clean \
# # && sudo rm -rf /var/cache/apt/* /var/lib/apt/lists/* /tmp/*

# create new data folder
RUN rm /workspace/.pgsql/data -rf
USER gitpod
RUN /usr/lib/postgresql/16/bin/initdb -D /workspace/.pgsql/data
# RUN sudo apt-get remove -y postgresql-12 && sudo apt-get autoremove -y
# RUN sudo install-packages redis-server postgresql postgresql-contrib

USER root
# # RUN sudo update-alternatives --set postgresql /usr/lib/postgresql/16/bin/postgres

# restore postgresql 16 settings
# RUN cp /tmp/databk/postgresql.conf /workspace/.pgsql/data/postgresql.conf
# RUN cp /tmp/databk/databk/pg_hba.conf /workspace/.pgsql/data/pg_hba.conf
# # RUN pyenv update && pyenv install 3.12.3 && pyenv global 3.12.3
# RUN pyenv install 3.12.3 && pyenv global 3.12.3
# RUN pip install pipenv

# remove postgresql 16 backup folder
# RUn rm /workspace/.pgsql/datapk -rf
# # Set up PostgreSQL 16
# USER postgres
# RUN /usr/lib/postgresql/16/bin/initdb -D /var/lib/postgresql/16/data
# RUN echo "host all all all md5" >> /var/lib/postgresql/16/data/pg_hba.conf
# RUN echo "listen_addresses='*'" >> /var/lib/postgresql/16/data/postgresql.conf
# RUN /usr/lib/postgresql/16/bin/pg_ctl -D /var/lib/postgresql/16/data start && \
# /usr/lib/postgresql/16/bin/psql -c "CREATE USER gitpod WITH PASSWORD 'gitpod';" && \
# /usr/lib/postgresql/16/bin/psql -c "ALTER USER gitpod WITH SUPERUSER;" && \
# /usr/lib/postgresql/16/bin/psql -c "CREATE DATABASE gitpod OWNER gitpod;" && \
# /usr/lib/postgresql/16/bin/pg_ctl -D /var/lib/postgresql/16/data stop

# remove PIP_USER environment
USER gitpod
# USER root

# Ensure PostgreSQL 16 is used
RUN echo "export PATH=/usr/lib/postgresql/16/bin:$PATH" >> /home/gitpod/.bashrc
# # remove postgresql 12
# RUN rm /usr/lib/postgresql/12 -rf

RUN if ! grep -q "export PIP_USER=no" "$HOME/.bashrc"; then printf '%s\n' "export PIP_USER=no" >> "$HOME/.bashrc"; fi
RUN echo "" >> $HOME/.bashrc
RUN echo "unset DATABASE_URL" >> $HOME/.bashrc
RUN echo "export DATABASE_URL" >> $HOME/.bashrc
# # backup postgresql 16 settings
# # RUN mkdir /tmp/databk
# # RUN cp /workspace/.pgsql/data/postgresql.conf /tmp/databk/postgresql.conf
# # RUN cp /workspace/.pgsql/data/pg_hba.conf /tmp/databk/pg_hba.conf

# # create new data folder
# RUN rm /workspace/.pgsql/data -rf
# USER gitpod
# RUN /usr/lib/postgresql/16/bin/initdb -D /workspace/.pgsql/data

# USER root

# # restore postgresql 16 settings
# # RUN cp /tmp/databk/postgresql.conf /workspace/.pgsql/data/postgresql.conf
# # RUN cp /tmp/databk/databk/pg_hba.conf /workspace/.pgsql/data/pg_hba.conf

# # remove postgresql 16 backup folder
# # RUn rm /workspace/.pgsql/datapk -rf

# # remove PIP_USER environment
# USER gitpod

# # Ensure PostgreSQL 16 is used
# RUN echo "export PATH=/usr/lib/postgresql/16/bin:$PATH" >> /home/gitpod/.bashrc

# RUN if ! grep -q "export PIP_USER=no" "$HOME/.bashrc"; then printf '%s\n' "export PIP_USER=no" >> "$HOME/.bashrc"; fi
# RUN echo "" >> $HOME/.bashrc
# RUN echo "unset DATABASE_URL" >> $HOME/.bashrc
# RUN echo "export DATABASE_URL" >> $HOME/.bashrc
27 changes: 27 additions & 0 deletions postgresql-hook.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
# Auto-start PostgreSQL server
(
if mkdir /tmp/.pgsql_lock 2>/dev/null; then {
target="${PGWORKSPACE}"
source="${target//\/workspace/$HOME}"

if test -e "$source"; then {

if test ! -e "$target"; then {
mv "$source" "$target"
}; fi

if ! [[ "$(pg_ctl status)" =~ PID ]]; then {
printf 'INFO: %s\n' "Executing command: pg_start"
pg_start
trap "pg_stop" TERM EXIT
exec {sfd}<> <(:)
printf 'INFO: %s\n' \
"Please create another terminal" \
"this one is monitoring postgres server for gracefully shutting down when needed"
until read -r -t 3600 -u $sfd; do continue; done
}; fi

}; fi
}; fi &
)

0 comments on commit 851ca5c

Please sign in to comment.