远程访问docker容器_docker运行python程序

远程访问docker容器_docker运行python程序                      Pycharm远程调试服务器中的代码(docker容器内部) 一、首先假设你已启动了一个docker容器,并在启动时将容器的22端口映射到宿主机的10022端口启动示例:dockerrun-d–namedjango_api-p8000:80-p10022:22-p5000:5000–linkmysql_ho…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

                       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、打开配置界面远程访问docker容器_docker运行python程序

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

远程访问docker容器_docker运行python程序

点击测试连接

远程访问docker容器_docker运行python程序

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

远程访问docker容器_docker运行python程序

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

远程访问docker容器_docker运行python程序

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

远程访问docker容器_docker运行python程序

彩蛋:修改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/*

# 更新pip
RUN pip3 install --upgrade pip
# install uwsgi now because it takes a little while
RUN pip3 install uwsgi
RUN pip3 install meld3==1.0.0
# setup all the configfiles
RUN echo "daemon off;" >> /etc/nginx/nginx.conf

# 设置root用户密码
RUN echo root:hancb|chpasswd

# 允许root用户使用密码通过ssh登录
RUN echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
RUN 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/default
RUN 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.conf
RUN 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 code
COPY 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 80
CMD ["supervisord", "-n"]
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/175120.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • docker 修改容器时间_docker开放容器端口

    docker 修改容器时间_docker开放容器端口前言用docker搭建的Jenkins环境时间显示和我们本地时间相差8个小时,需修改容器内部的系统时间查看时间查看系统时间date-R进入docker容器内部,查看容器时间dockere

    2022年7月31日
    5
  • docker卸载命令_删除docker

    docker卸载命令_删除docker删除容器(jenkins官网提供的安装方式,删除比较特殊,因为jenkins自己创建了数据卷,所以要删除数据卷)不然,就算删除了容器,再运行镜像,以前的配置还是删不掉,运行的还是以前的配置首先,关停并删除jenkins容器dockerstop容器iddockerrm容器id然后,查看数据卷(如果是用挂载目录方式安装的jenkins的话,就不需要执行下面的步骤了)dockervolumels发现一个jenkins_home的数据卷,删除数据卷dockervolume

    2022年9月7日
    0
  • docker下载安装教程_安卓安装docker

    docker下载安装教程_安卓安装docker前言Docker提供轻量的虚拟化,你能够从Docker获得一个额外抽象层,你能够在单台机器上运行多个Docker微容器,而每个微容器里都有一个微服务或独立应用,例如你可以将Tomcat运行在一个D

    2022年7月28日
    6
  • docker dockerfile详解_docker exec 进入容器

    docker dockerfile详解_docker exec 进入容器前言Dockerfile是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。Dockerfile简介Dockerfile是用来构建Docker镜像的构建文件,是由一系列

    2022年7月28日
    3
  • Dockerfile add_在docker中使用ubuntu

    Dockerfile add_在docker中使用ubuntu前言Dockerfile中提供了两个非常相似的命令COPY和ADD,本文尝试解释这两个命令的基本功能,以及其异同点,然后总结其各自适合的应用场景。Build上下文的概念在使用dock

    2022年8月6日
    1
  • docker dockerfile详解_进入docker容器命令

    docker dockerfile详解_进入docker容器命令前言Dockerfile是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。Dockerfile简介Dockerfile是用来构建Docker镜像的构建文件,是由一系列

    2022年7月30日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号