我使用PyCharm Professional开发python。

我能够将PyCharm运行/调试GUI连接到本地Docker镜像的python解释器,并使用Docker Container python环境库运行本地代码。通过此处描述的过程:Configuring Remote Interpreter via Docker

我还可以使用PyCharm SSH进入AWS实例并连接到那里的远程python解释器,该解释器将本地项目中的文件映射到远程目录,并再次允许我运行GUI来逐步浏览远程代码,就好像它是本地的一样。通过此处描述的过程:Configuring Remote Interpreters via SSH

我在Docker集线器上有一个Docker镜像,我想将其部署到一个AWS实例,然后将我的本地PyCharm GUI连接到远程容器内的环境,但是我看不到该怎么做,有人可以帮助我吗?

[EDIT]一旦提出建议,就是将SSH服务器放入远程容器中,并通过SSH将我的本地PyCharm直接连接到容器中,例如as described here。这是一个解决方案,但现在是extensively criticised elsewhere-还有更规范的解决方案吗?

最佳答案

经过一些研究,我得出的结论是,尽管在其他地方引起了关注,但最好的方法是在容器中安装SSH服务器并通过PyCharm SSH远程解释器登录。我按如下方式进行管理。

下面的Dockerfile将创建一个带有SSH服务器的镜像,您可以在其中使用SSH服务器。它还具有anaconda / python,因此可以在内部运行笔记本服务器并以通常的方式连接该服务器以进行Jupyter除胶。请注意,它具有纯文本密码(截屏视频),如果您将其用于敏感内容,则一定要启用密钥登录。

它会带本地库并将它们安装到容器内的包库中,并且可选地,您也可以从GitHub中提取存储库(如果要这样做,则可以在GitHub中注册API密钥,因此您无需输入纯文本文字密码)。它还需要您创建一个纯文本requirements.txt,其中包含需要点子安装的所有其他软件包。

然后运行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

Nb。您将需要扑向容器(docker exec -it xxxxxxxxxx bash)并使用jupyter notebook打开Jupyter。

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"]

10-04 15:01
查看更多