Pycharm远程调试服务器中的代码(docker容器内部)

                       Pycharm远程调试服务器中的代码(docker容器内部)

 

一、首先假设你已启动了一个docker容器,并在启动时将容器的22端口映射到宿主机的10022端口

启动示例:

docker run -d --name django_api -p 8000:80 -p 10022:22 -p 5000:5000 --link mysql_host:mymysql --link redis_host:myredis  -v $PWD:/home/docker/code/app/:Z python3/django/ngnix

 启动后使用xshell远程连接宿主机的10022端口是无法连接成功的,此时我们需要进入docker容器内部进行一些操作:

二、进行容器内部修改

彩蛋:文章最后我会讲解如何修改Dockerfile 使其在建立时就允许ssh远程登陆

docker exec -it 容器名 /bin/bash

1、修改root用户密码

passwd

2、首先检查容器内部是否以安装 openssh-server与openssh-client 若没安装执行一下命令安装

apt-get install openssh-server
apt-get install openssh-client

3、修改SSH配置文件以下选项

vim /etc/ssh/sshd_config


# PermitRootLogin prohibit-password # 默认打开 禁止root用户使用密码登陆,需要将其注释
RSAAuthentication yes #启用 RSA 认证
PubkeyAuthentication yes #启用公钥私钥配对认证方式
PermitRootLogin yes #允许root用户使用ssh登录

4、启动sshd服务

/etc/init.d/ssh restart

 5、退出容器,连接测试

ssh root@127.0.0.1 -p 10022

输入密码成功进入容器内部即配置成功

 6、如若需要将修改后的容器重新保存为镜像,则可进行相应处理,本文直接使用修改后的镜像进行后续操作

三、使用Pycharm远程连接

1、打开配置界面

2、按照远程服务器信息配置信息:配置好后可以点击测试连接测试是否能够连接成功

点击测试连接

将本地的代码和服务器代码连接

 此时便可以远程调试代码了

测试上传本地代码到服务器:

彩蛋:修改Dockerfile 建立镜像时就允许用户通过远程连接

由于我在CMD中启动了 supervisord 此时容器启动后需要手动进入容器启动sshd

/etc/init.d/ssh start
   
   

或者将启动命令放入supervisor-app.conf文件中,使其建立容器时就启动


    
    
  1. # Copyright 2013 Thatcher Peskens
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. FROM ubuntu:16.04
  15. MAINTAINER Dockerfiles
  16. # Install required packages and remove the apt packages cache when done.
  17. RUN apt-get update && \
  18. apt-get upgrade -y && \
  19. apt-get install -y \
  20. git \
  21. vim \
  22. python3 \
  23. python3-dev \
  24. python3-setuptools \
  25. python3-pip \
  26. nginx \
  27. supervisor \
  28. openssh-server \
  29. sqlite3 && \
  30. pip3 install -U pip setuptools && \
  31. rm -rf /var/lib/apt/lists/*
  32. # 更新pip
  33. RUN pip3 install --upgrade pip
  34. # install uwsgi now because it takes a little while
  35. RUN pip3 install uwsgi
  36. RUN pip3 install meld3==1.0.0
  37. # setup all the configfiles
  38. RUN echo "daemon off;" >> /etc/nginx/nginx.conf
  39. # 设置root用户密码
  40. RUN echo root:hancb|chpasswd
  41. # 允许root用户使用密码通过ssh登录
  42. RUN echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
  43. RUN sed -i 's/PermitRootLogin prohibit-password/# PermitRootLogin prohibit-password/' /etc/ssh/sshd_config
  44. ## 启动ssh连接
  45. RUN /etc/init.d/ssh start
  46. COPY nginx-app.conf /home/docker/code/app/
  47. # 将配置文件软连接过去, 注意需要写绝对路径
  48. RUN rm -f /etc/nginx/sites-available/default
  49. RUN ln -s /home/docker/code/app/nginx-app.conf /etc/nginx/sites-available/default
  50. COPY supervisor-app.conf /home/docker/code/app/
  51. RUN rm -f /etc/supervisor/conf.d/supervisor-app.conf
  52. RUN ln -s /home/docker/code/app/supervisor-app.conf /etc/supervisor/conf.d/
  53. RUN ln -s /home/docker/code/app/conf/supervisord.conf /etc/supervisor/conf.d/ # celery
  54. # COPY requirements.txt and RUN pip install BEFORE adding the rest of your code, this will cause Docker's caching mechanism
  55. # to prevent re-installing (all your) dependencies when you made a change a line or two in your app.
  56. COPY requirements.txt /home/docker/code/app/
  57. RUN pip3 install -r /home/docker/code/app/requirements.txt
  58. # 设置默认python版本为python3
  59. # RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 3
  60. # RUN update-alternatives --install /usr/bin/python python /usr/bin/python2 2
  61. # add (the rest of) our code
  62. COPY uwsgi.ini /home/docker/code/app/
  63. COPY uwsgi_params /home/docker/code/app/
  64. # install django, normally you would remove this step because your project would already
  65. # be installed in the code/app/ directory
  66. # RUN django-admin.py startproject website /home/docker/code/app/
  67. EXPOSE 80
  68. CMD ["supervisord", "-n"]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值