私信  •  关注

StackG

StackG 最近回复了
5 年前
回复了 StackG 创建的主题 » 基于aws实例的docker容器中pycharm的开发

在做了一点研究之后,我得出的结论是,在我的容器中安装SSH服务器并通过PyPosithSSH远程解释器登录是最好的办法,尽管在别处引发了关注。我做到了如下。

下面的DoCKFrm将创建一个带有SSH服务器的映像,其中SSH服务器可以进入SSH。它还拥有anaconda/python,因此可以在内部运行一个笔记本服务器,并以jupyter degubbing通常的方式连接到该服务器。请注意,它有一个纯文本密码(screencast),如果您将它用于任何敏感的东西,那么您肯定应该启用密钥登录。

它将使用本地库并将它们安装到容器内的包库中,并且可选地,您也可以从GITHUB中撤回RePOS(如果您想这样做,则在GITHUB中注册API密钥,这样就不需要输入纯文本密码)。它还要求您创建一个纯文本 requirements.txt 包含所有其他需要安装pip的包。

然后运行build命令创建映像,并运行以从该映像创建容器。在dockerfile中,我们通过容器的端口22公开ssh,因此让我们将其连接到aws实例上未使用的端口-这是我们将通过ssh的端口。如果要在任何时候从本地计算机使用jupyter,还可以添加另一个端口对:

docker build -t your_image_name .

不要错过 . 最后-这很重要!

docker run -d -p 5001:22 -p8889:8889 --name=your_container_name your_image_name

铌。你需要猛击容器( docker exec -it xxxxxxxxxx bash )打开Jupyter jupyter notebook .

Dockerfile:

ROM python:3.6

RUN apt-get update && apt-get install -y openssh-server

# Load an ssh server. Change root username and password. By default in debian, password login is prohibited,
# go into the file that controls this and make a change to allow password login
RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN /etc/init.d/ssh restart

# Install git, so we can pull in some repos
RUN apt-get update && apt-get upgrade -y && apt-get install -y git

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

# Install the requirements and the libraries we need (from a requirements.txt file)
COPY requirements.txt /tmp/
RUN python3 -m pip install -r /tmp/requirements.txt

# These are local libraries, add them (assuming a setup.py)
ADD your_libs_directory /your_libs_directory
RUN python3 -m pip install /your_libs_directory
RUN python3 your_libs_directory/setup.py install

# Adding git repos (optional - assuming a setup.py)
git clone https://git_user_name:git_API_token@github.com/YourGit/git_repo.git
RUN python3 -m pip install /git_repo
RUN python3 git_repo/setup.py install

# Cleanup
RUN apt-get update && apt-get upgrade -y && apt-get autoremove -y

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]