docker multi-images

This commit is contained in:
Jun Kurihara 2023-01-20 22:12:51 +09:00
commit c4ba156ea1
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
10 changed files with 138 additions and 12 deletions

View file

@ -0,0 +1,45 @@
########################################
FROM messense/rust-musl-cross:x86_64-musl as builder
ENV TARGET_DIR=x86_64-unknown-linux-musl
ENV CFLAGS=-Ofast
WORKDIR /tmp
COPY . /tmp/
ENV RUSTFLAGS "-C link-arg=-s"
RUN echo "Building rpxy from source" && \
cargo build --release && \
musl-strip --strip-all /tmp/target/${TARGET_DIR}/release/rpxy
########################################
FROM alpine:latest as runner
LABEL maintainer="Jun Kurihara"
ENV TAG_NAME=amd64-slim
ENV TARGET_DIR=x86_64-unknown-linux-musl
ENV RUNTIME_DEPS logrotate ca-certificates
RUN apk add --no-cache ${RUNTIME_DEPS} && \
update-ca-certificates && \
mkdir -p /opt/rpxy/sbin &&\
mkdir -p /var/log/rpxy && \
touch /var/log/rpxy/rpxy.log
COPY --from=builder /tmp/target/${TARGET_DIR}/release/rpxy /opt/rpxy/sbin/rpxy
COPY ./docker/${TAG_NAME}/run.sh /
COPY ./docker/entrypoint.sh /
RUN chmod 755 /run.sh && \
chmod 755 /entrypoint.sh
ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
ENV SSL_CERT_DIR=/etc/ssl/certs
EXPOSE 80 443
CMD ["/entrypoint.sh"]
ENTRYPOINT ["/entrypoint.sh"]

60
docker/amd64-slim/run.sh Normal file
View file

@ -0,0 +1,60 @@
#!/usr/bin/env sh
LOG_FILE=/var/log/rpxy/rpxy.log
CONFIG_FILE=/etc/rpxy.toml
LOG_SIZE=10M
LOG_NUM=10
# logrotate
if [ $LOGROTATE_NUM ]; then
LOG_NUM=${LOGROTATE_NUM}
fi
if [ $LOGROTATE_SIZE ]; then
LOG_SIZE=${LOGROTATE_SIZE}
fi
cat > /etc/logrotate.conf << EOF
# see "man logrotate" for details
# rotate log files weekly
weekly
# use the adm group by default, since this is the owning group
# of /var/log/syslog.
su root adm
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
#dateext
# uncomment this if you want your log files compressed
#compress
# packages drop log rotation information into this directory
include /etc/logrotate.d
# system-specific logs may be also be configured here.
EOF
cat > /etc/logrotate.d/rpxy.conf << EOF
${LOG_FILE} {
dateext
daily
missingok
rotate ${LOG_NUM}
notifempty
compress
delaycompress
dateformat -%Y-%m-%d-%s
size ${LOG_SIZE}
copytruncate
}
EOF
cp -f /etc/periodic/daily/logrotate /etc/periodic/15min
crond restart
# debug level logging
if [ -z $LOG_LEVEL ]; then
LOG_LEVEL=info
fi
echo "rpxy: Logging with level ${LOG_LEVEL}"
RUST_LOG=${LOG_LEVEL} /opt/rpxy/sbin/rpxy --config ${CONFIG_FILE}

55
docker/amd64/Dockerfile Normal file
View file

