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.
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.
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.
Please enable Hyper-V by referring to the following. Install Hyper-V on Windows 10
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.
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.
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.)
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
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
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
Prepare your favorite image. I got it from: https://hub.docker.com/_/python/
> docker pull python:3.5
Finally, we will enter the settings on PyCharm.
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.
: If the IP address specified in "1.7. Setting a fixed IP address for the VM" is ʻip_addr
, enter it as https: // $ {ip_addr}: 2376
. It must match the IP address of DOCKER_HOST
. There was information that the protocol was tcp or http, but it didn't work unless it was ** https **.Certificates folder
: Same path as DOCKER_CERT_PATH
in docker-machine env
. Here, use ** slash delimiter ** instead of backslash.Path mappings
: I feel that something was included by default, but since the settings after this will not work, ** I will delete everything. ** **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]
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.
Docker
with the radio buttonServer
: What was specified in "2.1. Specifying the VM"Python interpreter path
: As isIf you can set it well and wait for a while (3-4 minutes?), The valid Package will be displayed with the specified image.
2.3. Run/Debug Configurations Finally, make the settings for test execution.
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).
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
Target
: Select Path
with the radio button. For Path, select the directory where you saved the project test file.Python interpreter
: Select the Interpreter selected in" 2.2. Specifying the Project Interpreter ".Working directory
: ** Specify the project path ** on Windows.Path mappings
: ...
to bring up the dialog.Local path
** Specifies the project path on Windows.Remote Path
.Docker container settings
: ...
to display the dialog. Set as follows.Volume bindings
: >>
to display the dialog.Container path
.Host path
specifies the ** project path on the ** VM.If you leave the Path mappings
setting in" 2.1. Specifying the VM ", ** the path will be converted ** unintentionally, and it will not work.
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.
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