在Windows11中搭建docker+conda镜像并配置ssh
要在Windows上使用Docker搭建Ubuntu + Conda环境,并配置ssh,可以遵循以下步骤:
1. 安装Docker for Windows
首先,确保你已经安装了Docker for Windows。你可以从Docker官方网站下载并安装它。
2. 获取或创建Dockerfile
接下来,你需要一个Dockerfile来描述你的Docker镜像。这个Dockerfile应该基于Ubuntu,并安装Conda。以下是一个简单的示例:
Dockerfile
# 使用Ubuntu 22.04作为基础镜像
FROM ubuntu:22.04
# 更换国内源
RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
# 安装必要的软件包
RUN apt-get update --fix-missing &&\
apt-get upgrade -y &&\
apt-get install -y wget bzip2 ca-certificates curl git vim &&\
apt-get install -y nginx &&\
apt-get install -y python3.10 pip &&\
apt-get install python-is-python3 &&\
apt-get clean &&\
rm -rf /var/lib/apt/lists/*
# 下载并安装Miniconda
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && \
/bin/bash ~/miniconda.sh -b -p /opt/conda && \
rm ~/miniconda.sh && \
ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
echo "export PATH=\"/opt/conda/bin:$PATH\"" >> /etc/environment
# 设置工作目录为/app
WORKDIR /app
# 设置环境变量,以便在容器内部可以直接使用conda命令
ENV PATH /opt/conda/bin:$PATH
将上述内容保存为Dockerfile,文件不需要后缀
3. 构建Docker镜像
在包含Dockerfile的目录中打开命令行或终端,并运行以下命令来构建Docker镜像:
docker build -t ubuntu-conda .
这将创建一个名为ubuntu-conda的Docker镜像。
4. 运行Docker容器
现在,现在如果你不需要配置ssh,你可以直接运行容器:
docker run -it ubuntu-conda /bin/bash
如果你需要配置ssh,你需要先简单配置你的Docker容器:
创建一个新的镜像基于这个运行过的容器,ec28d359421f(docker ID),ubuntu-conda:v1(docker Name):
docker commit ec28d359421f ubuntu-conda:v1
端口映射,将22端口映射到12345端口:
docker run -itd --name test -p 12345:22 ubuntu-conda:v1
运行镜像:
docker exec -it 91ee700396ba /bin/bash
5.安装配置ssh,可以远端调用
先安装ssh:
apt update
apt install openssh-server
apt install lsof
进入docker配置ssh:
vim /etc/ssh/sshd_config
修改下面两个内容:
port 22
PermitRootLogin yes
之后需要重启ssh:
service ssh restart
6.创建conda环境
创建conda环境:
conda create -n myenv python=3.8
conda activate myenv
可以用这个指令来获取相应工程的requirements.txt文件:
cd /path/to/your/project
pip freeze > requirements.txt
使用清华源安装相应依赖:
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple