[Go + Gin] I tried to build a Docker environment

Introduction

We have built a development environment using Dokcer when using Go and Gin. I'm using docker-compose. The DB used is mysql, and the OR mapper assumes the use of Gorm.

TL;DR It has the following directory structure. Dockerfile and docker-compose.yml.

** Directory structure **

├── docker
│   ├── docker.mysql.env
│   └── go
│       └── Dockerfile
├── docker-compose.yml
└── src
    └── app

docker/go/Dockerfile


FROM golang:1.10.3-alpine3.8

COPY src/app /go/src/app/

WORKDIR /go/src/app/

RUN apk update \
  && apk add --no-cache git \
  && go get github.com/gin-gonic/gin \
  && go get github.com/jinzhu/gorm \
  && go get github.com/go-sql-driver/mysql

EXPOSE 8080

docker/docker.mysql.env

MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=go-gin-app
MYSQL_USER=root
MYSQL_PASSWORD=password

docker-compose.yml



services:
  app:
    container_name: app
    build:
      context: .
      dockerfile: ./docker/go/Dockerfile
    ports:
      - 3000:3000
    links:
      - database
    tty:
      true
    volumes:
      - ./src/app:/go/src/app

  database:
    restart: always
    image: mysql:5.7
    ports:
      - 3308:3306
    volumes:
      - mysql-datavolume:/var/lib/mysql
    env_file:
      - docker/docker.mysql.env

volumes:
  mysql-datavolume:
    driver: local

Commentary

Dockerfile

This time I will only create a Go Dockerfile and mysql will specify in docker-compose.yml to use the base image.


FROM golang:1.10.3-alpine3.8

COPY src/app /go/src/app/

WORKDIR /go/src/app/

RUN apk update \
  && apk add --no-cache git \
  && go get github.com/gin-gonic/gin \
  && go get github.com/jinzhu/gorm \
  && go get github.com/go-sql-driver/mysql

EXPOSE 8080

The file directly under src / app is specified as the volume.

Also, since the base image of aliphine is specified, Gin and Gorm related packages are installed with apk.

docker-compose.yml

services:
  app:
    container_name: app
    build:
      context: .
      dockerfile: ./docker/go/Dockerfile
    ports:
      - 3000:3000
    links:
      - database
    tty:
      true
    volumes:
      - ./src/app:/go/src/app

  database:
    restart: always
    image: mysql:5.7
    ports:
      - 3307:3306
    volumes:
      - mysql-datavolume:/var/lib/mysql
    env_file:
      - docker/docker.mysql.env

volumes:
  mysql-datavolume:
    driver: local

I created the mysql env file separately, so specify it with ʻenv_file. Port 3306 is often used by local mysql, so 3307` is specified.

Operation check

Create main.go directly under src / app and write the code to output the familiar hello world.

package main

import "fmt"

func main() {
        fmt.Println("Hello, world")
}

Then, output hello world with the following command.

docker-compose exec app go run main.go

Was hello world output?

At the end

It was surprisingly easy to build a Go + Gin environment.

After all, using Docker makes it easier to build an environment!

Recommended Posts

[Go + Gin] I tried to build a Docker environment
Build a go environment using Docker
I want to build a Python environment
I tried to build a super-resolution method / ESPCN
I tried to build a super-resolution method / SRCNN ①
I tried to build a super-resolution method / SRCNN ③
I tried to build a super-resolution method / SRCNN ②
I tried to build a Mac Python development environment with pythonz + direnv
How to build a Django (python) environment on docker
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
I tried to create a linebot (implementation)
I tried to create a server environment that runs on Windows 10
How to build a sphinx translation environment
When I tried to build a Rails environment on WSL2 (Ubuntu 20.04LTS), I stumbled and fell.
[Linux] Build a jenkins environment with Docker
I tried to make a Web API
I tried to build a service that sells machine-learned data at explosive speed with Docker
[Linux] Build a Docker environment with Amazon Linux 2
I tried to make a mechanism of exclusive control with Go
I tried to draw a system configuration diagram with Diagrams on Docker
Go (Echo) Go Modules × Build development environment with Docker
[Python] Build a Django development environment with Docker
I tried to generate a random character string
I tried to make a ○ ✕ game using TensorFlow
I tried to debug.
I tried to paste
Flutter in Docker-How to build and use a Flutter development environment inside a Docker container
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
When I tried to create a virtual environment with Python, it didn't work
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
I tried to make a "fucking big literary converter"
I tried to create a table only with Django
I tried to draw a route map with Python
I tried to implement a pseudo pachislot in Python
I tried to implement a recommendation system (content-based filtering)
I tried to build ML Pipeline with Cloud Composer
I tried to automatically generate a password with Python3
CTF beginner tried to build a problem server (web) [Problem]
How to build a development environment for TensorFlow (1.0.0) (Mac)
I added a function to CPython (build & structure grasp)
Build a development environment with Poetry Django Docker Pycharm
How to build a Python environment on amazon linux 2
I tried to draw a configuration diagram using Diagrams
Build a lightweight Fast API development environment using Docker
[Django] Use VS Code + Remote Containers to quickly build a Django container (Docker) development environment.
[AWS] Development environment version that tried to build a Python environment with eb [Elastic Beanstalk]
Memo A beginner tried to build a Java environment and Japaneseize it on Ubuntu 18.04.2 LTS.
I tried to build an environment for machine learning with Python (Mac OS X)
I tried to learn PredNet
I tried to organize SVM.
Build a LAMP environment [CentOS 7]
I tried to implement PCANet
I tried to reintroduce Linux
Build a machine learning environment
I tried to introduce Pylint
I tried to summarize SparseMatrix
Build a Python environment offline
Environment construction, Build -Go edition-
I tried to touch jupyter
I tried to implement StarGAN (1)
I tried to implement a volume moving average with Quantx