refactor: consolidate Docker build process
Some checks failed
CI / scan_ruby (push) Waiting to run
CI / lint (push) Waiting to run
CI / test (push) Waiting to run
Docker / docker (push) Has been cancelled

- Removed unused Dockerfile.base and Dockerfile.build.
- Combined build steps into the main Dockerfile for improved clarity and maintenance.
- Updated base image references accordingly.

This change streamlines the Docker build process by reducing the number of Dockerfiles and improving the clarity of dependency management.
This commit is contained in:
songtianlun 2025-02-02 00:44:36 +08:00
parent 2c72e96977
commit d69a193e6d
4 changed files with 17 additions and 84 deletions

View File

@ -13,45 +13,6 @@ 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,15 +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
FROM docker.io/songtianlun/today_ai_weather:base_main AS base
ARG RUBY_VERSION=3.3.5
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
# Rails app lives here
WORKDIR /rails
# Install base packages
# Move to Base Dockerfile.
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
# Set production environment
ENV RAILS_ENV="production" \
@ -25,12 +26,21 @@ ENV RAILS_ENV="production" \
BUNDLE_WITHOUT="development"
# Throw-away build stage to reduce size of final image
# FROM base AS build
FROM docker.io/songtianlun/today_ai_weather:devel_main AS build
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
# Move to Devel Dockerfile.
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
# Install application gems
COPY Gemfile Gemfile.lock ./

View File

@ -1,8 +0,0 @@
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

View File

@ -1,30 +0,0 @@
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