Go (Echo) Go Modules × Build development environment with Docker

Introduction

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.

Final directory structure

.
├── app
│   ├── Dockerfile
│   ├── go.mod
│   ├── go.sum
│   └── main.go
└── docker-compose.yml

GitHub

For those in a hurry

You can do this by downloading the code from here and docker-compose up.

Dockerfile creation

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"]

Create main.go

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"))
}

Create docker-compose.yml

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/

at the end

Next, I would like to build MySQL and connect it if I have time.

Recommended Posts

Go (Echo) Go Modules × Build development environment with Docker
Build Django + NGINX + PostgreSQL development environment with Docker
[Python] Build a Django development environment with Docker
Build a development environment with Poetry Django Docker Pycharm
Build a go environment using Docker
Build Mysql + Python environment with docker
Build PyPy execution environment with Docker
Rebuild Django's development environment with Docker! !! !! !!
[Memo] Build a development environment for Django + Nuxt.js with Docker
Build a Go development environment with VS Code's Remote Containers
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Easily build a development environment with Laragon
Build Jupyter Lab (Python) environment with Docker
[Linux] Build a jenkins environment with Docker
Build NGINX + NGINX Unit + MySQL environment with Docker
[Linux] Build a Docker environment with Amazon Linux 2
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build a C language development environment with a container
Build a Django development environment with Doker Toolbox
Build PyPy and Python execution environment with Docker
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
[Go + Gin] I tried to build a Docker environment
Build a machine learning application development environment with Python
Build a lightweight Fast API development environment using Docker
Build python3 environment with ubuntu 16.04
Prepare python3 environment with Docker
Build python environment with direnv
[ev3dev × Python] Build ev3dev development environment
Environment construction, Build -Go edition-
Create a GO development environment with [Mac OS Big Sur]
Build a comfortable development environment with VSCode x Remote Development x Pipenv
How to build a python2.7 series development environment with Vagrant
Create a simple Python development environment with VSCode & Docker Desktop
Build GPU environment with GCP and kaggle official image (docker)
Create a Todo app with Django ① Build an environment with Docker
Build a Flask development environment at low cost using Docker
Build python virtual environment with virtualenv
GUI development with Fyne by Go
Build a deb file with Docker
Tips for running Go with docker
Google App Engine development with Docker
Build Flask environment with Dockerfile + docker-compose.yml
Build IPython Notebook environment with boot2docker
Prepare Python development environment with Atom
Data science environment construction with Docker
Application development with Docker + Python + Flask
Prepare the development environment with anyenv
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Create a simple Python development environment with VS Code and Docker
Learning history to participate in team application development with Python ~ Build Docker / Django / Nginx / MariaDB environment ~
[Development environment] Python with Xcode [With screen transition]
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
Build a Tensorflow environment with Raspberry Pi [2020]
Let's try gRPC with Go and Docker
Build a Fast API environment with docker-compose
Get a local DynamoDB environment with Docker
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
Create Python + uWSGI + Nginx environment with Docker
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
Build Python environment with Anaconda on Mac