Skip to content
This repository has been archived by the owner on Nov 6, 2021. It is now read-only.

Dockerize a Go on Rails application

B1nj0y edited this page Aug 18, 2018 · 19 revisions

One of Golang's smart features is its app could be compiled as a single executable binary. That's convenient for release and deployment.

But we can't compile the assets like stylesheet, javascript and images into this binary by a common go build. Of course there're some solutions indeed, but I don't take it an idiomatic way, and sometimes it's not fit well with some framework like Gin.

In fact Docker is very nice solution for Golang web app release and deployment. So go-on-rails will generate a Dockerfile at meanwhile when generating the application. You can find a Dockerfile in the go_app folder.

Build a Docker image

Now just run make image under the go_app directory will build a docker image for the generated Golang app.

The docker image names after a format like USER/IMAGE:TAG, you can pass the values as arguments to make image command, like:

make image USER=goonr IMAGE=nice_app TAG=1.2.0

Your system username and other default values(IMAGE=myapp and TAG=latest) will be used if you omit them.

Of course, you can run docker build command by yourself instead of the make image.

Database connection in Docker

go-on-rails use the same database configuration with its Rails app defined in the config/database.yml.

If the database's host is not a local address, i.e. 127.0.0.1 or localhost, Dockerfile will keep it as it is. If the database's host is a local address, Dockerfile will change the database host in the code as a container link as db, and you need set up another container as db by hand and link it in Rails app and run those Rails deploy at first.

Or you can use a docker-compose skeleton we generate for running Rails app and Golang app all in containers. Then when the database host is a local one as above concerned, a db node can be found in this generated docker-compose.yml file. Of course you may need modify it with your own situation.

Note: The database SQLite is not supported to generate a db config in such a Dockerfile and docker-compose.

Clone this wiki locally