Setting up GoLang 1.13.7 with Docker
Today we’re going to go over how I set up docker and golang together on my local machine to get started programming in go. Docker is a service that uses virtualization to create containers used for software development. This will show you how to set up a golang environment within it. Go is a compiled programming language that was developed at google, and is syntactically similar to C.
Getting Started
This guide assumes you have gone through the basics of setting up docker on whatever you’re system may be. If not, be sure to check out the docker basics here: https://docs.docker.com/get-started/ .
Project
The first thing we will need is a place to put our project. Make a directory where you want to house your project. I work in linux, so mine is called
~/projects/golang
Inside this folder we will have two files; docker-compose.yml and main.go
projects |_ golang |_ docker-compose.yml |_ main.go
Docker
Now it’s time to set up our docker-compose.yml file so that it can pull down the newest golang container and run our program. This file is a YAML based file that contains information for docker to set up containers to run locally. Starting with the basics, we have this:
version: "3" services: app: image: golang:latest
The version is required for all docker-compose.yml files. Services is a list of all the containers that will start when you run docker-compose up and ‘app’ is the name for our golang docker container. The ‘image’ tells which image and version to pull, ‘:latest’ is our version. As of now that is 1.13.7 . You can view all the possible versions to pull from here: https://hub.docker.com/_/golang
Next up we’ll configure the settings for the container.
version: "3" services: app: image: golang:latest volumes: - .:/go/src/ working_dir: /go/src/ command: go run main.go
Volumes link up the files locally on your machine to the files that exist in the docker container. So we are linking up our current directory . to the folder /go/src in our golang container. The working_dir setting tells the golang container where the gopath is. Command tells us what to do when the container starts up. This completes our docker setup. Now we have to set up the golang project.
GoLang
Now that we have docker ready to go, it’s time to set up our app to run. To do this, all we need is our main.go file. First, we need to declare the package. The package that get’s executed in go should be called ‘main’
package main
Then we need to make our ‘main’ function. This is the default that get’s executed when running go.
package main func main() { }
For our purpose, we’re going to do a simple hello world. This is going to require a package called ‘fmt’.
package main import "fmt" func main() { fmt.Println("Hello World") }
Execution
Now that we’ve made our package and set up docker, we can run our program. Since we set the command up in the container, we have 2 ways to do this. The first is running our specific command.
docker-compose run app go run main.go
This should output:
Hello World
As you can see, this contains the command that we set up within the container. So to make things shorter, all we need to do is run:
docker-compose up
Our output should look something like this:
Attaching to golang_app_1 app_1 | Hello World golang_app_1 exited with code 0
docker-compose.yml
version: "3" services: app: image: golang:latest volumes: - .:/go/src/ working_dir: /go/src/ command: go run main.go
main.go
package main import "fmt" func main() { fmt.Println("Hello World") }
Alina
If you try any of these commands from a directory other than the directory that contains a Docker container and ERROR: Can’t find a suitable configuration file in this directory or any parent. Are you in the right directory? Supported filenames: docker-compose.yml, docker-compose.yaml
FROSTO0610
Thank you!!1