制作vscode远程容器开发镜像(支持docker和k8s远程容器开发)
为什么要弄远程容器开发镜像
- 越来越少的时间在自己家里搞机,总是东走西走,电脑也不一定用自己的,所以,在一个新地方就重新搭环境,不合理。
- 机器多了,可以折腾
- 巨硬的vscode太好用了,java,golang, nodejs全部可以在上面开发
远程开发镜像中有什么
java开发环境,golang开发环境,nodejs开发环境
同时支持docker内嵌docker
准备
- 搭建一个ng服务,提前缓存好jdk等软件,加速构建过程,局域网dns: ng-dev-dependency.bee.anarckk.me
- 搭建好verdaccio,这个适合我用,别人不一定需要,局域网dns: verdaccio.bee.anarckk.me
- 内网弄好dns解析,软路由上配一下就行
制作基础镜像fh-dev
dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| FROM bee.anarckk.me:5000/anarckk/ubuntu:20.04-git2.24.0-docker20.10.19
ARG NG_DEV_DEPENDENCY=ng-dev-dependency.bee.anarckk.me ENV JAVA_HOME /usr/local/jdk1.8.0_371 ENV MAVEN_HOME /usr/local/apache-maven-3.9.3 ENV PATH="${PATH}:/usr/local/go/bin:/usr/local/node-v18.16.0-linux-x64/bin:${MAVEN_HOME}/bin:${JAVA_HOME}/bin:/usr/local/gradle-8.2.1/bin" ENV GOPROXY https://proxy.golang.com.cn
RUN echo 'install gradle' \ && cd /usr/local \ && curl -L -O http://${NG_DEV_DEPENDENCY}/gradle/gradle-8.2.1-bin.zip \ && unzip gradle-8.2.1-bin.zip \ && rm -rf gradle-8.2.1-bin.zip \ && echo 'install maven' \ && cd /usr/local \ && curl -L -O http://${NG_DEV_DEPENDENCY}/apache-maven-3.9.3-bin.tar.gz \ && tar xzf apache-maven-3.9.3-bin.tar.gz \ && rm -rf apache-maven-3.9.3-bin.tar.gz \ && echo 'install java8' \ && curl -L -O http://${NG_DEV_DEPENDENCY}/jdk-8u371-linux-x64.tar.gz \ && tar zxf jdk-8u371-linux-x64.tar.gz \ && rm -rf jdk-8u371-linux-x64.tar.gz \ && echo 'install java17' \ && curl -L -O http://${NG_DEV_DEPENDENCY}/openjdk-17.0.2_linux-x64_bin.tar.gz \ && tar zxf openjdk-17.0.2_linux-x64_bin.tar.gz \ && rm -rf openjdk-17.0.2_linux-x64_bin.tar.gz \ && echo 'install nodejs' \ && cd /usr/local \ && curl -L -O http://${NG_DEV_DEPENDENCY}/node-v18.16.0-linux-x64.tar.xz \ && tar -xvf node-v18.16.0-linux-x64.tar.xz \ && rm -rf node-v18.16.0-linux-x64.tar.xz \ && echo 'install npm@^9.7.2' \ && npm install -g npm@^9.7.2 \ && echo 'install nrm' \ && npm i -g nrm open@8.4.2 --save \ && nrm add verdaccio http://verdaccio.bee.anarckk.me/ \ && nrm use verdaccio \ && echo 'install yarn' \ && npm install -g yarn@^1.22.19 \ && echo 'install golang' \ && cd /usr/local \ && curl -L -O http://${NG_DEV_DEPENDENCY}/go1.20.4.linux-amd64.tar.gz \ && rm -rf /usr/local/go \ && tar -C /usr/local -xzf go1.20.4.linux-amd64.tar.gz \ && rm -rf go1.20.4.linux-amd64.tar.gz
VOLUME [ "/root/.vscode-server", "/root/.m2", "/root/.ssh","/root/go/pkg", "/root/go/bin", "/root/.npm" ]
|
构建命令
1 2
| docker build -t bee.anarckk.me:5000/anarckk/fh-dev:latest . \ && docker push bee.anarckk.me:5000/anarckk/fh-dev:latest
|
已推送到dockerhub上
https://hub.docker.com/repository/docker/anarckk/fh-dev
在基础镜像上制作业务镜像
dockerfile
业务镜像主要在基础镜像上增加自定义maven配置,自定义golang的私有依赖仓库配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| FROM bee.anarckk.me:5000/anarckk/fh-dev:latest
ARG GIT_NAME=michael ARG GIT_EMAIL=michael@gmail.com
ARG MVN_SETTINGS=settings.xml ENV GOINSECURE gogs.bee.anarckk.me ENV GONOPROXY gogs.bee.anarckk.me ENV GONOSUMDB gogs.bee.anarckk.me ENV GOPRIVATE gogs.bee.anarckk.me
RUN git config --global user.name $GIT_NAME \ && git config --global user.email $GIT_EMAIL \ && ln -s /usr/bin/python3 /usr/bin/python
COPY ${MVN_SETTINGS} /usr/local/apache-maven-3.9.3/conf/settings.xml
|
构建命令
1 2 3 4 5
| docker build --no-cache -t bee.anarckk.me:5000/anarckk/my-dev2:latest \ --build-arg GIT_NAME=anarckk \ --build-arg GIT_EMAIL=anarckk@gmail.com \ --build-arg MVN_SETTINGS=settings.xml . \ && docker push bee.anarckk.me:5000/anarckk/my-dev2:latest
|
docker-compose.yaml运行脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| version: "3"
services: my-dev2: image: bee.anarckk.me:5000/anarckk/my-dev2:latest container_name: my-dev2 restart: unless-stopped networks: custom-bridge: volumes: - /workdir/data2/dev/my-dev/workspace:/workdir/data2/dev/my-dev/workspace - ./vscode-server:/root/.vscode-server - ../common/m2:/root/.m2 - ../common/ssh:/root/.ssh - ../common/pkg:/root/go/pkg - ../common/go-bin:/root/go/bin - ../common/npm-cache:/root/.npm - /var/run/docker.sock:/var/run/docker.sock - /etc/localtime:/etc/localtime:ro - /etc/timezone:/etc/timezone:ro environment: - TZ=Asia/Shanghai logging: options: max-size: "10m" stdin_open: true privileged: true user: "root" networks: custom-bridge: external: true
|
同时,这个镜像那个可以k8s远程容器开发
这个技术学会的太晚了,早一年学会这个技术,就不用telepresence折腾的麻烦,远程到容器中开发,对于调试k8s二开的应用来说,方便的很。
k8s1-dev.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| apiVersion: v1 kind: Namespace metadata: name: k8s1-dev-ns spec: finalizers: - kubernetes
---
apiVersion: v1 kind: ServiceAccount metadata: labels: k8s-app: k8s1-dev name: k8s1-dev namespace: k8s1-dev-ns
---
apiVersion: v1 kind: Pod metadata: name: k8s1-dev namespace: k8s1-dev-ns labels: app: k8s1-dev spec: containers: - name: k8s1-dev image: bee.anarckk.me:5000/anarckk/my-dev2:latest stdin: true tty: true volumeMounts: - mountPath: /workdir/data1/k8s1-dev/workspace name: workspace - mountPath: /root/.vscode-server name: vscode-server - mountPath: /root/go/pkg name: go-pkg - mountPath: /root/go/bin name: go-bin - mountPath: /root/.ssh name: ssh - mountPath: /root/.kube name: kube resources: limits: cpu: '2' memory: 3Gi requests: cpu: 500m memory: 500Mi nodeName: k8s1 volumes: - name: workspace hostPath: path: /workdir/data1/k8s1-dev/workspace type: DirectoryOrCreate - name: vscode-server hostPath: path: /workdir/data1/k8s1-dev/common/vscode-server type: DirectoryOrCreate - name: go-pkg hostPath: path: /workdir/data1/k8s1-dev/common/go-pkg type: DirectoryOrCreate - name: go-bin hostPath: path: /workdir/data1/k8s1-dev/common/go-bin type: DirectoryOrCreate - name: ssh hostPath: path: /workdir/data1/k8s1-dev/common/ssh type: DirectoryOrCreate - name: kube hostPath: path: /workdir/data1/k8s1-dev/common/.kube type: Directory serviceAccountName: k8s1-dev
--- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: k8s1-dev-admin roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: k8s1-dev namespace: k8s1-dev-ns
|
运行这个脚本
1
| kubectl create -f k8s1-dev.yaml
|