TannerDanger
TannerDanger

Reputation: 33

How write dockerfile to properly pull code from my github

I'm working on building a website in Go, which is hosted on my home server via docker.

What I'm trying to do: I make changes to my website/server locally, then push them to github. I'd like to write a dockerfile such that it pulls this data from my github, builds the image, which my docker-compose file will then use to create the container.

Unfortunately, all of my attempts have been somewhat close but wrong.

FROM golang:1.8-onbuild
MAINTAINER <my info>
RUN go get <my github url>
ENV webserver_path /website/
ENV PATH $PATH: webserver_path
COPY website/ .
RUN go build .
ENTRYPOINT ./website
EXPOSE <ports>

This file is kind of a combination of a few small guides I found through google searches, but none quite gave me the information I needed and it never quite worked.

I'm hoping somebody with decent docker experience can just put a Dockerfile together for me to use as a guide so I can find what I'm doing wrong? I think what I'm looking for can be done in only a few lines, and mine is a little more verbose than needed.

ADDITIONAL BUT PROBABLY UNNECESSARY INFORMATION BELOW

Project layout:

enter image description here

Data: is where my go files are Sidenote: This was throwing me errors when trying to build image, something about not being in the environment path. Not sure if that is helpful

Static: CSS, JS, Images

TPL: go template files

Main.go: launches server/website

Upvotes: 0

Views: 607

Answers (3)

TannerDanger
TannerDanger

Reputation: 33

I was able to solve my issue by just creating an automated build through docker hub, and just using this for my dockerfile:

FROM golang-onbuild
EXPOSE <ports>

It isn't exactly the correct answer to my question, but it is an effective workaround. The automated build connects with my github repo the way I was hoping my dockerfile would.

Upvotes: 0

Igor Skobelev
Igor Skobelev

Reputation: 337

There are several strategies:

  • Using of pre-build app. Build your app using go build command according to target system architecture and OS (using GOOS and GOARCH system variable for example) then use COPY docker command to move this builded file (with assets and templates) to your WORKDIR and finally run it via CMD or ENTRYPOINT (last is preferable). Dockerfile for this example will look like:

FROM scratch

ENV PORT 8000 EXPOSE $PORT

COPY advent / CMD ["/advent"]

  • Build by dockerfile. Typical Dockerfile:
# Start from a Debian image with the latest version of Go installed
# and a workspace (GOPATH) configured at /go.
FROM golang

# Copy the local package files to the container's workspace.
ADD . /go/src/github.com/golang/example/outyet

# Build the outyet command inside the container.
# (You may fetch or manage dependencies here,
# either manually or with a tool like "godep".)
RUN go install github.com/golang/example/outyet

# Run the outyet command by default when the container starts.
ENTRYPOINT /go/bin/outyet

# Document that the service listens on port 8080.
EXPOSE 8080
  • Using GitHub. Build your app and pull to dockerhub as ready to use image.

Upvotes: 1

Adam Smith
Adam Smith

Reputation: 54213

Github supports Webhooks which can be used to do all sorts of things automagically when you push to a git repo. Since you're already running a web server on your home box, why don't you have Github send a POST request to that when it receives a commit on master and have your home box re-download the git repo and restart web services from that?

Upvotes: 0

Related Questions