I\'m starting with Docker, but I don\'t know how to configure PyCharm to use a python interpreter located in a container.
It was easy to setup with Vagrant, but there\'s
Steps specific to PyCharm Professional Edition 2017.2(however they may work with PyCharm CE)
Here are a couple steps I took to get my setup working
A few assumptions of the structure of your (or anyone who might be reading this) project:
bleh
├── README.md
├── api
│ ├── Dockerfile <---- this is the one we want to debug
│ ├── config.example.ini
│ └── src
│ ├── __init__.py <---- this is a pycharm project
│ ├── __main__.py <---- this is a pycharm project
│ └── ...
├── proxy
│ ├── Dockerfile
│ ├── config.example.ini
│ └── src
│ ├── ...
│ └── ...
├── webserver
│ ├── Dockerfile
│ ├── config.example.ini
│ └── src
│ ├── ...
│ └── ...
├── frontend
│ ├── Dockerfile
│ ├── config.example.ini
│ └── src
│ ├── ...
│ └── ...
├── db
│ ├── Dockerfile
│ ├── ...
│ └── migrations
│ ├── ...
│ └── ...
└── docker-compose.yml
bleh
as a my project name only as an example. /Users/myfunkyusername/Projects/bleh
. api
service as shown later in the docker-compose.yml
fileNote We're also going to assume a content of your api
's one and only Dockerfile
is as such
FROM python
ADD config.example.ini /etc/bleh/config.ini
RUN chmod +x /usr/bin/bleh
COPY ./src /usr/bin/bleh
WORKDIR /usr/bin/bleh
RUN pip install -r requirements.txt
CMD ["sh", "-c", "python -m bleh --cfg=/etc/bleh/config.ini"]
Note We're assuming your one and only docker-compose.yml
has these contents
version: '2'
services:
api:
build:
context: ./api
depends_on:
- db
expose:
- "8080"
networks:
- default
frontend:
build:
context: ./frontend
ports:
- "80:7000"
networks:
- default
webserver:
build:
context: ./webserver
depends_on:
- frontend
networks:
- default
proxy:
build:
context: ./proxy
ports:
- "80:80"
- "443:443"
depends_on:
- webserver
- api
networks:
- default
db:
build:
context: ./db
expose:
- "3306"
networks:
- default
networks:
default:
driver: bridge
Create docker-machine specifically for the bleh
project
docker-machine create bleh
PyCharm
/ Preferences
/ Build, Execution, Deployment
/ Docker
click +
Docker machine
radio button and highlight bleh
's docker machine in the pull downApply
PyCharm
/ Preferences
/ Project:bleh
/ Project Interpreter
Project Interpreter
field and select Add Remote
Docker
radio buttonServer
field, select previously created docker machine for this projectbleh_api
)Python interpreter path
neededOK
Run
/ Edit Configurations
select +
to add a configurationPython
Script
field, use location of script file on the docker container that will be run (in this example it's /usr/bin/bleh/__main__.py
as we're giving the absolute location of our target script)Script parameters
field, supply CLI parameters, if any (mimics the Dockerfile
's last CMD
command, which is --cfg=/etc/bleh/config.ini
)Python Interpreter
field, select your previously established remote python interpreterWorking directory
field, select the directory where Script
is located within the Docker container (e.g /usr/bin/bleh
)Path mappings
field, click the ...
and select local (e.g /Users/myfunkyusername/Projects/bleh/api/src
) and remote (e.g /usr/bin/bleh
) as aboveDocker container settings
field, click ...
bleh_api:latest
)Dockerfile
(e.g 8080/8080 and expose to 0.0.0.0
using the tcp
protocol, now I haven't shown what your app structure is, but let's assume that you were sane and within your app are also specifying 8080
as the port where your'e serving your data./usr/bin/bleh
/ /Users/myfunkyusername/Projects/bleh/api/src
Network mode
(thanks Piotr) is set to _
(e.g bleh_default
, you can confirm with docker network ls
from within the correct docker-machine
)These are the steps that got me to a working docker and PyCharm setup.
I don't pretend to be correct in each of these steps. I will gladly update any errors/improvements you find.