feat: automate base image build for GitHub Actions

- Added new GitHub Actions to docker-main.yml to build and push base images
- Created a new Dockerfile.base with the base packages installation
- Modified the existing Dockerfile to use the new base image
- Created a new Dockerfile.build with the build stage to reduce the size of the final image
This commit is contained in:
songtianlun 2025-02-02 00:38:11 +08:00
parent 536b97a7da
commit 378530cc1b
4 changed files with 86 additions and 17 deletions

View File

@ -13,6 +13,45 @@ env:
IMAGE_NAME: ${{ github.event.repository.name }}
jobs:
build-base-docker:
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v') # 只在发布新版本时构建基础镜像
steps:
- uses: actions/checkout@v3
- name: Login to ${{ env.REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push base image
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.base
push: true
tags: ${{ env.REGISTRY }}/${{ github.actor }}/${{ env.IMAGE_NAME }}:base_main
build-devel-docker:
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v') # 只在发布新版本时构建基础镜像
steps:
- uses: actions/checkout@v3
- name: Login to ${{ env.REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push base image
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.base
push: true
tags: ${{ env.REGISTRY }}/${{ github.actor }}/${{ env.IMAGE_NAME }}:devel_main
docker:
runs-on: ubuntu-latest
steps:

View File

@ -8,16 +8,16 @@
# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.3.5
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
# ARG RUBY_VERSION=3.3.5
# FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
ARG BASE_IMAGE=docker.io/songtianlun/today_ai_weather:base_main
FROM ${BASE_IMAGE} AS base
# Rails app lives here
WORKDIR /rails
# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 libpq5 redis-tools && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Move to Base Dockerfile.
# Set production environment
ENV RAILS_ENV="production" \
@ -26,21 +26,13 @@ ENV RAILS_ENV="production" \
BUNDLE_WITHOUT="development"
# Throw-away build stage to reduce size of final image
FROM base AS build
# FROM base AS build
ARG DEVEl_IMAGE=docker.io/songtianlun/today_ai_weather:devel_main
FROM ${DEVEL_IMAGE} AS build
# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git node-gyp pkg-config python-is-python3 libpq-dev && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Install JavaScript dependencies
ARG NODE_VERSION=20.17.0
ARG YARN_VERSION=1.22.22
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
npm install -g yarn@$YARN_VERSION && \
rm -rf /tmp/node-build-master
# Move to Devel Dockerfile.
# Install application gems
COPY Gemfile Gemfile.lock ./

8
Dockerfile.base Normal file
View File

@ -0,0 +1,8 @@
ARG RUBY_VERSION=3.3.5
# Dockerfile.base
FROM docker.io/library/ruby:$RUBY_VERSION-slim
# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 libpq5 redis-tools && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

30
Dockerfile.build Normal file
View File

@ -0,0 +1,30 @@
ARG BASE_IMAGE=docker.io/your-username/today_ai_weather-base:main
FROM ${BASE_IMAGE} AS base
# Rails app lives here
WORKDIR /rails
# Install base packages, Move to Base Dockerfile.
# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
# Throw-away build stage to reduce size of final image
FROM base AS build
# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git node-gyp pkg-config python-is-python3 libpq-dev && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Install JavaScript dependencies
ARG NODE_VERSION=20.17.0
ARG YARN_VERSION=1.22.22
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
npm install -g yarn@$YARN_VERSION && \
rm -rf /tmp/node-build-master