首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Docker Container on AWS实例中使用PyCharm进行开发

问在Docker Container on AWS实例中使用PyCharm进行开发
EN

Stack Overflow用户
提问于 2019-01-15 20:00:42
回答 1查看 1.7K关注 0票数 8

我使用Professional来开发PyCharm。

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

我还可以使用PyCharm通过SSH进入AWS实例,并在那里连接到远程python解释器,它将文件从我的本地项目映射到远程目录,并再次允许我运行图形用户界面单步执行远程代码,就像它是本地代码一样。通过这里描述的过程:Configuring Remote Interpreters via SSH。

我在Docker hub上有一个Docker镜像,我想将其部署到AWS实例,然后将我的本地PyCharm图形用户界面连接到远程容器内的环境,但我不知道如何执行此操作,有人能帮助我吗?

已经提出的一次编辑建议是将SSH Server放在远程容器中,并通过SSH (例如as described here )将本地PyCharm直接连接到容器中。这是一种解决方案,但是已经被extensively criticised elsewhere了--还有更规范的解决方案吗?

EN

回答 1

Stack Overflow用户

发布于 2019-01-21 14:06:00

在做了一些研究之后,我得出的结论是:在我的容器中安装一个PyCharm服务器并通过SSH远程解释器登录是最好的做法,尽管在其他地方提出了一些问题。我按如下方式进行了处理。

下面的Dockerfile将创建一个包含SSH服务器的镜像,您可以通过SSH连接到该镜像。它还具有anaconda/python,因此可以在其中运行笔记本服务器,并以通常的方式连接到该服务器以进行Jupyter解析。请注意,它有一个纯文本密码(截屏),如果你正在使用它做任何敏感的事情,你绝对应该启用密钥登录。

它将获取本地库并将它们安装到容器内的包库中,您也可以选择从GitHub中提取repos (如果您想这样做,请在GitHub中注册一个API密钥,这样您就不需要输入纯文本密码)。它还要求您创建一个纯文本requirements.txt,其中包含需要通过pip安装的所有其他包。

然后运行build命令以创建映像,并运行以从该映像创建容器。在Dockerfile中,我们通过容器的端口22公开SSH,所以让我们将其挂接到AWS实例上的一个未使用的端口-这是我们将通过的端口。如果您想在任何时候从本地机器使用Jupyter,还需要添加另一个端口配对:

代码语言:javascript
复制
docker build -t your_image_name .

不要错过最后的. -它很重要!

代码语言:javascript
复制
docker run -d -p 5001:22 -p8889:8889 --name=your_container_name your_image_name

注意:您需要使用jupyter notebook猛击容器(docker exec -it xxxxxxxxxx bash)并打开Jupyter。

Dockerfile:

代码语言:javascript
复制
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"]
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54198412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档