Using Docker (Hyper-V) with PyCharm on Windows 10 (as of August 2017)

When I tried to use Docker for PyCharm on Windows 10, I was addicted to various things such as path resolution, so make a note of it. As a result of trial and error, there may be extra steps.

To be able to

Things impossible

Tried environment

PyCharm didn't work well in 2017.1. For those who are not Windows or who are Windows but do not use Hyper-V, refer to Using Docker for development with PyCharm. I think it would be good if it was done.

1. Preparing the Docker environment

1.1. Installing Docker Toolbox

When using Docker on Windows, there are Docker for Windows and Docker Toolbox, but ** Docker Toolbox ** is included. Select the Get Docker Toolbox for Windows button on the page below to install it.

https://docs.docker.com/toolbox/overview/

At first, I installed Docker for Windows to use Hyper-V, but I gave up because I couldn't set the Certificates folder described later [^ 1]. I also used Hyper-V with Docker Toolbox.

1.2. Enabling Hyper-V

Please enable Hyper-V by referring to the following. Install Hyper-V on Windows 10

1.3. Creating a Boot2Docker virtual machine

Create a VM referring to the following. https://docs.docker.com/machine/drivers/hyper-v/

$ docker-machine create --driver hyperv --hyperv-memory 4096 --hyperv-cpu-count 4 default

When you execute the above command, a VM will be created with the following settings.

Setting items option Above example
driver --driver hyperv
Memory capacity --hyperv-memory 4096
Number of CPUs --hyperv-cpu-count 4
Machine name default

Please refer to the above link for other options and details. It seems that the setting value of this area can be changed later with Hyper-V Manager.

1.4. Hyper-V settings

