Maison  >  Article  >  développement back-end  >  aller installer prend beaucoup de temps dans dockerfile

aller installer prend beaucoup de temps dans dockerfile

王林
王林avant
2024-02-06 09:50:041130parcourir

go install 在 dockerfile 中需要花费很多时间

Contenu de la question

J'utilise go-jet 从数据库生成模型,因此我需要在 go build 之前运行 jet。我需要通过 go install 安装它,否则 docker 找不到 jet des exécutables. J'ai rencontré un problème : Lorsque je reconstruis mon application, Docker télécharge à nouveau Go-Jet à chaque fois (cela prend 60 à 80 secondes).

Existe-t-il une meilleure façon de générer (ou de mettre en cache la bibliothèque jet) ? L'installation prend trop de temps

J'ai essayé de mettre en cache jet dans le Dockerfile, mais il a été retéléchargé :

RUN --mount=type=cache,target=/go/pkg/mod/ \
    go install github.com/go-jet/jet/v2/cmd/jet@latest

Mon Dockerfile :

FROM golang:latest

WORKDIR /app

COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . .

RUN go install github.com/go-jet/jet/v2/cmd/jet@latest

CMD sh deployments/startup.sh

Mon startup.sh :

jet -dsn="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?sslmode=disable" -schema=public -path=.gen
cd cmd/app
go build
./app

Et mon docker-compose, juste au cas où :

version: '3.9'

x-common-variables: &database-credentials
  POSTGRES_DB: ${POSTGRES_DB}
  POSTGRES_USER: ${POSTGRES_USER}
  POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
  POSTGRES_HOST: ${POSTGRES_HOST}
  POSTGRES_PORT: ${POSTGRES_PORT}

services:
  cache:
    image: redis:latest
    restart: always
    ports:
      - "6379:6379"
    command: redis-server
    environment:
      - REDIS_PORT=6379

  database:
    image: postgres:latest
    container_name: db
    environment:
      <<: *database-credentials
    ports:
      - "5433:5432"
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 5s
      timeout: 5s
      retries: 10

  migrate:
    image: migrate/migrate
    volumes:
      - ../internal/app/storage/postgres/migrations:/migrations
    command: -path /migrations -database "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:${POSTGRES_PORT}/${POSTGRES_DB}?sslmode=disable" up
    depends_on:
      database:
        condition: service_healthy

  webapi:
    image: webapi
    build: ../
    ports:
      - "8080:8080"
    depends_on:
      - migrate
    environment:
      <<: *database-credentials

Bonne réponse


J'ai trouvé une solution très simple. J'en ai besoin go install 放在 COPY 之前。 .。发生了什么:当 docker 检测到项目中的更改时,它删除了应用程序缓存(行 COPY . . )和所有以下行(包括 go install).

Mon Dockerfile final :

...

RUN go install github.com/go-jet/jet/v2/cmd/jet@latest

COPY . .

...

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer