Echo
in the framework
Go mod
for library management
Fresh
for hot reload
I would like to build a Go development environment using Docker using Docker.
The version of Go is Go 1.15
.
This article does not explain how to install Docker or explain it in detail.
.
├── app
│ ├── Dockerfile
│ ├── go.mod
│ ├── go.sum
│ └── main.go
└── docker-compose.yml
You can do this by downloading the code from here and docker-compose up
.
First of all, create a suitable directory (in my case, create a directory called go-docker)
Create a directory called app by referring to the above directory structure,
Then create a file in the app directory with the name Dockerfile
.
Dockerfile
FROM golang:1.15-alpine
WORKDIR /go/src/app
ADD ./app /go/src/app
RUN apk update && \
apk add --no-cache git && \
go get github.com/labstack/echo/... && \
go get github.com/pilu/fresh
EXPOSE 8080
CMD ["fresh"]
Next, create a file called main.go
in the app directory.
main.go
package main
import (
"net/http"
"github.com/labstack/echo"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello world")
})
e.Logger.Fatal(e.Start(":8080"))
}
Then create a file with the name docker-compose.yml
.
Please be careful where you create the file by referring to the folder structure.
docker-compose.yml
version: "3"
services:
app:
build:
context: .
dockerfile: app/Dockerfile #Dockerfile location
volumes:
- ./app:/go/src/app
ports:
- "8080:8080"
tty: true #Container persistence
build
Then run the docker-compose build
command in the same hierarchy as where the yml file is.
$ docker-compose build
Building app
Step 1/6 : FROM golang:1.15-alpine
---> b3bc898ad092
Step 2/6 : WORKDIR /go/src/app
---> Running in 55f4bfc0b0e5
Removing intermediate container 55f4bfc0b0e5
---> bb957624bc5e
Step 3/6 : ADD ./app /go/src/app
---> 94a4c0aeb52e
Step 4/6 : RUN apk update && apk add --no-cache git && go get github.com/labstack/echo/... && go get github.com/pilu/fresh
---> Running in 2e16203c8eac
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
v3.12.3-65-g746e0b7bc7 [http://dl-cdn.alpinelinux.org/alpine/v3.12/main]
v3.12.3-62-gebf75fec7d [http://dl-cdn.alpinelinux.org/alpine/v3.12/community]
OK: 12756 distinct packages available
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
(1/5) Installing nghttp2-libs (1.41.0-r0)
(2/5) Installing libcurl (7.69.1-r3)
(3/5) Installing expat (2.2.9-r1)
(4/5) Installing pcre2 (10.35-r0)
(5/5) Installing git (2.26.2-r0)
Executing busybox-1.31.1-r16.trigger
OK: 22 MiB in 20 packages
Removing intermediate container 2e16203c8eac
---> dfceb18e2ebd
Step 5/6 : EXPOSE 8080
---> Running in 8353095ba65e
Removing intermediate container 8353095ba65e
---> fa4b48a798c4
Step 6/6 : CMD ["fresh"]
---> Running in a8c85cdb33ba
Removing intermediate container a8c85cdb33ba
---> e121e6032342
Successfully built e121e6032342
Successfully tagged go-docker_app:latest
go mod init
Then run the command docker-compose run --rm app go mod init
.
This will create a go.mod
in your app directory.
$ docker-compose run --rm app go mod init
Creating network "go-docker_default" with the default driver
Creating go-docker_app_run ... done
go: creating new go.mod: module app
docker-compose up
Finally, run the command ddocker-compose up
.
And go.sum
is also created and the container starts up.
$ docker-compose up
Creating go-docker_app_1 ... done
Attaching to go-docker_app_1
app_1 | 9:27:18 runner | InitFolders
app_1 | 9:27:18 runner | mkdir ./tmp
app_1 | 9:27:18 watcher | Watching .
app_1 | 9:27:18 main | Waiting (loop 1)...
app_1 | 9:27:18 main | receiving first event /
app_1 | 9:27:18 main | sleeping for 600 milliseconds
app_1 | 9:27:18 main | flushing events
app_1 | 9:27:18 main | Started! (5 Goroutines)
app_1 | 9:27:18 main | remove tmp/runner-build-errors.log: no such file or directory
app_1 | 9:27:18 build | Building...
app_1 | 9:27:34 runner | Running...
app_1 | 9:27:34 main | --------------------
app_1 | 9:27:34 main | Waiting (loop 2)...
app_1 | 9:27:34 app |
app_1 | ____ __
app_1 | / __/___/ / ___
app_1 | / _// __/ _ \/ _ \
app_1 | /___/\__/_//_/\___/ v3.3.10-dev
app_1 | High performance, minimalist Go web framework
app_1 | https://echo.labstack.com
app_1 | ____________________________________O/_______
app_1 | O\
app_1 | 9:27:34 app | ⇨ http server started on [::]:8080
In this state, access the following and if Hello world
is displayed, it is successful.
http://localhost:8080/
Next, I would like to build MySQL and connect it if I have time.