How to efficiently rebuild a project using Docker Compose?

This might be a dumb question, but I'm new to using Docker-compose. While I like it ... but I have a lot of time to build. I have a project with several dependencies, and I have to explicitly rebuild the source every time I make changes. I'm calling right now docker-compose buildto rebuild the container, and then docker-compose up. The problem is this:

  • It rebuilds the entire container for every change I make for the source code (which takes a lot of time - fetching / etc). It slows me down a lot.

  • I really feel that I just need to run the command in the recovery container and then re-run the executable, for example:

    docker-compose run web go build.
    docker-compose run web ./app
    or
    docker-compose run web go build.
    docker-compose restart
    
    . This should work because I'm using a volume to share code between the host and the container. You will not need to reinstall all the dependencies. Should I just use the newly created executable? However, this does not reflect the built-in changes, and port forwarding seems to be interrupted.

For reference, here is my Docker file:

FROM golang:1.8

COPY . /go/src/github.com/codeblooded/test1
WORKDIR /go/src/github.com/codeblooded/test1

RUN echo $PATH
RUN go get -d -v ./...
RUN go install -v ./...

RUN go build -o test1 .
CMD ["test1"]
EXPOSE 3470

And my docker-compose.yml file:

version: '3'
services:
  postgres:
    image: postgres
    volumes:
      - ./db/data/psql:/var/lib/postgresql/data
      - ./db/schema:/db/schema
  redis:
    image: redis
    volumes:
      - ./db/data/redis:/data
  server:
    build: .
    command: test1
    volumes:
      - .:/go/src/github.com/codeblooded/test1
    ports:
      - "3470:3470"
    depends_on:
      - postgres
      - redis

Is there something I am missing?

+4
source share
2 answers

You asked a good question.

Docker . , , , , , :

FROM golang:1.8

RUN go get -d -v ./...
RUN go install -v ./...

COPY . /go/src/github.com/codeblooded/test1
WORKDIR /go/src/github.com/codeblooded/test1

RUN echo $PATH

RUN go build -o test1 .
CMD ["test1"]
EXPOSE 3470

, , , .

" ", , .

, , fresh, go , . command: fresh docker-compose.yml

+5

Docker impl, . " ",

600mb

FROM golang:1.8

RUN go get -d -v ./...
RUN go install -v ./...

COPY . /go/src/github.com/codeblooded/test1
WORKDIR /go/src/github.com/codeblooded/test1

RUN echo $PATH

RUN go build -o test1 .
CMD ["test1"]
EXPOSE 3470

,

FROM golang:1.8 as builder

RUN go get -d -v ./...
RUN go install -v ./...

COPY . /go/src/github.com/codeblooded/test1

WORKDIR /go/src/github.com/codeblooded/test1

RUN echo $PATH
RUN CGO_ENABLED=0 GOOS=linux go build -o test1 .


FROM alpine:latest

RUN apk --no-cache add ca-certificates

WORKDIR /go/src/github.com/codeblooded/

COPY --from=builder /go/src/github.com/codeblooded/test1 .

CMD ["test1"]

EXPOSE 3470

, , .

0

Source: https://habr.com/ru/post/1678968/


All Articles