From d69a193e6deeba5597e64f3fbfa0a1a09a6c61ff Mon Sep 17 00:00:00 2001 From: songtianlun Date: Sun, 2 Feb 2025 00:44:36 +0800 Subject: [PATCH] refactor: consolidate Docker build process - 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. --- .github/workflows/docker-main.yml | 39 ------------------------------- Dockerfile | 24 +++++++++++++------ Dockerfile.base | 8 ------- Dockerfile.build | 30 ------------------------ 4 files changed, 17 insertions(+), 84 deletions(-) delete mode 100644 Dockerfile.base delete mode 100644 Dockerfile.build diff --git a/.github/workflows/docker-main.yml b/.github/workflows/docker-main.yml index 949c2c5..4146b84 100644 --- a/.github/workflows/docker-main.yml +++ b/.github/workflows/docker-main.yml @@ -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: diff --git a/Dockerfile b/Dockerfile index 5394067..69e505b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 ./ diff --git a/Dockerfile.base b/Dockerfile.base deleted file mode 100644 index 25b4ac2..0000000 --- a/Dockerfile.base +++ /dev/null @@ -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 \ No newline at end of file diff --git a/Dockerfile.build b/Dockerfile.build deleted file mode 100644 index db05382..0000000 --- a/Dockerfile.build +++ /dev/null @@ -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