donglvlao8367 2017-06-11 02:28
浏览 382
已采纳

在使用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?

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题