├── Dockerfile ├── LICENSE ├── README.md └── bin ├── go-build └── go-run /Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # Go runtime Dockerfile 3 | # 4 | # https://github.com/dockerfile/go-runtime 5 | # 6 | 7 | # Pull base image. 8 | FROM dockerfile/go 9 | 10 | # Add scripts. 11 | ADD bin/go-build /usr/local/bin/go-build 12 | ADD bin/go-run /usr/local/bin/go-run 13 | 14 | # Add executable permission to scripts. 15 | RUN chmod +x /usr/local/bin/go-* 16 | 17 | # Set instructions on build. 18 | ONBUILD ADD . /gopath/src/app/ 19 | ONBUILD RUN go-build 20 | 21 | # Define default command. 22 | CMD ["go-run"] 23 | 24 | # Expose port. 25 | EXPOSE 8080 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Dockerfile Project 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Go runtime Dockerfile 2 | 3 | 4 | This repository contains **Dockerfile** of [Go programming language](http://golang.org/) runtime for [Docker](https://www.docker.com/)'s [automated build](https://registry.hub.docker.com/u/dockerfile/go-runtime/) published to the public [Docker Hub Registry](https://registry.hub.docker.com/). 5 | 6 | This image is a base image that makes it easy to dockerize standard golang applications. 7 | 8 | It can automatically bundle a golang application with its dependencies and set the default command to the compiled binary with no additional Dockerfile instructions. 9 | 10 | This project heavily borrowed code from Google's [google/golang-runtime](https://registry.hub.docker.com/u/google/golang-runtime/) Docker image. 11 | 12 | 13 | ### Base Docker Image 14 | 15 | * [dockerfile/go](http://dockerfile.github.io/#/go) 16 | 17 | 18 | ### Installation 19 | 20 | 1. Install [Docker](https://www.docker.com/). 21 | 22 | 2. Download [automated build](https://registry.hub.docker.com/u/dockerfile/go-runtime/) from public [Docker Hub Registry](https://registry.hub.docker.com/): `docker pull dockerfile/go-runtime` 23 | 24 | (alternatively, you can build an image from Dockerfile: `docker build -t="dockerfile/go-runtime" github.com/dockerfile/go-runtime`) 25 | 26 | 27 | ### Usage 28 | 29 | This image assumes that your application: 30 | 31 | * has a `main` package 32 | * listens on port `8080` 33 | * may have a `.godir` file containing the import path for your application if it vendors its dependencies 34 | 35 | When building your application docker image, `ONBUILD` triggers fetch non-vendored dependencies of your application using `go get`. 36 | 37 | * **Step 1**: Create a Dockerfile in your golang application directory with the following content: 38 | 39 | ```dockerfile 40 | FROM dockerfile/go-runtime 41 | ``` 42 | 43 | * **Step 2**: Build your container image by running the following command in your application directory: 44 | 45 | ```sh 46 | docker build -t="app" . 47 | ``` 48 | 49 | * **Step 3**: Run application by mapping port `8080`: 50 | 51 | ```sh 52 | APP=$(docker run -d -p 8080 app) 53 | PORT=$(docker port $APP 8080 | awk -F: '{print $2}') 54 | echo "Open http://localhost:$PORT/" 55 | ``` 56 | -------------------------------------------------------------------------------- /bin/go-build: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | if [ -f /gopath/src/app/.godir ]; then 6 | GODIR=$(cat /gopath/src/app/.godir) 7 | mkdir -p /gopath/src/$(dirname ${GODIR}) 8 | ln -s /gopath/src/app /gopath/src/${GODIR} 9 | go get ${GODIR} 10 | else 11 | go get app 12 | fi 13 | -------------------------------------------------------------------------------- /bin/go-run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | if [ -f /gopath/src/app/.godir ]; then 6 | GODIR=$(cat /gopath/src/app/.godir) 7 | GOBIN=/gopath/bin/$(basename ${GODIR}) 8 | else 9 | GOBIN=/gopath/bin/app 10 | fi 11 | 12 | exec ${GOBIN} $@ 13 | --------------------------------------------------------------------------------