go install takes a lot of time in dockerfile
I use go-jet
to generate the model from the database, so I need to do it in go build
Before running jet
. I need to install it via go install
otherwise docker cannot find the jet
executable. I encountered a problem:
When I rebuild my application, docker re-downloads go-jet every time (takes 60-80 seconds).
Is there a better way to generate (or cache the jet library)? Installation takes too long
I tried caching jet
in the Dockerfile, but it re-downloaded again:
RUN --mount=type=cache,target=/go/pkg/mod/ \ go install github.com/go-jet/jet/v2/cmd/jet@latest
My 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
My 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
And my docker-compose, just in case:
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
Correct Answer
Found a very simple solution. I need to put go install
before COPY. .
. What happens: When docker detects changes in the project, it deletes the application cache (line COPY . .
) and all following lines (including go install
).
My final Dockerfile:
... RUN go install github.com/go-jet/jet/v2/cmd/jet@latest COPY . . ...
The above is the detailed content of go install takes a lot of time in dockerfile. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.

Golang is suitable for rapid development and concurrent scenarios, and C is suitable for scenarios where extreme performance and low-level control are required. 1) Golang improves performance through garbage collection and concurrency mechanisms, and is suitable for high-concurrency Web service development. 2) C achieves the ultimate performance through manual memory management and compiler optimization, and is suitable for embedded system development.

The performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.

C is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.
