本地搭建playground

本文主要是记录我搭建go playground的步骤。

1、安装docker

如果你使用的Ubuntu,docker的安装步骤可以参见这里,这是我之前写的在Ubuntu18.04下安装fabric,其中有docker的安装步骤,这里就不再赘述了。

CentOS下安装docker的,可以参见这里。与Ubuntu不同的是,CentOS需要自己手动安装docker-compose,可以从github.com下载对应系统的compose。

2、安装runsc

wget https://storage.googleapis.com/gvisor/releases/nightly/latest/runsc
chmod +x runsc
sudo mv runsc /usr/local/bin

配置docker使用runsc,需要在/etc/docker/daemon.json中添加如下内容:

{
    "runtimes": {
        "runsc": {
            "path": "/usr/local/bin/runsc"
        }
    }
}

之后重启docker:

sudo systemctl restart docker

3、安装playground

这里使用的golang官方提供的playground,可以从这里下载。

按照README.md中的指导就可以在本地构建出可运行的playground。

Tips:因为构建dock二镜像用到debain使用的是官方的源,国内访问速度很慢,修改Dockerfile使用国内的源替换
Tips:国内环境的话,还需要修改镜像中的GOPROXY,使用GOPROXY=https://goproxy.io,direct代替ENV GOPROXY=https://proxy.golang.org,否则在执行go mod download时会失败。

4、启动playground

compose文件示例如下:

version: '2'
services: 
  sandbox_dev:
    image: golang/playground-sandbox:latest
    networks: 
      - sandnet
    command: sh -c '/usr/local/bin/play-sandbox'
    ports: 
      - 8080:80
    volumes: 
      - /var/run/docker.sock:/var/run/docker.sock

  play_dev:
    image: golang/playground:latest
    environment:
      - SANDBOX_BACKEND_URL=http://playground_sandbox_dev_1/run
    networks: 
      - sandnet
    command: sh -c '/app/playground'
    ports: 
      - 8081:8080
    volumes: 
      - /var/run/docker.sock:/var/run/docker.sock

networks: 
  sandnet:

启动:

docker-compose -f docker-compose.yaml up

附图:

浏览器访问:http://localhost:8081

原文地址:https://www.cnblogs.com/lianshuiwuyi/p/14288387.html