Skip to content

Commit

Permalink
ci: minify docker image (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyang01 authored Dec 5, 2024
1 parent 2e14b6f commit 4dabd74
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 61 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/replication-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name: Docker Replica Mode Test
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
test-replication:
Expand Down
100 changes: 41 additions & 59 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
# Use the official Go image for building the Go application
# Step 1: Build stage
FROM --platform=${BUILDPLATFORM} golang:latest AS builder

# Set environment variables for cross-compilation
ARG TARGETOS
ARG TARGETARCH
ENV GOOS=$TARGETOS
ENV GOARCH=$TARGETARCH

# Set the working directory inside the container
WORKDIR /app

# Copy Go module files and download dependencies
# Install build dependencies
RUN apt-get update && \
apt-get install -y debian-archive-keyring curl unzip \
gcc-aarch64-linux-gnu g++-aarch64-linux-gnu \
--no-install-recommends && \
rm -rf /var/lib/apt/lists/*

# Download MySQL Shell and DuckDB CLI in builder stage
RUN mkdir -p /downloads && cd /downloads && \
if [ "$TARGETARCH" = "arm64" ]; then \
curl -LJO https://dev.mysql.com/get/Downloads/MySQL-Shell/mysql-shell-9.1.0-linux-glibc2.28-arm-64bit.tar.gz && \
tar -zxf mysql-shell-9.1.0-linux-glibc2.28-arm-64bit.tar.gz && \
mv mysql-shell-9.1.0-linux-glibc2.28-arm-64bit mysqlsh && \
ARCH="aarch64"; \
else \
curl -LJO https://dev.mysql.com/get/Downloads/MySQL-Shell/mysql-shell_9.1.0-1debian12_amd64.deb && \
ARCH="amd64"; \
fi && \
curl -LJO https://github.com/duckdb/duckdb/releases/download/v1.1.3/duckdb_cli-linux-$ARCH.zip && \
unzip duckdb_cli-linux-$ARCH.zip && \
chmod +x duckdb

# Build Go application
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download

# Copy the remaining source code
COPY . .

RUN apt-get update && \
apt-get install -y debian-archive-keyring && \
apt-get update && \
apt-get install -y \
gcc-aarch64-linux-gnu \
g++-aarch64-linux-gnu \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

# Build the Go application for the target OS and architecture
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
Expand All @@ -39,80 +47,54 @@ RUN --mount=type=bind,target=. \
CGO_ENABLED=1 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /myduckserver; \
fi


# Step 2: Final stage
FROM debian:bookworm-slim

ARG TARGETOS
ARG TARGETARCH

RUN apt-get update && apt-get install -y debian-archive-keyring \
&& apt-get update && apt-get install -y \
vim \
procps \
curl \
unzip \
libssh-4 \
libstdc++6 \
python3 python3-pip \
libpq-dev postgresql-client \
postgresql-client \
--no-install-recommends \
&& pip install --no-cache-dir "sqlglot[rs]" --break-system-packages \
&& rm -rf /var/lib/apt/lists/*

# Install Python dependencies
RUN pip install --no-cache-dir "sqlglot[rs]" --break-system-packages
# Install MySQL Shell from builder stage
COPY --from=builder /downloads/duckdb /usr/local/bin/
COPY --from=builder /downloads/mysqlsh /usr/local/mysqlsh.arm64
COPY --from=builder /downloads/mysql-shell_9.1.0-1debian12_amd64.deb /tmp/mysql-shell.deb

# Install mysql-shell
RUN if [ "$TARGETARCH" = "arm64" ]; then \
curl -LJO https://dev.mysql.com/get/Downloads/MySQL-Shell/mysql-shell-9.1.0-linux-glibc2.28-arm-64bit.tar.gz \
&& tar -zxf mysql-shell-9.1.0-linux-glibc2.28-arm-64bit.tar.gz \
&& rm mysql-shell-9.1.0-linux-glibc2.28-arm-64bit.tar.gz \
&& mv mysql-shell-9.1.0-linux-glibc2.28-arm-64bit /usr/local/mysqlsh \
&& ln -s /usr/local/mysqlsh/bin/mysqlsh /usr/local/bin/mysqlsh; \
mv /usr/local/mysqlsh.arm64 /usr/local/mysqlsh && \
ln -s /usr/local/mysqlsh/bin/mysqlsh /usr/local/bin/mysqlsh; \
else \
curl -LJO https://dev.mysql.com/get/Downloads/MySQL-Shell/mysql-shell_9.1.0-1debian12_amd64.deb \
&& apt install -y ./mysql-shell_9.1.0-1debian12_amd64.deb \
&& rm mysql-shell_9.1.0-1debian12_amd64.deb; \
fi \
&& mysqlsh --version

# Dynamic DuckDB CLI download based on architecture
RUN if [ "$TARGETARCH" = "arm64" ]; then \
ARCH="aarch64"; \
else \
ARCH="amd64"; \
apt-get update && \
apt-get install -y /tmp/mysql-shell.deb && \
rm /tmp/mysql-shell.deb && \
rm -rf /var/lib/apt/lists/*; \
fi && \
curl -LJO https://github.com/duckdb/duckdb/releases/download/v1.1.3/duckdb_cli-linux-$ARCH.zip \
&& unzip duckdb_cli-linux-$ARCH.zip \
&& chmod +x duckdb \
&& mv duckdb /usr/local/bin \
&& rm duckdb_cli-linux-$ARCH.zip \
&& duckdb -c 'SELECT extension_name, loaded, install_path FROM duckdb_extensions() where installed'

RUN duckdb -version
rm -f /usr/local/mysqlsh.arm64

# Setup user and working directory
RUN useradd --create-home --user-group --shell /bin/bash admin \
&& echo 'admin:admin' | chpasswd \
&& echo 'admin ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# Set the working directory inside the container
USER admin
WORKDIR /home/admin

# Copy the compiled Go binary from the builder stage
# Copy application files
COPY --from=builder /myduckserver /usr/local/bin/myduckserver
COPY --chown=admin:admin --chmod=755 docker/*.sh .
COPY --chown=admin:admin --chmod=755 devtools/replica-setup-mysql ./replica-setup-mysql
COPY --chown=admin:admin --chmod=755 devtools/replica-setup-postgres ./replica-setup-postgres

# ENV LC_CTYPE="en_US.UTF-8"
# ENV LANG="en_US.UTF-8"

# Install the required DuckDB extensions
# Initialize DuckDB
RUN myduckserver --init

# Expose the ports your server will run on (if applicable)
EXPOSE 3306
EXPOSE 5432

# Set the default command to run the Go server
EXPOSE 3306 5432
ENTRYPOINT /home/admin/entrypoint.sh

0 comments on commit 4dabd74

Please sign in to comment.