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 :
Python
Environmental Docker
Docker
install ssh-server
And create a new image ssh
port Pycharm
Connect Docker
The interpreter in A little .
Docker
Installation configuration ssh-server
Purpose : In order to make Docker
Can be ssh
Command remote connection .
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
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
ssh
Configuration file for vim /etc/ssh/sshd_config
Modify the following two places
......
PermitRootLogin yes
......
UsePAM no
......
cd /home
mkdir PythonProject
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
了 .
The reason why I did this , All because the leaders have to use Docker
The environment in ,…, It's killing me .
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 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
Python
Interpreter ssh
Remote connection interpreter , And configure ssh
Whether the service starts ;# -*- 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 .