“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”
A lot of people read the previous series of Docker articles and were confused. What does this have to do with Python?
Docker can be used to reduce the tedious steps of setting up an environment. We can deploy an environment like Selenium in a Docker container, and directly use the remote Selenium in the code, which is simple and convenient.
Use the Python environment in the Docker container for development
Environment: Tencent Cloud CentOS 7 + Docker
Modify the Docker configuration
Easy version:
Edit docker-related configuration files:
vi /etc/docker/daemon.json
Copy the code
Insert the following configuration:
Note that this is the standard JSON format, the format is wrong Docker can not restart
{
"hosts": ["TCP: / / 0.0.0.0:2375"."unix:///var/run/docker.sock"]}Copy the code
Reload Docker configuration :systemctl daemon-reload
Restart Docker:systemctl restart Docker
Complex version: this version is used to use the above configuration modification is still unable to connect the situation, salty fish first configuration appeared this situation, salty fish in a class net notes plate to find the answer.
Here is the address of the original text:
Refer to the link: https://www.imooc.com/article/details/id/28426
Copy the code
The operations are as follows: Edit the following files:
vi /lib/systemd/system/docker.service
Copy the code
Modify the configuration items in the file and save the modification:
Will ExecStart = / usr /bin/ dockerd ExecStart = / usr/insteadbin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0. 0. 0:2375
Copy the code
Configuration Pycharm
First open the Docker display item in Pycharm:
Modify Docker configuration:
Fill in the Docker configuration you edited earlier in the corresponding place below:
Note the format: TCP ://host:ports
After the configuration is complete, a message is displayed indicating that the connection is successful.
Configure the remote Docker image as the interpreter for Pycharm
Click On configuration and click Add Configuration:
In the selection of new items in the Docker, here will be automatically loaded you connect Docker service contains all Python mirror:
Configure automatic uploading of code
You think it’s over at the top? The interpreter is configured, but your code is still local, so you need to configure the code to automatically upload to the cloud server.
First, find the corresponding option. If you haven’t configured it before, the option is gray and you need to configure it first. Click Configuration:
Click the plus sign and fill in the corresponding configuration. After filling in, you can test whether it is available:
If no, log in to the console and configure security groups.
Switch to the mapping TAB next door and configure the relevant paths and paths to upload to the server as shown below:
When all is said and done, go back to the first image in this section and check it with the automatic upload option. After that, changes to your files will be automatically uploaded to the path specified by the server.
At this point, you’d be naive to think it’s over.
Solve the code automatically uploaded after the operation error
When you upload code, run after the error “could not find the corresponding folder/file”, this is because our code just uploaded to the server at this time, but we the Python interpreter is running on the container, and our container lookup code is to find the data volume, so this time we need to do a simple address mapping in the configuration.
Take a look at the schematic diagram above:
Let’s configure global address mapping:
Edit the configuration in the Docke component and add the path Mapping effect: when we configure the cloud service path, our local path will be automatically mapped to it
Set Python’s default mapping to the corresponding path:
Note that container Path refers to the path in a container. Based on the schematic diagram above, the mapping process is local path > cloud server path > container path
The above is the whole content of this article, if the principle of which do not understand the salt fish suggestion can be reviewed before the part of the data volume, if you still do not understand can directly ignore the principle, according to the picture with a steed directly, of course, welcome everyone to leave a message exchange ~ ~