liuyuqi-dellpc 9 months ago
parent
commit
d2ea131a08
4 changed files with 103 additions and 0 deletions
  1. 4 0
      .devcontainer/.env
  2. 27 0
      .devcontainer/Dockerfile
  3. 26 0
      .devcontainer/devcontainer.json
  4. 46 0
      .devcontainer/docker-compose.yml

+ 4 - 0
.devcontainer/.env

@@ -0,0 +1,4 @@
+POSTGRES_USER=postgres
+POSTGRES_PASSWORD=postgres
+POSTGRES_DB=postgres
+POSTGRES_HOSTNAME=localhost

+ 27 - 0
.devcontainer/Dockerfile

@@ -0,0 +1,27 @@
+FROM mcr.microsoft.com/devcontainers/go:1-1.20-bullseye
+
+# [Optional] Uncomment this section to install additional OS packages.
+# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
+#     && apt-get -y install --no-install-recommends <your-package-list-here>
+
+# [Optional] Uncomment the next lines to use go get to install anything else you need
+# USER vscode
+# RUN go get -x <your-dependency-or-tool>
+# USER root
+
+# [Optional] Uncomment this line to install global node packages.
+# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
+
+RUN GO111MODULE=on go install github.com/zeromicro/go-zero/tools/goctl@latest
+
+RUN goctl env check --install --verbose --force
+
+# 国内设置代理
+# go env -w GO111MODULE=on
+# go env -w GOPROXY=https://goproxy.cn,direct
+
+# mkdir demo1 && cd demo1
+# go mod init demo1_module
+# go get -u github.com/zeromicro/go-zero@latest
+
+# vscode 安装 goctl 插件  xiaoxin-technology.goctl

+ 26 - 0
.devcontainer/devcontainer.json

@@ -0,0 +1,26 @@
+// For format details, see https://aka.ms/devcontainer.json. For config options, see the
+// README at: https://github.com/devcontainers/templates/tree/main/src/go-postgres
+{
+	"name": "Go & PostgreSQL",
+	"dockerComposeFile": "docker-compose.yml",
+	"service": "app",
+	"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
+	// Features to add to the dev container. More info: https://containers.dev/features.
+	// "features": {},
+	// Configure tool-specific properties.
+	// "customizations": {},
+	// Use 'forwardPorts' to make a list of ports inside the container available locally.
+	// "forwardPorts": [5432],
+	// Use 'postCreateCommand' to run commands after the container is created.
+	// "postCreateCommand": "go version",
+	// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
+	// "remoteUser": "root"
+	"customizations": {
+		"vscode": {
+			"extensions": [
+				"xiaoxin-technology.goctl",
+				"golang.go"
+			]
+		}
+	}
+}

+ 46 - 0
.devcontainer/docker-compose.yml

@@ -0,0 +1,46 @@
+version: '3.8'
+
+volumes:
+  postgres-data:
+
+services:
+  app:
+    build: 
+      context: .
+      dockerfile: Dockerfile
+    env_file:
+        # Ensure that the variables in .env match the same variables in devcontainer.json
+        - .env
+
+    volumes:
+      - ../..:/workspaces:cached
+      
+    # Overrides default command so things don't shut down after the process ends.
+    command: sleep infinity
+
+    # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
+    network_mode: service:mysql
+
+    # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. 
+    # (Adding the "ports" property to this file will not forward from a Codespace.)
+
+  mysql:
+    image: postgres:latest
+    restart: unless-stopped
+    volumes:
+      - postgres-data:/var/lib/postgresql/data
+    env_file:
+      # Ensure that the variables in .env match the same variables in devcontainer.json
+      - .env
+
+  
+    # Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
+    # (Adding the "ports" property to this file will not forward from a Codespace.)
+  
+  mongodb:
+    image: mongo:latest
+    restart: unless-stopped
+    volumes:
+      - mongodb-data:/data/db
+    env_file:
+      - .env