Skip to content

Commit

Permalink
+ docker README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
darklynx committed Jul 19, 2016
1 parent e4003c0 commit c0661d8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
79 changes: 79 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,82 @@ request-baskets alpine 7991c7cec214 6 minutes ago 15.2 MB
request-baskets golang 8fe4269dbc19 7 minutes ago 768.4 MB
...
```

## Docker with "golang"

This is an example of how to use a great [golang:onbuild](https://github.com/docker-library/docs/tree/master/golang) image from official Docker library for Go language.

Building an image that is based on `golang:ongoing` image should be triggered from the root folder of Go application. The source code from the current folder is copied into container and compiled inside. The result of compilation is packed into a new image with a command that launches the application. The content of [Dockerfile](./golang/Dockerfile) in this case is minimal.

Usage example (should be started from source code root directory):
```bash
$ docker build -t request-baskets .
$ docker run -it --rm --name rbaskets -p 55555:55555 request-baskets
```

Now you can visit [http://localhost:55555](http://localhost:55555) in your browser.

To stop the service simply hit `Ctrl+C` - this will stop and delete the running container (note `--rm` flag).

Use of `golang:ongoing` image allows you to build and run Go applications without Go SDK being installed in your development environment. However the Go SDK with all dependencies remains in final image with built application, which results in big image size (see above).

## Docker with "ubuntu"

This is an example of how to build and package Go application within container based on well known Linux server images. In this case the image is based on latest [ubuntu](https://github.com/docker-library/docs/tree/master/ubuntu) image from official Docker library.

The [Dockerfile](./ubuntu/Dockerfile) performs several steps within a one-line command:

* Update current container
* Install `golang` & `git` packages
* Build `request-baskets` service and move it in `bin` directory
* Cleanup: delete built folder and purge `golang` & `git` packages

Since Docker persists every command as a separate layer, one-liner allows to keep the container size smaller and do not include `golang` & `git` packages with dependencies in any layer of final container.

However, the purge command does not remove all dependencies that are installed with `golang` & `git` packages and final image results in more than 350 MB, even though the original Ubuntu image is only 125 MB.

Since application is built using `go get github.com/darklynx/request-baskets` command, which pulls the latest version of application from GitHub, you do not need to launch image built from source directory, it can be done from anywhere:

```bash
$ cd docker/ubuntu
$ docker build -t request-baskets .
$ docker run -d --name rbaskets -p 55555:55555 request-baskets
```

Last command from above will launch an instance of container in detached mode (as a service). Now you can perform following commands.

Access log of running service:
```bash
$ docker logs rbaskets
```

Stop the container with service:
```bash
$ docker stop rbaskets
```

And start it again:
```bash
$ docker start rbaskets
```

Note that `request-baskets` is configured to save data in Bolt database, hence the collected data is not lost after container restart. Database is placed on a volume that can be accessed from another containers for analyses or backup purposes.

## Docker with "minimal"

Building minimalistic docker image with `request-baskets` service is spit in 2 steps:

* Compile Go project with `golang:latest` container
* Create image based on `alpine:latest` with `request-baskets` service only

In order to simplify the process [build script](./minimal/build.sh) is provided. Simply run it from any location and it should build a minimalistic image with `request-baskets` service for you.

**Behind the scene**

During initial step [golang](https://github.com/docker-library/docs/tree/master/golang) Docker container is used to compile `request-baskets` from source code hosted on GitHub. The result of compilation is saved outside of the container within the [script's directory](./minimal). This is achieved by mapping the current directory as a volume for compilation target `/go/bin` (default for `golang` image) inside the container. Additionally compilation is configured to enable static linking (note `CGO_ENABLED=0` environment variable).

Second step uses [Dockerfile](./minimal/Dockerfile) to build image based on simply wonderful [alpine](https://github.com/docker-library/docs/tree/master/alpine) image from official Docker library. This image has less then 5 MB size and with `request-baskets` service included the final image takes just ~15 MB.

Important: since `alpine` image includes minimal or even less dependencies to minimize the image size the [static linking](http://www.blang.io/posts/2015-04_golang-alpine-build-golang-binaries-for-alpine-linux/) during Go compilation is a solution to build an executable that may run inside `alpine` container.

Similar to "ubuntu" Docker script this [Dockerfile](./minimal/Dockerfile) also declares a volume to expose the Bolt database location.
4 changes: 2 additions & 2 deletions docker/minimal/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ set -e
cd "$(dirname "$0")"

# build (via get, static link) request-baskets service with latest golang docker
docker run --rm -v "$PWD":/go/bin --env CGO_ENABLED=0 golang:latest go get -v github.com/darklynx/request-baskets
docker run --rm -v "$PWD":/go/bin --env CGO_ENABLED=0 golang go get -v github.com/darklynx/request-baskets

# create a minimalistic docker image (based on alpine) with service only
docker build -t request-baskets:alpine .
docker build -t request-baskets .

# cleanup
rm request-baskets
Expand Down

0 comments on commit c0661d8

Please sign in to comment.