You cannot connect to the external network with the VM as it is. [Introduction to Docker Machine (for Hyper-V)](http://qiita.com/ohhara_shiojiri/items/801a24a2c94ff93be82d#hyper-v%E7%92%B0%E5%A2%83%E8%A8%AD%E5 Please set the virtual switch and network adapter by referring to ** Hyper-V environment setting ** of% AE% 9A).

As a precaution in this case, ** Be sure to set both the external and internal networks. ** If you use only the external network, the address will change when you connect to another network and it will not work.

It should be in the following state.

virtual-machineのネットワーク設定.png

1.5. Creating a Windows shared folder

Setting method to allow only specific people to access the shared folder of Windows 10 (Part 2)

Make the following settings by referring to the above page.

Make sure the shared folder contains ** the project you are developing **. The user name and password set here will be used in the next section. (Adding users may not really be necessary. I'm not confident here.)

1.6. Mount the shared folder on the VM

Make the Windows shared folder accessible from the VM as cifs. First, install cifs-utils on your VM. Log in to the VM with docker-machine ssh and do the following: (Reference)

$ wget http://distro.ibiblio.org/tinycorelinux/5.x/x86/tcz/cifs-utils.tcz
$ tce-load -i cifs-utils.tcz

Then, mount the shared folder set by referring to the following. Mount and use Windows shared folders from Ubuntu

Persistence of settings

Set it to mount automatically when the VM is restarted. Create a new /var/lib/boot2docker/bootlocal.sh on the VM with the following contents.

bootlocal.sh


# /var/lib/boot2docker/When stored in docker-Executed when machine starts

ip addr add 172.29.32.2/20 dev eth0 #Set a fixed IP address within the same subnet as HostOS

uid=1000              #docker user
gid=50                #staff group
user=share            #Username allowed to share on HostOS
password=xxxxxxxxxxxx #Password of the user who allowed sharing
host_ip=//172.29.32.1 #IP address of HostOS

mkdir -p /home/docker/src
mount -t cifs -o uid=${uid},gid=${gid},username=${user},password=${password} ${host_ip}/src /home/docker/src

The user and password here are the ones set in "1.5. Creating a Windows shared folder".

host_ip is the IP address of your Windows PC. Specify the IP address of the internal network so that it is not affected by network changes. In my environment, it was in the following state.

> ipconfig

Windows IP configuration

Ethernet adapter vEthernet(HNS Internal NIC) 2:

Connection-specific DNS suffix. . . . .:
Link-local IPv6 address. . . . .: fe80::68fe:336d:b69b:e392%24
IPv4 address. . . . . . . . . . . .: 172.29.32.1 ★ Use this as the IP address of HostOS
sub-net mask. . . . . . . . . .: 255.255.240.0 ★ This is for subnets
default gateway. . . . . . .:

···abridgement

1.7. Setting a fixed IP address for the VM

As I mentioned earlier, set the fixed IP address in the following line described in bootlocal.sh above.

ip addr add 172.29.32.2/20 dev eth0 #Set a fixed IP address within the same subnet as HostOS

The subnet mask matches the result of ʻipconfig` on Windows. From PyCharm, you will access the IP address set here.

It is OK if DOCKER_HOST of docker-machine env is the IP address specified here.

$ docker-machine env default
You can further specify your shell with either 'cmd' or 'powershell' with the --shell flag.

SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://172.29.32.2:2376
SET DOCKER_CERT_PATH=C:\Users\uj\.docker\machine\machines\default
SET DOCKER_MACHINE_NAME=default
SET COMPOSE_CONVERT_WINDOWS_PATHS=true

1.8. Preparing Docker image for Python

Prepare your favorite image. I got it from: https://hub.docker.com/_/python/

> docker pull python:3.5

2. Settings in PyCharm

Finally, we will enter the settings on PyCharm.

2.1. Specifying the VM

Open the settings screen from File> Settings and open Build, Execution, Deployment> Docker. After adding by pressing the + button, enter as shown in the screen below. pycharm-virtual-machine設定2.png

If successful, you will see Connection successful in the middle of the window. In addition, this Certificates folder could not be specified in Docker for Windows. [^ 1]

2.2. Designation of Project Interpreter

Continue to open Project: Own Project> Project Interpreter from the settings screen. Click the gear icon at the top right of the window and set as follows. pycharm-interpreter設定.png

If you can set it well and wait for a while (3-4 minutes?), The valid Package will be displayed with the specified image.

pycharm-interpreter設定2.png

2.3. Run/Debug Configurations Finally, make the settings for test execution.

2.3.1 Addition of execution settings

Select Run> Edit Configurations from the menu bar to display the Run / Debug Configurations screen. Press the + button and select Python tests> Unittests (Unittests can be anything you like). pycharm-test-add.png

2.3.2 Execution settings

This is the most complicated. ** It is necessary to distinguish between the path on Windows and the path on VM **. Please read the path according to your own environment.

In the following examples, the project path is: C:\Users\uj\src\github.com\ujiro99\auto_logger

pycharm-test設定.png pycharm-test-container設定.png

If you leave the Path mappings setting in" 2.1. Specifying the VM ", ** the path will be converted ** unintentionally, and it will not work.

3. Test execution

After completing the settings up to this point, you can run the test. In this example, if you store the test files under project path / tests / and execute the test, it will be executed as follows. You can also debug and use breakpoints.

pycharm-test.png

Summary

I introduced how to use PyCharm in Docker (Hyper-V) environment. To be honest, the setting is quite troublesome, so at the moment I think it is better to obediently manage the Python environment on Local. I look forward to the future development of Jetbrans (or Docker for Windows).

[^ 1]: Docker for Windows might have been fine if you were running docker-machine regenerate-certs Hmm.

Recommended Posts

Using Docker (Hyper-V) with PyCharm on Windows 10 (as of August 2017)
Until the start of the django tutorial with pycharm on Windows
PATH when using ANACONDA virtual environment with Pycharm (as of Mac 2020/10/03)
Easy build of C ++ code with CMake on Docker
Specify the project name of docker-compose with Docker integration of Pycharm
Run python with PyCharm (Windows)
Try using matplotlib with PyCharm
Try using OpenCV on Windows
Python environment construction procedure memo using Docker on Windows10 Home
Memo of Linux environment construction using VirtualBox + Vagrant on Windows 10
Use Python on Windows (PyCharm)
Blogging with Pelican on Windows
Get the host name of the host PC with Docker on Linux
[Python machine learning] Recommendation of using Spyder for beginners (as of August 2020)
Environment construction of TensorFlow + JupyterNotebook + Matplotlib on Windows version Anaconda (August 2017 version)
Japaneseize Matplotlib with Alpine using Docker
Completion of docker command on Linux
Ideone> Python version: 3.5 (as of August 29, 2017)
Run SwitchBot on Windows 10 with Bleak
Use Tensorflow 2.1.0 with Anaconda on Windows 10!
Using Japanese with Rodeo's IPython @ Windows
Location of pip.ini (pip.conf) on Windows 10
Notes on using rstrip with python.
Getting started with USD on Windows
Getting started with Python 3.8 on Windows
[Windows] Cause and solution of NotImplementedError when using asyncio.create_subprocess_shell () with Fast API
When using nginx etc. with Docker, first of all, knowledge of Linux ... (commandment)
Install NumPy, SciPy, Matplotlib with Homebrew on OS X (as of November 2015)
Notes for using OpenCV on Windows10 Python 3.8.3.
Run servo with Python on ESP32 (Windows)
Try using Bash on Windows 10 2 (TensorFlow installation)
Using venv in Windows + Docker environment [Python]
Behind the flyer: Using Docker with Python
How to use Cmder with PyCharm (Windows)
Building scikit-learn in Windows 10 environment using Pycharm
Study on Tokyo Rent Using Python (3-1 of 3)
Launch Flask application with Docker on Heroku
Run matplotlib on a Windows Docker container
I tried object detection with YOLO v3 (TensorFlow 2.1) on the GPU of windows!