去年更新了两篇blog,记录了怎么搭建 vscode code server,以及配置golang和php环境的。 一年过去了,我对它们又做了一些更新,完善了golang debug、k8s源码开发、配置同步、ide增量build、docker等方面的支持。
一、为什么不用本地ide
Q:为什么不用本地ide,毕竟本地ide功能要强大很多。
A:客观原因是设备太多了。我有1台办公Win,4台开发机Linux,1台个人Mac,1台家庭Win,1个iPad。如此多的设备和平台,我不希望只给1-2台配置开发环境,太受束缚了。
以前的我在所有设备上都配置了开发环境,用的是IntelliJ IDEA。公司的主要用go,个人主要用php和vue,有时也做一些Android的。配环境总是让人苦恼的一件事,如果遇上重装系统,那就更痛苦了。
当然还有一个问题,就是运行ide其实是个挺费系统性能的事情,我目前还在使用 2012版的 Macbook Air,这样的配置跑个ide就烫手了。
Q:为什么不用JetBrains的Fleet?
A:因为Fleet出来太晚了,目前用vscode还是很好的。
二、dockerfile
另外特别要注意的是我是在x86_64的机器上使用的,所有的镜像均为intel的cpu。
镜像编译主要的难点还是区分在国内还是国外。先假定我们的环境到大部分地区都是可达的,我直接在dockerfile中下载go、php和docker_cli,所以有如下的dockerfile。
FROM kelvinblood/code-server:base-20210903
LABEL org.kelu.image.version.go=1.16.7
LABEL org.kelu.image.version.php=7.1.5
LABEL org.kelu.image.version.code-server=3.11.1
LABEL org.kelu.image.version.vscode=1.57.1
LABEL org.kelu.image.version.dockercli=19.03.14
ARG DOCKER_CLI_VERSION=19.03.14
ARG GOLANG_VERSION=1.16.7
ARG PHP_VERSION=7.1.5
ENV GOROOT /usr/local/go
ENV GOPATH /var/local/go
ENV PATH $PATH:$GOROOT/bin:$GOPATH/bin
ENV GO111MODULE on
ENV GOPROXY https://goproxy.cn
RUN \
GO_TGZ=go$(echo "$GOLANG_VERSION").linux-amd64.tar.gz && \
PHP_TGZ=php-${PHP_VERSION}.tar.gz && \
DOCKER_TGZ=docker-ce-cli_${DOCKER_CLI_VERSION}~3-0~debian-stretch_amd64.deb && \
echo "**** install tools ****" && \
apt-get update && \
apt-get install -y \
wget \
iputils-ping \
build-essential \
xinetd \
telnetd \
rsync \
jq \
python-pip && \
pip install pyyaml && \
cd /tmp && wget https://download.docker.com/linux/debian/dists/stretch/pool/stable/amd64/${DOCKER_TGZ} && \
dpkg -i *.deb && \
echo "**** install golang ****" && \
cd /tmp && wget https://golang.org/dl/${GO_TGZ} && \
tar zxvf ${GO_TGZ} >/dev/null 2>&1 && \
mv go ${GOROOT} && \
echo "**** go get for vscode ****" && \
go get golang.org/x/tools/gopls && \
go get golang.org/x/lint/golint && \
go get github.com/rogpeppe/godef && \
go get github.com/ramya-rao-a/go-outline && \
go get github.com/uudashr/gopkgs/v2/cmd/gopkgs && \
go get github.com/cweill/gotests/gotests && \
go get github.com/fatih/gomodifytags && \
go get github.com/josharian/impl && \
go get github.com/haya14busa/goplay/cmd/goplay && \
go get github.com/go-delve/delve/cmd/dlv && \
go get honnef.co/go/tools/cmd/staticcheck && \
go get github.com/spf13/cobra/cobra && \
cp /var/local/go/bin/dlv /var/local/go/bin/dlv-dap && \
echo "**** install php ****" && \
apt-get -y install libssl-dev libcurl4-openssl-dev libbz2-dev libjpeg-dev libpng-dev libgmp-dev libicu-dev libmcrypt-dev freetds-dev libxslt-dev libcurl3-dev autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c && \
ln -s /usr/lib/x86_64-linux-gnu/libsybdb.a /usr/lib/libsybdb.a && \
ln -s /usr/lib/x86_64-linux-gnu/libsybdb.so /usr/lib/libsybdb.so && \
ln -s /usr/lib/x86_64-linux-gnu/libct.a /usr/lib/libct.a && \
ln -s /usr/lib/x86_64-linux-gnu/libct.so /usr/lib/libct.so && \
ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h && \
ln -s /usr/include/x86_64-linux-gnu/curl /usr/include/curl && \
cd /tmp && wget http://am1.php.net/distributions/${PHP_TGZ} && \
tar -xzvf ${PHP_TGZ} && \
cd php-${PHP_VERSION} && \
./configure --prefix /usr/share/php7 --enable-fpm --enable-mbstring --enable-zip --enable-calendar --enable-bcmath --enable-exif --enable-intl --enable-opcache --enable-shmop --enable-soap --enable-sockets --with-fpm-user=www-data --with-fpm-group=www-data --with-pcre-regex --with-kerberos --with-openssl --with-mcrypt --with-zlib --with-bz2 --with-curl --with-gd --with-jpeg-dir=/usr/include/jpeg8 --with-png-dir=/usr/include/libpng12 --with-gettext --with-gmp --with-mhash --with-xsl && \
make clean && make && make install && make test && make clean && \
ln -s /usr/share/php7/sbin/php-fpm /usr/local/bin/php-fpm && \
ln -s /usr/share/php7/bin/php /usr/local/bin/php && \
cd .. && \
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer && \
echo "**** vscdoe tools ****" && \
/usr/local/bin/code-server --install-extension ms-ceintl.vscode-language-pack-zh-hans && \
/usr/local/bin/code-server --install-extension vscodevim.vim && \
/usr/local/bin/code-server --install-extension golang.Go && \
mkdir -p /config/extensions && \
mv /config/.local/share/code-server/extensions/* /config/extensions && \
echo "**** clean up ****" && \
apt-get clean && \
rm -rf \
/tmp/* \
/var/lib/apt/lists/* \
/var/tmp/*
主要做了几件工作:
- 配置常用的软件。有些是k8s编译必须的一些软件。
- go和相关go库的安装。有些是vscode支持和k8s编译必须的或者优化的库。
- php编译安装。
- vscode的插件安装。
如果你不想安装php或者go,或是想编译其他语言的,可以根据这份dockerfile做调整。
三、dockerfile(国内)
按以上的dockerfile,在国外的机器上是可以编译通过的。在国内build遇到了这几个问题,非常影响编译时候的心情💢:
- go/php/docker下载慢
- apt源update慢
pip install pyyaml
直接timeout了,(吐血
于是针对这几个问题,都做了相应的工作:
- 先在国外下好传到本地
- 换成国内163源
- 配置一个http proxy提供给pip使用
1. 下好go/php/docker文件
2个文件:一个负责下载,一个负责启动本地http服务器,方便dockerfile下载。
# download.sh
#!/bin/bash
DOCKER_CLI_VERSION=19.03.14
GOLANG_VERSION=1.16.7
PHP_VERSION=7.1.5
GO_TGZ=go$(echo "$GOLANG_VERSION").linux-amd64.tar.gz
PHP_TGZ=php-${PHP_VERSION}.tar.gz
DOCKER_TGZ=docker-ce-cli_${DOCKER_CLI_VERSION}~3-0~debian-stretch_amd64.deb
wget https://golang.org/dl/${GO_TGZ}
wget http://am1.php.net/distributions/${PHP_TGZ}
wget https://download.docker.com/linux/debian/dists/stretch/pool/stable/amd64/${DOCKER_TGZ}
# web.sh
#!/bin/bash
python -m SimpleHTTPServer 31415
2. 更换apt源
大致如下,详情我贴在文后
3. 创建一个http proxy 提供给pip使用
使用privoxy创建的,具体内容就不展示了,可以参考我这篇文章《linux 配置 privoxy 实现系统全局/自动代理》.
完成这三个步骤后的dockerfile如下,主要区别的地方就是增加了本地服务器地址和代理地址两个变量。
FROM kelvinblood/code-server:base-20210903
LABEL org.kelu.image.version.go=1.16.7
LABEL org.kelu.image.version.php=7.1.5
LABEL org.kelu.image.version.code-server=3.11.1
LABEL org.kelu.image.version.vscode=1.57.1
LABEL org.kelu.image.version.dockercli=19.03.14
ARG DOCKER_CLI_VERSION=19.03.14
ARG GOLANG_VERSION=1.16.7
ARG PHP_VERSION=7.1.5
ENV GOROOT /usr/local/go
ENV GOPATH /var/local/go
ENV PATH $PATH:$GOROOT/bin:$GOPATH/bin
ENV GO111MODULE on
ENV GOPROXY https://goproxy.cn
RUN \
LOCAL_WEB_SERVER=172.16.0.2:31415 && \
PROXY=172.16.0.2:8118 && \
GO_TGZ=go$(echo "$GOLANG_VERSION").linux-amd64.tar.gz && \
PHP_TGZ=php-${PHP_VERSION}.tar.gz && \
DOCKER_TGZ=docker-ce-cli_${DOCKER_CLI_VERSION}~3-0~debian-stretch_amd64.deb && \
echo "**** download from china ****" && \
echo "deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse" >/etc/apt/sources.list && \
echo "deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse" >>/etc/apt/sources.list && \
echo "deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
echo "deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse" >>/etc/apt/sources.list && \
echo "deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse" >>/etc/apt/sources.list && \
echo "**** install tools ****" && \
apt-get update && \
apt-get install -y \
wget \
iputils-ping \
build-essential \
xinetd \
telnetd \
rsync \
jq \
python-pip && \
# pip install pyyaml && \
pip install --proxy http://${PROXY} pyyaml && \
cd /tmp && wget http://${LOCAL_WEB_SERVER}/${DOCKER_TGZ} && \
dpkg -i *.deb && \
echo "**** install golang ****" && \
cd /tmp && wget http://${LOCAL_WEB_SERVER}/${GO_TGZ} && \
tar zxvf ${GO_TGZ} >/dev/null 2>&1 && \
mv go ${GOROOT} && \
echo "**** go get for vscode ****" && \
go get golang.org/x/tools/gopls && \
go get golang.org/x/lint/golint && \
go get github.com/rogpeppe/godef && \
go get github.com/ramya-rao-a/go-outline && \
go get github.com/uudashr/gopkgs/v2/cmd/gopkgs && \
go get github.com/cweill/gotests/gotests && \
go get github.com/fatih/gomodifytags && \
go get github.com/josharian/impl && \
go get github.com/haya14busa/goplay/cmd/goplay && \
go get github.com/go-delve/delve/cmd/dlv && \
go get honnef.co/go/tools/cmd/staticcheck && \
go get github.com/spf13/cobra/cobra && \
cp /var/local/go/bin/dlv /var/local/go/bin/dlv-dap && \
echo "**** install php ****" && \
apt-get -y install libssl-dev libcurl4-openssl-dev libbz2-dev libjpeg-dev libpng-dev libgmp-dev libicu-dev libmcrypt-dev freetds-dev libxslt-dev libcurl3-dev autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c && \
ln -s /usr/lib/x86_64-linux-gnu/libsybdb.a /usr/lib/libsybdb.a && \
ln -s /usr/lib/x86_64-linux-gnu/libsybdb.so /usr/lib/libsybdb.so && \
ln -s /usr/lib/x86_64-linux-gnu/libct.a /usr/lib/libct.a && \
ln -s /usr/lib/x86_64-linux-gnu/libct.so /usr/lib/libct.so && \
ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h && \
ln -s /usr/include/x86_64-linux-gnu/curl /usr/include/curl && \
cd /tmp && wget http://${LOCAL_WEB_SERVER}/${PHP_TGZ} && \
tar -xzvf ${PHP_TGZ} && \
cd php-${PHP_VERSION} && \
./configure --prefix /usr/share/php7 --enable-fpm --enable-mbstring --enable-zip --enable-calendar --enable-bcmath --enable-exif --enable-intl --enable-opcache --enable-shmop --enable-soap --enable-sockets --with-fpm-user=www-data --with-fpm-group=www-data --with-pcre-regex --with-kerberos --with-openssl --with-mcrypt --with-zlib --with-bz2 --with-curl --with-gd --with-jpeg-dir=/usr/include/jpeg8 --with-png-dir=/usr/include/libpng12 --with-gettext --with-gmp --with-mhash --with-xsl && \
make clean && make && make install && make test && make clean && \
ln -s /usr/share/php7/sbin/php-fpm /usr/local/bin/php-fpm && \
ln -s /usr/share/php7/bin/php /usr/local/bin/php && \
cd .. && \
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer && \
echo "**** vscdoe tools ****" && \
/usr/local/bin/code-server --install-extension ms-ceintl.vscode-language-pack-zh-hans && \
/usr/local/bin/code-server --install-extension vscodevim.vim && \
/usr/local/bin/code-server --install-extension golang.Go && \
mkdir -p /config/extensions && \
mv /config/.local/share/code-server/extensions/* /config/extensions && \
echo "**** clean up ****" && \
apt-get clean && \
rm -rf \
/tmp/* \
/var/lib/apt/lists/* \
/var/tmp/*
四、运行
整个ide镜像弄好了,对于开发者来说还有一个需要解决的,我平时使用肯定是需要增加插件、一些自定义的配置,还有golang开发时go mod需要下载库之类的,不能镜像一挂就什么都没了吧。。。
所以这里需要将它们提取出来,我分成了两个步骤,一个是build images时提取,另一个是docker-compose拉起镜像。
1. build.sh
#!/bin/bash
rm -rf ./config ./go
tag=$(date +%Y%m%d)
docker build -t kelvinblood/code-server:release-$tag .
id=$(docker create kelvinblood/code-server:release-$tag)
docker cp $id:/config .
docker cp $id:/var/local/go .
docker rm -v $id
touch settings.json
另外,我个人使用的个人配置文件如下:
# settings.json
{
"workbench.colorTheme": "Solarized Dark",
"editor.fontSize": 18,
"go.testTimeout": "60s",
"go.toolsManagement.autoUpdate": true
}
2. docker-compose.yml
version: "2.1"
services:
code-server:
image: kelvinblood/code-server:release-20220127
container_name: code-server
hostname: vscode
network_mode: bridge
environment:
- PUID=0
- PGID=0
- TZ=Asia/Shanghai
- PASSWORD= #optional
# - HASHED_PASSWORD= #optional
# - SUDO_PASSWORD= #optional
# - SUDO_PASSWORD_HASH= #optional
- PROXY_DOMAIN=kelu.org #optional
volumes:
- ./gophp/config:/config
- ./gophp/go:/var/local/go
- ./gophp/settings.json:/config/data/User/settings.json:rw
- /var/run/docker.sock:/var/run/docker.sock:rw
- /root/Workspace:/root/Workspace
restart: unless-stopped
运行后界面如下: