pycharm遠程調試docker容器內程序

文章連接: https://blog.csdn.net/hanchaobiao/article/details/84069299python

參考連接: https://blog.csdn.net/github_33934628/article/details/80919646   https://blog.csdn.net/Wendy019900107/article/details/81985837mysql

1、首先假設你已啓動了一個docker容器,並在啓動時將容器的22端口映射到宿主機的10022端口
啓動示例:nginx

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/ngnixgit

 啓動後使用xshell遠程鏈接宿主機的10022端口是沒法鏈接成功的,此時咱們須要進入docker容器內部進行一些操做:github

2、進行容器內部修改
彩蛋:文章最後我會講解如何修改Dockerfile 使其在創建時就容許ssh遠程登錄web

docker exec -it 容器名 /bin/bashredis

一、修改root用戶密碼sql

passwddocker

二、首先檢查容器內部是否以安裝 openssh-server與openssh-client 若沒安裝執行一下命令安裝shell

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

三、修改SSH配置文件如下選項

vim /etc/ssh/sshd_config


# PermitRootLogin prohibit-password # 默認打開 禁止root用戶使用密碼登錄,須要將其註釋
RSAAuthentication yes #啓用 RSA 認證
PubkeyAuthentication yes #啓用公鑰私鑰配對認證方式
PermitRootLogin yes #容許root用戶使用ssh登陸

四、啓動sshd服務

/etc/init.d/ssh restart

 五、退出容器,鏈接測試

ssh root@127.0.0.1 -p 10022

輸入密碼成功進入容器內部即配置成功

 六、如若須要將修改後的容器從新保存爲鏡像,則可進行相應處理,本文直接使用修改後的鏡像進行後續操做

3、使用Pycharm遠程鏈接
一、打開配置界面

二、按照遠程服務器信息配置信息:配置好後能夠點擊測試鏈接測試是否可以鏈接成功

 

點擊測試鏈接

 

將本地的代碼和服務器代碼鏈接

 

 此時即可以遠程調試代碼了

 

測試上傳本地代碼到服務器:

 

彩蛋:修改Dockerfile 創建鏡像時就容許用戶經過遠程鏈接
因爲我在CMD中啓動了 supervisord 此時容器啓動後須要手動進入容器啓動sshd

/etc/init.d/ssh start
或者將啓動命令放入supervisor-app.conf文件中,使其創建容器時就啓動

# Copyright 2013 Thatcher Peskens## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License. FROM ubuntu:16.04 MAINTAINER Dockerfiles # Install required packages and remove the apt packages cache when done. RUN apt-get update && \apt-get upgrade -y && \apt-get install -y \git \vim \python3 \python3-dev \python3-setuptools \python3-pip \nginx \supervisor \openssh-server \sqlite3 && \pip3 install -U pip setuptools && \rm -rf /var/lib/apt/lists/* # 更新pipRUN pip3 install --upgrade pip# install uwsgi now because it takes a little whileRUN pip3 install uwsgiRUN pip3 install meld3==1.0.0# setup all the configfilesRUN echo "daemon off;" >> /etc/nginx/nginx.conf # 設置root用戶密碼RUN echo root:hancb|chpasswd # 容許root用戶使用密碼經過ssh登陸RUN echo "PermitRootLogin yes" >> /etc/ssh/sshd_configRUN sed -i 's/PermitRootLogin prohibit-password/# PermitRootLogin prohibit-password/' /etc/ssh/sshd_config ## 啓動ssh鏈接RUN /etc/init.d/ssh start COPY nginx-app.conf /home/docker/code/app/# 將配置文件軟鏈接過去, 注意須要寫絕對路徑RUN rm -f /etc/nginx/sites-available/defaultRUN ln -s /home/docker/code/app/nginx-app.conf /etc/nginx/sites-available/default COPY supervisor-app.conf /home/docker/code/app/RUN rm -f /etc/supervisor/conf.d/supervisor-app.confRUN ln -s /home/docker/code/app/supervisor-app.conf /etc/supervisor/conf.d/RUN ln -s /home/docker/code/app/conf/supervisord.conf /etc/supervisor/conf.d/ # celery # COPY requirements.txt and RUN pip install BEFORE adding the rest of your code, this will cause Docker's caching mechanism# to prevent re-installing (all your) dependencies when you made a change a line or two in your app. COPY requirements.txt /home/docker/code/app/RUN pip3 install -r /home/docker/code/app/requirements.txt # 設置默認python版本爲python3# RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 3# RUN update-alternatives --install /usr/bin/python python /usr/bin/python2 2 # add (the rest of) our codeCOPY uwsgi.ini /home/docker/code/app/ COPY uwsgi_params /home/docker/code/app/ # install django, normally you would remove this step because your project would already# be installed in the code/app/ directory# RUN django-admin.py startproject website /home/docker/code/app/ EXPOSE 80CMD ["supervisord", "-n"]

相關文章
相關標籤/搜索