文章连接: 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"]