打开浏览器,即可进行Golang程序开发,开发体验可媲美桌面版的vs code,一致的开发环境(基于Ubuntu镜像)。
安装docker
-
删除旧版本
for pkg in docker.io docker-doc docker-compose \ podman-docker containerd runc; \ do sudo apt-get remove $pkg; done
-
设置apt 仓库
# Add Docker's official GPG key: sudo apt-get update sudo apt-get install ca-certificates curl sudo install -m 0755 -d /etc/apt/keyrings sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc # Add the repository to Apt sources: echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \ https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update
-
安装docker
sudo apt-get install docker-ce docker-ce-cli \ containerd.io docker-buildx-plugin docker-compose-plugin
-
验证安装
sudo docker run hello-world
-
将用户添加到docker组
sudo groupadd docker sudo usermod -aG docker $USER newgrp docker
使用docker创建开发服务器
-
创建项目文件夹
mkdir code-server && cd code-server
-
创建Dockerfile
FROM codercom/code-server:ubuntu USER root # 修改源 RUN sed -i 's#http://ports.ubuntu.com/ubuntu-ports/#https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/#g' /etc/apt/sources.list RUN apt update -y && apt upgrade -y # 设置时区 ENV TZ="Asia/Shanghai" RUN DEBIAN_FRONTEND=noninteractive apt-get install -yq tzdata && \ ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && \ dpkg-reconfigure -f noninteractive tzdata USER 1000 ENV USER=coder WORKDIR /home/coder RUN sudo apt install build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev curl \ libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev -y # 安装go ENV GO111MODULE=on \ GOPROXY=https://goproxy.cn,direct RUN ARCH=$(dpkg --print-architecture) && echo $ARCH && wget https://go.dev/dl/go1.22.2.linux-$ARCH.tar.gz \ && sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.22.2.linux-$ARCH.tar.gz \ && export PATH=$PATH:/usr/local/go/bin \ && echo 'export PATH=$PATH:/usr/local/go/bin' >> .bashrc \ && rm go1.22.2.linux-$ARCH.tar.gz RUN sudo apt install vim tree -y # 修改提示符显示最后一个文件夹 RUN sed -i 's/\(PS1.*\)\\w/\1\\W/g' ~/.bashrc
-
创建docker-comose.yaml
services: code-server: build: . container_name: code-server user: "${UID}:${GID}" environment: - DOCKER_USER=$USER volumes: - ./.config:/home/coder/.config - ./.local:/home/coder/.local - ~/code:/home/coder/workspace - ~/.ssh:/home/coder/.ssh ports: - 8080:8080 restart: unless-stopped
挂载
~/code
到容器中的/home/coder/workspace,这里可以根据自己的要求修改。 -
启动容器
mkdir .config .local docker compose up -d
浏览器访问本机的8080端口,即可进入web版的vscode,登陆密码可以在
./.config/code-server/config.yaml
文件查看