1、前言
本文记录了通过docker、docker-compose部署code-server、mysql,搭建在线开发环境的过程,综合体验很爽,适合小规模开发团队使用。
安装环境:
vmware hypervision、centos 8
2、安装docker
# 获取阿里云yum镜像文件
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
# 非阿里云主机需要替换地址
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
# 重建 yun cache
yum clean all
yum makecache
# 使用yum升级系统
yum update --nobest
#删除旧的docker
sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
# 安装最新版的containerd.io
dnf install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/nightly/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
#安装Yum源管理工具
yum install -y yum-utils device-mapper-persistent-data lvm2
#安装阿里云的docker-ce源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache
#安装docker-ce
yum install docker-ce docker-ce-cli
#配置docker 镜像
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://heusyzko.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
3、安装 docker-compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.26.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
4、准备vs的settings.json配置文件
vi settings.json
{
// Nomal
"editor.fontSize": 18,
"workbench.iconTheme": "vscode-icons",
"vsicons.dontShowNewVersionMessage": true,
"editor.minimap.enabled": true,
"workbench.colorTheme": "Visual Studio Light",
"workbench.startupEditor": "newUntitledFile",
// 保存格式化
"files.autoSave": "onFocusChange",
"editor.formatOnPaste": true,
"editor.formatOnType": true,
// Env
"java.home": "/usr/local/jdk1.8.0_261",
"maven.executable.path": "/usr/local/apache-maven-3.6.0/bin/mvn",
"java.configuration.maven.userSettings": "/usr/local/apache-maven-3.6.0/conf/settings.xml",
"maven.terminal.customEnv": [
{
"environmentVariable": "JAVA_HOME",
"value": "/usr/local/jdk1.8.0_261"
}
],
"python.formatting.provider": "yapf",
"python.autoComplete.addBrackets": true,
"python.jediEnabled": false,
"python.linting.pylintEnabled": true,
// exclude file
"files.exclude": {
"**/.classpath": true,
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true,
"**/.vscode": true,
"**/.empty": true,
},
// code-runner
"code-runner.clearPreviousOutput": true,
"code-runner.runInTerminal": false,
// 执行文件的脚本,可以使用绝对路径
"code-runner.executorMap": {
"python": "/usr/local/anaconda3/python3",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
},
}
5、编辑Dockerfile文件
vs code的插件建议自行在官方市场下载后,放到extensions目录。
下载地址:
https://marketplace.visualstudio.com/
Java开发环境常用插件如下:
名称 | 说明 |
---|---|
beautify | 代码美化插件 |
Chinese (Simplified) Language Pack for Visual Studio Code | 中文语言包 |
Debugger for Java | 关键插件,java debug插件 |
GitLens | git增强插件,可以很方便看到代码的提交修改记录,还至此在代码行提示代码的变更信息 |
Java Test Runner | java测试插件 |
Language Support for Java™ by Red Hat | 关键插件,支持java语言 |
Maven for Java | 关键插件,java maven插件 |
Visual Studio IntelliCode | 关键插件,代码自动提示插件 |
FROM codercom/code-server:latest
# COPY JDK和MAVEN
COPY jdk1.8.0_261 /usr/local/jdk1.8.0_261/
COPY apache-maven-3.6.3 /usr/local/apache-maven-3.6.3/
# code-server配置文件 插件
COPY extensions /root/.local/share/code-server/extensions/
COPY settings.json /root/.local/share/code-server/User/
# Env
ENV JAVA_HOME=/usr/local/jdk1.8.0_261 \
JRE_HOME=/usr/local/jdk1.8.0_261/jre \
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin \
CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib \
MAVEN_HOME=/usr/local/apache-maven-3.6.3
ENV PATH=$MAVEN_HOME/bin:$PATH
WORKDIR /home/coder/project
# 容器启动code-server:指定插件目录,指定中文,指定免密登录
ENTRYPOINT ["code-server","--locale","zh-cn","--host","0.0.0.0","--port","8080", "--user-data-dir", "/home/coder","--cert",""]
6、build 自定义docker image
docker build -t boshine/code-server:latest --rm=true .
7、编辑docker-compose
如果团队多人一起协同开发,建议每个人部署一个code-server容器,实现环境隔离。
配合github/gitee/gitlab等代码管理服务器使用。
version: "3"
services:
luocoder:
container_name: luocoder
image: boshine/code-server
links:
- db
depends_on:
- db
ports:
- "8080:8080"
- "8088:8088"
volumes:
- "/root/code-server:/home/"
- "/root/code-server/root:/root"
- "/root/code-server/tmp:/tmp"
environment:
PASSWORD: 12345678
restart: always
privileged: true
user: root
db:
container_name: mysql
image: mysql:5.7
ports:
- "3306:3306"
volumes:
- "/root/mysql/data:/var/lib/mysql"
- "/root/mysql/conf:/etc/mysql"
- "/root/mysql/logs:/var/log/mysql"
command: [
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
'--max_connections=3000'
]
environment:
MYSQL_ROOT_PASSWORD: 12345678
restart: always
privileged: true
user: root
8、使用docker-compose运行coder容
docker-compose up -d