In this guide we are going to learn how to run a golang revel app using docker and docker-compose. Golang revel is a high productivity, full-stack web framework for the Go
language. Checkout the code used here in this github repo here.
Related Content:
- Building a REST CRUD app with Golang Revel
- How to install Go in Fedora and Rocky Linux/Centos/RHEL
- How to install Go (Golang) in Arch Linux/Manjaro
- Managing Database migrations with Golang goose using incremental SQL changes
In this guide we are going to check how to create a:
- Developent Docker file that hot reloads code changes
- Production Docker file that runs the compiled code package
- Docker Compose file that can be used to bring up the service and
Development Dockerfile
For the dev environment, we want to build a docker image with the current code in it and the dependencies installed then run it while mounting the current code directory to the working directory of the app. That way when we run the container and make local changes, the code changes will reflect in the container as well. This is what is normally known as hot reload.
I have the file named as Dockerfile.local
in my case. This is its content.
|
|
The above Dockerfile builds from Golang image golang:1.17.2-alpine
. We install some packages (git curl bash) using apk then install the revel framework and revel command line tool using go get
.
We then set the working directory to /app
and copy golang dependencies files go.mod
and go.sum
and install the dependencies using go mod download
. The advantage of copying only the dependencies file is that everytime we change the codebase or add new files, we do not have to recreate that build stage.
Finally, we add the code and use revel run
as our entrypoint. Revel run creates a proxy container to run your application in, it also can watch your file for changes and if any changes are made it can redeploy the application (if Go source files are changed), or recompile the templates. It also downloads all necessary libraries
Running In dev with Docker Compose
We are going to use Docker Compose to run our app.
Docker Compose allows us to define and run multi-container applications. We will use Docker compose in out case to run the app container and connect with the database.
This is our docker-compose.yaml
|
|
Here, we are defining two docker containers. We define our app cotainer go-revel-crud and a database container postgres
. For our app container, we are defining build instructions to use the ./Dockerfile.local
we created earlier. We are also mapping the app port 8090
so we can access the app locally. We are also mapping the local path .
to the container path /app where we set as our working directory earlier. The finally we are defining some env variables.
For the postgres container, we are using Postgres 14.0 alpine image to create a container with some initial user and password defined using the env variables.
To build the app image, use this command:
|
|
Output:
|
|
The run the containers using this comand:
|
|
Output:
|
|
Check that the containers are running using the ps command:
|
|
We can test our app by doing a curl to the healthcheck endpoint:
|
|
We can now work on our app locally, the changes will be watched and compiled in the container.
Check the logs using this command:
|
|
Production Docker file that runs the compiled code package
For production build, we would want to run the resultant package of compiling the golang code. Golang revel has a package
command that creates a .tar.gz
with the executable and a run.sh
that will start the app on the defined port.
Here is the docker file:
|
|
The most notable thing here is the different stages. Since go produces an executable, there is no need to have a runtime and the code in the image we deploy to a production environment. Thatn is why we have a first stage where we have a goalang runtime that we use to install dependencies and build the app.
In the second stage, we use a minimal alpine image to extract the package created in the intial stage then define entry point to be the run.sh
script.
Here is the updated docker compose file if we want to run the image created.
|
|
Please note that instead of the build that we had earlier, we are specifying an image that was built from the Dockerfile.
Conclusion
In this tutorial, we explored how to run a golang revel app locally and how we can build an image for production like setting.