前言
vscode连接docker方便开发,此处主要用在hexo的博客编写,很方便~
安心码文了~
1,docker启动
1.1 blog镜像的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| FROM ubuntu:20.04
WORKDIR /home LABEL maintainer="XINWEN" RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse" >> /etc/apt/sources.list \ && echo "deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse" >> /etc/apt/sources.list \ && echo "deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse" >> /etc/apt/sources.list \ && echo "deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse" >> /etc/apt/sources.list \ && echo "deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse" >> /etc/apt/sources.list
ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update && apt-get upgrade -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends apt-utils \ && apt-get -y install dialog
RUN apt-get -y install dialog clang-format nfs-common \ && apt-get -y install openssh-server \ && apt-get -y install vim python3-pip git curl
RUN pip3 install -i http://mirrors.aliyun.com/pypi/simple -U pip --trusted-host mirrors.aliyun.com \ && pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple/ \ && pip3 config set install.trusted-host mirrors.aliyun.com
RUN apt-get install -y nodejs npm
RUN node -v \ && npm install n -g \ && n stable \ && hash -r \ && cp /usr/local/bin/node /usr/bin/node \ && node -v
RUN mkdir /var/run/sshd EXPOSE 22 CMD ["/user/sbin/ssh","-D"]
ENTRYPOINT /bin/bash
|
构建命令,在Dockerfile的同目录运行:
1
| docker build -f Dockerfile -t blog .
|
1.2 创建容器
1
| docker run -it --name=xinwen -p 4000:4000 -p 8022:22 -v /home/xinwen/xinwenblog:/xinwen blog
|
2,宿主机和docker中关于ssh的配置
2.1 docker允许远程登录
在docker中修改sshd服务的配置文件,在/etc/ssh/sshd_config
位置,将下面的配置项去掉注释即可。
1 2 3
| PermitRootLogin yes PubkeyAuthentication yes PasswordAuthentication yes
|
保存后重启sshd服务,也就是:
2.2 宿主机查看docker的ip
在宿主机上运行,xinwen是docker别名。
2.3 登录宿主机进行连接验证
密码登录或者ssh免密登录方式都可以,二者择一即可。
复制宿主机的公钥到docker中
1
| ssh-copy-id -i /home/xinwen/.ssh/id_rsa.pub root@172.17.0.2
|
root是docker的用户,172.17.0.2
是docker的ip,/home/xinwen/.ssh/id_rsa.pub
是宿主机的公钥。
- 验证登录
输出下面信息就成功了
1 2 3 4 5 6 7 8 9 10 11
| Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.15.0-41-generic x86_64)
* Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage
This system has been minimized by removing packages and content that are not required on a system that users do not log into.
To restore this content, you can run the 'unminimize' command. Last login: Mon Aug 1 17:55:51 2022 from 172.17.0.1
|
3,VSCODE插件
主要是两个插件Docker
和Remote-Containers
点击左下角”><“标志,在顶部的输入下拉框选择Attach to Running Container...
就能打开docker的home目录。