Skip to content

Commit e67811f

Browse files
authored
Merge pull request #2 from shaananc/master
Add Dockerfile
2 parents 57b449b + eabb200 commit e67811f

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

docker/Dockerfile

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
FROM golang:alpine
2+
3+
# Set necessary environmet variables needed for our image
4+
ENV GO111MODULE=on \
5+
CGO_ENABLED=0 \
6+
GOOS=linux \
7+
GOARCH=amd64
8+
9+
10+
11+
ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH
12+
ENV PGDATA /var/lib/postgresql/data
13+
ENV POSTGRES_DB headscale
14+
ENV POSTGRES_USER admin
15+
16+
ENV LANG en_US.utf8
17+
18+
RUN apk update && \
19+
apk add git su-exec tzdata libpq postgresql-client postgresql postgresql-contrib gnupg supervisor inotify-tools wireguard-tools openssh && \
20+
mkdir /docker-entrypoint-initdb.d && \
21+
rm -rf /var/cache/apk/*
22+
23+
RUN gpg --keyserver ipv4.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4
24+
RUN gpg --list-keys --fingerprint --with-colons | sed -E -n -e 's/^fpr:::::::::([0-9A-F]+):$/\1:6:/p' | gpg --import-ownertrust
25+
RUN wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/1.7/gosu-amd64" && \
26+
wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/1.7/gosu-amd64.asc" && \
27+
gpg --verify /usr/local/bin/gosu.asc && \
28+
rm /usr/local/bin/gosu.asc && \
29+
chmod +x /usr/local/bin/gosu
30+
RUN apk --purge del gnupg ca-certificates
31+
32+
VOLUME /var/lib/postgresql/data
33+
34+
35+
36+
37+
RUN rm -rf /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_dsa_key
38+
39+
WORKDIR /build
40+
41+
RUN git clone https://github.com/juanfont/headscale.git
42+
43+
WORKDIR /build/headscale
44+
45+
RUN go build cmd/headscale/headscale.go
46+
47+
COPY headscale.sh /headscale.sh
48+
COPY postgres.sh /postgres.sh
49+
COPY supervisord.conf /etc/supervisord.conf
50+
51+
WORKDIR /
52+
53+
RUN mkdir -p /run/postgresql
54+
RUN chown postgres:postgres /run/postgresql
55+
56+
RUN adduser -S headscale
57+
58+
#ENV GIN_MODE release
59+
60+
EXPOSE 8000
61+
62+
CMD ["supervisord","--nodaemon", "--configuration", "/etc/supervisord.conf"]

docker/headscale.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
cd /build/headscale
3+
echo 'Writing config...'
4+
echo '''
5+
{
6+
"server_url": "$SERVER_URL",
7+
"listen_addr": "0.0.0.0:8000",
8+
"private_key_path": "private.key",
9+
"public_key_path": "public.key",
10+
"db_host": "localhost",
11+
"db_port": 5432,
12+
"db_name": "headscale",
13+
"db_user": "admin",
14+
"db_pass": "$POSTGRES_PASSWORD"
15+
}
16+
''' > config.json
17+
18+
# Wait until PostgreSQL started and listens on port 5432.
19+
while [ -z "`netstat -tln | grep 5432`" ]; do
20+
echo 'Waiting for PostgreSQL to start ...'
21+
sleep 1
22+
done
23+
echo 'PostgreSQL started.'
24+
25+
# Start server.
26+
echo 'Starting server...'
27+
28+
./headscale

docker/postgres.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/sh
2+
chown -R postgres "$PGDATA"
3+
if [ -z "$(ls -A "$PGDATA")" ]; then
4+
gosu postgres initdb
5+
sed -ri "s/^#(listen_addresses\s*=\s*)\S+/\1'*'/" "$PGDATA"/postgresql.conf
6+
7+
: ${POSTGRES_USER:="postgres"}
8+
: ${POSTGRES_DB:=$POSTGRES_USER}
9+
10+
if [ "$POSTGRES_PASSWORD" ]; then
11+
pass="PASSWORD '$POSTGRES_PASSWORD'"
12+
authMethod=md5
13+
else
14+
echo "==============================="
15+
echo "!!! NO PASSWORD SET !!! (Use \$POSTGRES_PASSWORD env var)"
16+
echo "==============================="
17+
pass=
18+
authMethod=trust
19+
fi
20+
echo
21+
22+
23+
if [ "$POSTGRES_DB" != 'postgres' ]; then
24+
createSql="CREATE DATABASE $POSTGRES_DB;"
25+
echo $createSql | gosu postgres postgres --single -jE
26+
echo
27+
fi
28+
29+
if [ "$POSTGRES_USER" != 'postgres' ]; then
30+
op=CREATE
31+
else
32+
op=ALTER
33+
fi
34+
35+
userSql="$op USER $POSTGRES_USER WITH SUPERUSER $pass;"
36+
echo $userSql | gosu postgres postgres --single -jE
37+
echo
38+
39+
gosu postgres pg_ctl -D "$PGDATA" \
40+
-o "-c listen_addresses=''" \
41+
-w start
42+
43+
echo
44+
for f in /docker-entrypoint-initdb.d/*; do
45+
case "$f" in
46+
*.sh) echo "$0: running $f"; . "$f" ;;
47+
*.sql) echo "$0: running $f"; psql --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" < "$f" && echo ;;
48+
*) echo "$0: ignoring $f" ;;
49+
esac
50+
echo
51+
done
52+
53+
gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
54+
55+
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA"/pg_hba.conf
56+
fi
57+
58+
exec gosu postgres postgres

docker/run.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Example of how to user the docker image
2+
POSTGRES_PASSWORD=
3+
docker build . -t headscale-docker
4+
docker run -p 8000:8000 -v $(pwd)/pgdata:/var/lib/postgresql/data -v "$(pwd)/private.key:/build/headscale/private.key" -v "$(pwd)/public.key:/build/headscale/public.key" -e SERVER_URL=127.0.0.1:8000 -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -ti headscale-docker

docker/supervisord.conf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[supervisord]
2+
nodaemon=true
3+
user = root
4+
5+
[program:headscale]
6+
command=/bin/bash -c "/headscale.sh"
7+
stdout_logfile=/dev/stdout
8+
stdout_logfile_maxbytes=0
9+
10+
[program:postgres]
11+
command=/bin/bash -c "/postgres.sh"
12+
stdout_logfile=/dev/stdout
13+
stdout_logfile_maxbytes=0

0 commit comments

Comments
 (0)