From 3231a27e59f21d82a8859244298bb8348e04018f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TZ=20=7C=20=E5=A4=A9=E7=8C=AA?= Date: Thu, 3 Nov 2022 15:05:17 +0000 Subject: [PATCH] feat: devcontainer with codespaces --- .devcontainer/devcontainer.json | 43 ++++++++++++++++++ .devcontainer/scripts/postCreateCommand.sh | 4 ++ .gitignore | 1 + Dockerfile | 7 ++- docker-compose.yml | 52 +++++++++++++++------- test/TestUtil.ts | 2 +- 6 files changed, 92 insertions(+), 17 deletions(-) create mode 100644 .devcontainer/devcontainer.json create mode 100755 .devcontainer/scripts/postCreateCommand.sh diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000..2a5c2d2c --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,43 @@ +// Update the VARIANT arg in docker-compose.yml to pick a Node.js version +{ + "name": "Node.js && Redis && MySQL", + "dockerComposeFile": "../docker-compose.yml", + "service": "app", + "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}", + + // Configure tool-specific properties. + "customizations": { + // Configure properties specific to VS Code. + "vscode": { + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + ] + } + }, + + // Forwards ports + "forwardPorts": [ + 8080 + ], + "portsAttributes": { + "8080": { + "label": "Adminer", + "onAutoForward": "notify" + } + }, + + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": [ + "bash .devcontainer/scripts/postCreateCommand.sh" + ], + + // Container Env + "containerEnv": { + "MYSQL_HOST": "mysql", + "MYSQL_USER": "root", + "MYSQL_PASSWORD": "root" + }, + + // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. + "remoteUser": "node" +} diff --git a/.devcontainer/scripts/postCreateCommand.sh b/.devcontainer/scripts/postCreateCommand.sh new file mode 100755 index 00000000..d21676bb --- /dev/null +++ b/.devcontainer/scripts/postCreateCommand.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +pnpm i +socat TCP4-LISTEN:8080,reuseaddr,fork TCP:adminer:8080 & diff --git a/.gitignore b/.gitignore index 13fa0f64..f416fbbe 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ lerna-debug.log* .npmrc package-lock.json +pnpm-lock.yaml config/config.prod.ts config/**/*.js diff --git a/Dockerfile b/Dockerfile index 28510d29..69882492 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,12 @@ WORKDIR /usr/src/app # Install app dependencies COPY . . -RUN npm install -g npminstall --registry=https://registry.npmmirror.com \ +# NPM Mirror +# npm install -g npminstall --registry=https://registry.npmmirror.com +# apk add --no-cache socat \ +RUN apt-get update \ + && apt-get -y install socat \ + && npm install -g npminstall \ && npminstall -c \ && npm run tsc diff --git a/docker-compose.yml b/docker-compose.yml index 8340d2cf..b3ff9dab 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,30 @@ -version: '3.6' +version: '3.8' services: + app: + build: + context: . + dockerfile: Dockerfile + volumes: + - ../..:/workspaces:cached + + # Overrides default command so things don't shut down after the process ends. + command: sleep infinity + depends_on: + - mysql + - redis + networks: + - cnpm + + # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function. + # networks: + # - cnpm + + # Uncomment the next line to use a non-root user for all processes. + # user: node + + # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. + # (Adding the "ports" property to this file will not forward from a Codespace.) + redis: image: redis:6-alpine # command: redis-server --appendonly yes --requirepass cnpm @@ -7,7 +32,7 @@ services: volumes: - cnpm-redis:/data ports: - - 6379:6379 + - 6379 networks: - cnpm @@ -16,7 +41,7 @@ services: command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci restart: always environment: - MYSQL_ROOT_PASSWORD: + MYSQL_ROOT_PASSWORD: root MYSQL_ALLOW_EMPTY_PASSWORD: 'yes' # MYSQL_DATABASE: 'cnpmcore_unittest' MYSQL_USER: user @@ -25,33 +50,30 @@ services: - cnpm-mysql:/var/lib/mysql # - ./conf.d/mysql/:/etc/mysql/conf.d # - ./init.d/mysql/:/docker-entrypoint-initdb.d + - ./sql:/docker-entrypoint-initdb.d/sql + - ./prepare-database.sh:/docker-entrypoint-initdb.d/init.sh ports: - - 3306:3306 + - 3306 networks: - cnpm # database explorer - phpmyadmin: - image: phpmyadmin + adminer: + image: adminer restart: always environment: - MYSQL_ROOT_PASSWORD: - MYSQL_ALLOW_EMPTY_PASSWORD: 'yes' - MYSQL_USER: user - MYSQL_PASSWORD: pass - PMA_HOST: 'mysql' + ADMINER_DEFAULT_DB_HOST: 'mysql' + depends_on: + - mysql ports: - - 8080:80 + - 8080 networks: - cnpm - depends_on: - - mysql volumes: cnpm-redis: cnpm-mysql: - networks: cnpm: name: cnpm diff --git a/test/TestUtil.ts b/test/TestUtil.ts index 1c29c5ec..cd8c2efc 100644 --- a/test/TestUtil.ts +++ b/test/TestUtil.ts @@ -49,7 +49,7 @@ export class TestUtil { host: process.env.MYSQL_HOST || '127.0.0.1', port: process.env.MYSQL_PORT || 3306, user: process.env.MYSQL_USER || 'root', - password: process.env.MYSQL_PASSWORD, + password: process.env.MYSQL_PASSWORD || '', multipleStatements: true, }; }