0

Docker Ubuntu 指南

 2 years ago
source link: https://www.enoch2090.me/docker-ubuntu-man-ual
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Docker Ubuntu 指南

2021 年 5 月 20 日
我不知道其他 macOS/Windows 用户怎么看,反正我对没完没了的虚拟机感到足够厌倦了。Parallels 已经做得很好了,但是每逢启动你还是会看见它贪婪地占用内存,用渲染糟糕的图形界面为你提供卡顿。有时候我就想在 Ubuntu 下面编译个程序,真的不需要那么多。所以我搞了个 Docker Ubuntu。
先去 docker.com 下个 Docker。然后拉取 Ubuntu 镜像:
docker pull ubuntu
docker images list
这时候你该看到像这样的输出:
REPOSITORY                    TAG       IMAGE ID       CREATED        SIZE
ubuntu-enoch                  latest    0dc75989e598   37 hours ago   4.22GB
<none>                        <none>    e51fff257731   47 hours ago   4.05GB
citybrainchallenge/cbengine   0.1.2     a113d5290bcc   2 weeks ago    3.64GB
ubuntu                        latest    7e0aa2d69a15   3 weeks ago    72.7MB
这个 CREATED 指的是这个镜像本身被创建的时间,所以看到几周前也不用慌。记下要启动的镜像 IMAGE ID,然后运行它:
docker run -it 7e0aa2d69a15 /bin/bash
notion image
这个镜像一般是 minimum 的,所以要先装一波常用软件。
apt-get update
apt-get upgrade
apt-get install net-tools vim nano openssh-serve git curl wget
也许根据需要再装一下 Anaconda 之类。全部弄好以后用 exit 命令退出,这时候一定要把做的改动提交保存成镜像。
docker commit {CONTAINER_ID} {CONTAINER_NAME}
CONTAINER_ID 就是你在容器里交互时,root@ 后面那串 ID。后面的名字随便起。下次再打开这个镜像就行了。
其中 {IMAGE_NAME} 是你的镜像名字。

SSH连接

容器是可以用 VS Code 远程的。首先启动的时候要加上 -p 50001:22,把容器的 22 端口映射到宿主机的 50001(或者其它任何可用端口上)。调整ssh设置:
nano /etc/ssh/sshd_config
然后在容器里启动 ssh 服务:
/etc/init.d/ssh restart
然后就可以用VS Code远程了。
ssh -p 50001 [email protected]
每次 docker commit 以后镜像的都会变,但是 commit 的时候可以保证名字不变。如果是常用的 Ubuntu 镜像,可以用管道直接抓取对应的id来启动。
docker run -v ~/:/data -it $(docker image ls | grep {IMAGE_NAME}| awk '{print $3}')
如果考虑到容器每次要 ssh,还要加上端口。所以可以考虑把这句命令加进你的 ~/.bashrc 或者 ~/.zshrc
alias ubt="docker run -p 50001:22 -v ~/:/data -it $(docker image ls | grep {IMAGE_NAME}| awk '{print $3}')"
老样子,{IMAGE_NAME} 是你的镜像名字。
别的没什么值得说的了,记下来主要是省得自己下次要用了满世界找。这就去编译一手 GMP。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK