程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Calling the Python interpreter in docker with pychart

編輯:Python

Reference article :https://blog.csdn.net/ambm29/article/details/96483086#commentBox

utilize Pycharm Remote use Docker Medium Python The environment is divided into 4 Step

  1. Ready to own Python Environmental Docker
  2. to Docker install ssh-server And create a new image
  3. Run the new image , Opening to the outside world ssh port
  4. Pycharm Connect Docker The interpreter in

One 、 Ready to own Python Environmental Docker Mirror image

A little .

Two 、 to Docker Installation configuration ssh-server

Purpose : In order to make Docker Can be ssh Command remote connection .

  1. function Docker Image creates a new container , And install... In the container ssh-server
# Run the image to create a new container (python_env:3.2 It's made by yourself Python Environmental Docker)
sudo docker run -it --name python_env python_env:3.2
# It should already be in the container , install openssh-server
apt install update
apt install openssh-server -y
# start-up ssh service 
service ssh start # Output [ok] Starting OpenBSD Secure Shell server: sshd. That is success 
# see ssh Service status 
service ssh status
  1. To configure ssh The login password , Here the password is set to “1237894560”, The user name defaults to “root
# The terminal inputs the following commands 
passwd
# Enter new UNIX password: Input password 1237894560
# Retype new UNIX password: Enter the password again 1237894560
# passwd: password update successfully Password set successfully 
  1. modify ssh Configuration file for
vim /etc/ssh/sshd_config

Modify the following two places

......
PermitRootLogin yes
......
UsePAM no
......
  1. Create a folder for mounting items
    Purpose : It can be omitted , Customize a directory for mounting items , After use
cd /home
mkdir PythonProject
  1. Submit as a new image
    Here we are ,Docker In container ssh-server The configuration has been completed , Let's commit the container as a new image , Left for later use .
# Push out the container 
exit
# Look at all the containers in the system 
sudo docker ps -a
# Commit the container just now as a new image (python_env For the container name just now , You can also write containers ID)
sudo docker commit -a "lifan" -m "add ssh, passwd=1237894560" python_env python_env_ssh:1
# View the newly generated container 
sudo docker images

Be careful : In the reference article , The author is in 1 Step installation openssh-server After the service ssh start It's set to ~/.bashrc In file , So as to achieve container startup ,ssh Self service .
But I'll use it later Pycharm Remote connected Docker I encountered the problem as shown in the figure below , I checked a lot of data and found that ~/.bashrc There is output information in the file .
So I won't start here ssh The command of wrote ~/.bashrc 了 .

3、 ... and 、 Run the new image , Mount items , Open ports

The reason why I did this , All because the leaders have to use Docker The environment in ,…, It's killing me .

  1. Run the new image , Start a new container
sudo docker run -id --name python_env_ssh -p 7061:22 -p 5000:5000 -v /home/lifan/Python_SSH_Project:/home/Python_Project python_env_ssh:1
  • -id: Container in interactive mode , Run in the background
  • --name: Container name
  • -p: Port number mapping , Host port : Container port ,5000 It's for flask With ,7061 It's for ssh With
  • -v: Directory mapping , Host project directory : Container project directory
  1. Because I'm up there , No settings ssh The self enlightenment of , So now you need to go into the container , Manual start ssh service :
# Into the container 
sudo docker exec -it python_env_ssh /bin/bash
# start-up ssh service 
service ssh start

Four 、Pycharm Remote connection Docker

  1. Add a new one Python Interpreter
  2. choice ssh Remote connection interpreter , And configure

    If the connection is successful, go back to the following interface ;
    If not, check whether the container is started , In container ssh Whether the service starts ;

5、 ... and 、 Use the remote interpreter to run local programs

# -*- coding: utf8 -*-
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def index():
return "Hello Flask"
if __name__ == '__main__':
print(app.url_map)
app.run(host="0.0.0.0", port=5000, debug=True)

Before running the project , You can see if the interpreter is correct

Enter the... Of the host in the browser IP, And then it's done .

Last , It's how to make ssh The service starts automatically when the container is started , Who has the time to leave a message and a blog link , thank .

The reference article is much more detailed than what I copied , You can read the original .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved