Build from Source in a Docker Container

Referring to this example,we create a canonical dev environment for Go and Python developers using Docker images.

Editing on Host

When we use this Docker image for daily development work, the source code relieson the host computer instead of the container. The source code includes this repoand all its dependencies, for example, the Go package google.golang.org/grpc.Code-on-the-host allows us to run our favorite editors (Emacs, VIM, Eclipse, and more)on the host. Please free to rely on editors add-ons to analyze the source codefor auto-completion.

Building in Container

We build a Docker image that contains development tools below.

  • Python Interpreter
  • Go compiler
  • Protobuf compiler
  • Protobuf to Go compiler extension
  • Protobuf to Python compiler extensionBecause this repo contains Go code, please make sure that you have the directory structure required by Go. On my computer, I have GOPATH set to $Home/go, you can have your $GOPATH pointing to any directory as you like.
  1. export GOPATH=$HOME/go

Now that $GOPATH$ is set, we could git clone the source code of our project by running:

  1. go get github.com/sql-machine-learning/sqlflow

Change the directory to our project root, and we can use go get to retrieveand update Go dependencies. Note -t instructs get to also download the packages required to buildthe tests for the specified packages. As all Git users would do, we run git pull from time to time to sync up withothers’ work. If somebody added new dependencies, we might need to run go -u ./…after git pull to update dependencies.

  1. cd $GOPATH/src/github.com/sql-machine-learning/sqlflow
  2. go get -u -t ./...

To build the project, we need protobuf compiler, Go compiler, Python interpreter and gRPC extension to protobuf compiler. To prepare our dev environment with these tools, the easist way is to pull latest image from DockerHub by running command below and give it an alias sqlflow:latest. Alternatively, we provide a Dockerfile where can build image from. Note it will take a while to build from Dockerfile, especially when the network is unpredictable.

  1. docker pull sqlflow/sqlflow:latest
  2. docker tag sqlflow/sqlflow:latest sqlflow:latest

or

  1. docker build -t sqlflow:latest .

Development

Build and Test

We build and test the project inside the docker container. To run the container, we need to map the $GOPATH directory on the host into the/go directory in the container, because the Dockerfile configures /go asthe $GOPATH in the container:

  1. docker run --rm -it -v $GOPATH:/go \
  2. -w /go/src/github.com/sql-machine-learning/sqlflow \
  3. sqlflow:latest bash

Inside the Docker container, start a MySQL server in the background

  1. service mysql start

run all the tests as

  1. go generate ./...
  2. go test -v ./...

where go generate invokes the protoc command to translate server/sqlflow.protointo server/sqlflow.pb.go and go test -v builds and run unit tests.

Demo: Command line Prompt

The demo requires a MySQL server instance with populated data. If you don’t, pleasefollow example/datasets/ to start one on the host.After setting up MySQL, run the following inside the Docker container

  1. go run cmd/demo/demo.go --datasource="mysql://root:root@tcp(host.docker.internal:3306)/?maxAllowedPacket=0"

You should be able to see the following prompt

  1. sqlflow>