donglvlao8367
2017-06-11 02:28
浏览 257

在使用Docker Compose时如何有效地重建go项目?

This may be a stupid question, but I'm new to using Docker-compose. So far, I love it... but I'm having some long build times. I have a project with several dependencies, and I need to obviously rebuild the source every time I make a change. Right now, I'm calling docker-compose build to rebuild the container, followed by a docker-compose up. The problem is:

  1. It's rebuilding the entire container for every change I make to the source code (which takes a long time -- fetching dependencies/etc). This is significantly slowing me down.

  2. I really feel like I should just be able to run a command on the container to rebuild and then re-run the executable, like-so:

    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 amongst the host and container. There shouldn't be a need to refetch all the dependencies. Shouldn't it use the freshly built executable? However, this does not reflect the built changes and port forwarding appears to break.

For reference, here is my Dockerfile:

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'm missing?

图片转代码服务由CSDN问答提供 功能建议

这可能是一个愚蠢的问题,但我不熟悉使用Docker-compose。 到目前为止,我很喜欢...但是我的构建时间很长。 我有一个具有多个依赖项的项目,并且每次进行更改时,都需要明显地重建源代码。 现在,我要调用 docker-compose build 来重建容器,然后调用 docker-compose up 。 问题是:

  1. 对于我对源代码所做的每一次更改,它都在重建整个容器(这需要很长时间-获取依赖项等) 。

  2. 我真的觉得我应该能够在容器上运行命令来重建然后重新运行可执行文件,例如 -so:

     
    docker-compose run web go build。
    docker-compose run web ./app 
     
    docker-compose run web go build。
    docker-compose restart  
     这应该起作用,因为我正在使用一个卷在主机和容器之间共享代码。 不需要重新获取所有依赖项。 它不应该使用新构建的可执行文件吗? 但是,这并不反映已建立的更改,并且端口转发似乎中断。 
      
     
     

    作为参考,这是我的 Dockerfile

      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进行构建-o test1。
    CMD [“ test1”] 
    EXPOSE 3470 
       
     
     

    和我的 docker -compose.yml 文件:

     版本:'3'
    服务:
     postgres:
    图片:postgres 
    卷:
    -./  db / data / psql:/ var / lib / postgresql / data 
    -./db/schema:/db/schema
     redis:
    映像:redis 
    卷:
    -./db/data/redis  :/ data 
    服务器:
    构建:。
    命令:test1 
    卷:
    -。:/ go / src / github.com / codeblooded / test1 
    端口:
    -“ 3470:3470”  
    depends_on:
    -postgres 
    -redis 
       
     
     

    是否缺少某些内容?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongruyan4948 2017-06-11 03:21
    已采纳

    You have asked a good question.

    The command's order in the Dockerfile really matters. Put first the things that don't change frecuently, and later those that are most likely to change in every build:

    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
    

    When a layer change regarding previous build, docker discards the following cached layers an runs them again, sometimes wasting your time.

    Pay attention to the "Using cache" sentence that docker output in each layer that is re-used from previous build.

    Another recommendation, for your dev work, use fresh to re-build your go app automatically every time you change the code. Just installing it in the container and simply using command: fresh in your docker-compose.yml

    点赞 打赏 评论
  • duanletao9487 2017-08-01 13:42

    And if you want to improve your Docker impl, you can make a smaller image. I suggest "multi-stage builds" to do that

    The image size for this build is like 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
    

    Using multi-stage builds, the image weight is the size of the binary and a scratch

    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
    

    Using multi-stage builds you are using a heavy image for build the app and another really smaller for run your app.

    点赞 打赏 评论

相关推荐 更多相似问题