How to create a Go environment with Docker

Posted in php, blog on November 4, 2020 by Henk Verlinde ‐ 1 min read

How to create a Go environment with Docker

at first

We will assume that you already have a Docker environment.

It doesn’t matter if you haven’t read it, but I recommend reading the following articles as well. [Create Docker+Go environment]

Create DockerFile

Create a working directory, create [DockerFile] and copy and paste the following

# specify base Docker image
FROM golang:latest
# create a working directory inside the container
RUN mkdir /go/src/work
# specify the directory when logging into the container
WORKDIR /go/src/work
# install gin framework
RUN go get github.com/gin-gonic/gin
# Launch GoApp
CMD ["go","run","main.go"]
# migrate host files to container working directory
ADD . /go/src/work

create docker-compose.yml

Create a [docker-compose.yml] file in the same directory and copy and paste the following

version: '3' # Specify the version of the compose file
services:
  app: # service name
    build: . # Specify the directory containing the Dockerfile used for building
    tty: true # Container startup persistence
    volumes:
      - .:/go/src/work # Specify mount directory
    ports:
      - 8080: 8080

Command execution with CLI

curl https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go > main.go

docker-compose build

docker-compose up -d

Try visiting http://localhost:8080.