@ -0,0 +1,55 @@
FROM ubuntu:22.04 AS base
LABEL maintainer="Jun Kurihara"
SHELL ["/bin/sh", "-x", "-c"]
ENV SERIAL 2
########################################
FROM base as builder
ENV CFLAGS=-Ofast
ENV BUILD_DEPS curl make ca-certificates build-essential
WORKDIR /tmp
COPY . /tmp/
ENV RUSTFLAGS "-C link-arg=-s"
RUN update-ca-certificates 2> /dev/null || true
RUN apt-get update && apt-get install -qy --no-install-recommends $BUILD_DEPS && \
curl -sSf https://sh.rustup.rs | bash -s -- -y --default-toolchain stable && \
export PATH="$HOME/.cargo/bin:$PATH" && \
echo "Building rpxy from source" && \
cargo build --release && \
strip --strip-all /tmp/target/release/rpxy
########################################
FROM base AS runner
ENV TAG_NAME=amd64
ENV RUNTIME_DEPS logrotate ca-certificates
RUN apt-get update && \
apt-get install -qy --no-install-recommends $RUNTIME_DEPS && \
apt-get -qy clean && \
apt-get -qy autoremove &&\
rm -fr /tmp/* /var/tmp/* /var/cache/apt/* /var/lib/apt/lists/* /var/log/apt/* /var/log/*.log &&\
mkdir -p /opt/rpxy/sbin &&\
mkdir -p /var/log/rpxy && \
touch /var/log/rpxy/rpxy.log
COPY --from=builder /tmp/target/release/rpxy /opt/rpxy/sbin/rpxy
COPY ./docker/${TAG_NAME}/run.sh /
COPY ./docker/entrypoint.sh /
RUN chmod 755 /run.sh && \
chmod 755 /entrypoint.sh
EXPOSE 80 443
CMD ["/entrypoint.sh"]
ENTRYPOINT ["/entrypoint.sh"]

61
docker/amd64/run.sh Normal file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env sh
LOG_FILE=/var/log/rpxy/rpxy.log
CONFIG_FILE=/etc/rpxy.toml
LOG_SIZE=10M
LOG_NUM=10
# logrotate
if [ $LOGROTATE_NUM ]; then
LOG_NUM=${LOGROTATE_NUM}
fi
if [ $LOGROTATE_SIZE ]; then
LOG_SIZE=${LOGROTATE_SIZE}
fi
cat > /etc/logrotate.conf << EOF
# see "man logrotate" for details
# rotate log files weekly
weekly
# use the adm group by default, since this is the owning group
# of /var/log/syslog.
su root adm
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
#dateext
# uncomment this if you want your log files compressed
#compress
# packages drop log rotation information into this directory
include /etc/logrotate.d
# system-specific logs may be also be configured here.
EOF
cat > /etc/logrotate.d/rpxy << EOF
${LOG_FILE} {
dateext
daily
missingok
rotate ${LOG_NUM}
notifempty
compress
delaycompress
dateformat -%Y-%m-%d-%s
size ${LOG_SIZE}
copytruncate
}
EOF
cp -p /etc/cron.daily/logrotate /etc/cron.hourly/
service cron start
# debug level logging
if [ -z $LOG_LEVEL ]; then
LOG_LEVEL=info
fi
echo "rpxy: Logging with level ${LOG_LEVEL}"
RUST_LOG=${LOG_LEVEL} /opt/rpxy/sbin/rpxy --config ${CONFIG_FILE}

21
docker/docker-compose.yml Normal file
View file

@ -0,0 +1,21 @@
version: "3"
services:
rpxy-rp:
image: jqtype/rpxy
container_name: rpxy
restart: unless-stopped
ports:
- 127.0.0.1:8080:8080
- 127.0.0.1:8443:8443
build:
context: ../
dockerfile: ./docker/amd64/Dockerfile
environment:
- LOG_LEVEL=debug
- LOG_TO_FILE=false
tty: false
privileged: true
volumes:
- ../example-certs/server.crt:/certs/server.crt:ro
- ../example-certs/server.key:/certs/server.key:ro
- ../config-example.toml:/etc/rpxy.toml:ro

14
docker/entrypoint.sh Normal file
View file

@ -0,0 +1,14 @@
#!/usr/bin/env sh
LOG_FILE=/var/log/rpxy/rpxy.log
if [ -z ${LOG_TO_FILE} ]; then
LOG_TO_FILE=false
fi
if "${LOG_TO_FILE}"; then
echo "rpxy: Start with writing log file"
/run.sh 2>&1 | tee $LOG_FILE
else
echo "rpxy: Start without writing log file"
/run.sh 2>&1
fi