使用docker搭建 browser网络code环境
搭建web-code学习记录
0.前言
之前买的服务器一直闲着么有用,最近就想搭建点东西用起来。之前有看到docker就想学习一下,便决定使用docker搭建一个webcoder。本次使用到的环境和软件有ubuntu+docker+codercom/code-server。由于docker是第一次使用所以搭建的过程的同时也在学习这些软件的使用。
1.安装docker(参考菜鸟教程)
由于服务器已经安装好ubuntu系统这里便不再记录。直接从docker的安装开始记录。教程也是在百度搜到的可以直接百度搜索这一步骤。步骤未详细了解可能有坑。tip:由于安装的环境有所不同所以安装过程可能出现不同的错误。本文仅供参考
-
卸载旧的安装,以确保环境的一致性
Docker旧的版本为docker,dcker-engine或docker.io,当前的版本称为Docker Engine-Community(docker社区办)软件包名为docker-ce
sudo apt-get remove docker docker-engine docker.io containerd runc
-
自动安装docker
-
官方安装命令
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
-
使用国内的源
curl -sSL https://get.daocloud.io/docker | sh
-
-
手动安装docker
-
卸载旧的软件
-
更新apt包的索引
$ sudo apt-get update
- 添加docker的官方GPG秘钥 ```shell $ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add
-
设置稳定版的厂库
$ sudo add-apt-repository \ "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \ $(lsb_release -cs) \ stable"
-
安装Docker Engine-Community
-
更新索引
$ sudo apt-get update
-
安装docker ec和containerd
$ sudo apt-get install docjer-ce docker-ce-cli containerd.io
-
检测是否安装成功
docker run hello-world
语句可能受网络影响
-
成功示例
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
-
2.使用docker搭建起web-code的容器
-
使用docker命令前面需要加上docker 这个单词。
#从docker官方pull到需要使用使用的镜像, $ docker pull docercom/code-server # 根据官方的建议,需要把容器内配置文件映射到服务器主机上。在主机建立对应的文件 $ mkdir -p ~/.config #拉取到镜像后需要使用镜像run一个容器,docker-hub上的vscode官方提供有使用的shell命令为 $ docker run -it --name code-server -p 127.0.0.1:8080:8080 \ -v "$HOME/.config:/home/coder/.config" \ -v "$PWD:/home/coder/project" \ -u "$(id -u):$(id -g)" \ -e "DOCKER_USER=$USER" \ codercom/code-server:latest #我在使用这一命令是总是报错,所以使用了简化后的命令 $ docker run -it --name webdocer -p 8080:8080 \ -v /home/webcode:/home/coder/project \ -u root \ codercom/coder-server #本人目前使用未发现异常
-
对一些命令的解释
根据官方的建议,需要把容器内配置文件映射到服务器主机上,以便对wedcodede密码进行管理,
对应的命令为,1.在服务器建立一个config文件夹对应容器内的/home/coder/.config,同步其中文件,以便对容器配置文件的操作
$ mkdir -p ~/.config
-v "$HOME/.config:/home/coder/.config"
个人感觉这一步意义不大,不添加这一步也可以使用,并不会添加太多的麻烦。这一步使用到的技术为docker的数据卷技术,如果要想学习docker这是必须掌握的.
-
docker命令的讲解
-
docker run image
使用镜像image创建一个容器
-
docker run -it
-i 保持后台运行
-t 打开一窗口运行。两个命令一般一起使用
-
docker run -p 81:80
-p端口映射把主机的端口81对应到容器的80端口
-
docker run -v path1:path2
数据卷技术,把主机的文件夹path1对应到容器的path2文件夹。两者内的数据同步,容器删除后主机的文件不受影响。
-
docker run -u
授权指定的用户权限启动运行docker(个人认为),在容器中的用户为docker未有root权限,使用-u可以提高docker的权限使其可是进行更多的操作,如建立删除文件
-
docker run -e
配置容器的环境变量。
-
-
之后便可以在浏览器的访问web-code的网站,浏览的url为ip:8080