How to rebuild go project efficiently while using Docker Compose?

Source – stackoverflow.com

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
  • btschirhart

    Yeah. We have similar issues. I found this thread hoping I could just rebuild the project itself without even having to mess with the container when something changes. It seem like your question is more dedicated to having to rebuild ALL your containers when you make a change to a single container. That I think I can help you with.

    Here’s the steps I take. (In a SEPARATE terminal session)
    1) Stop the Service in question
    $ docker-compose -f docker-compose-file.yml stop {$ServiceName}

    2) Rebuild just that service
    $ docker-compose -f docker-compose-file.yml build {$ServiceName}

    3) Restart that service
    $ docker-compose -f docker-compose-file.yml up -d {$ServiceName}
    (the -d just pipes the service back into the original terminal session so you won’t need to keep spinning up new terminals)

    *** hope that answers your question and helps ***