Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker with pycharm 5

I try to build a docker-based development box for our django app. It's running smoothly.

None of my teammembers will care about that until there is a nice IDE integration, therefore I play the new and shiny Docker Support in pycharm 5.

I followed the linked documentation and pycharm does recognise my web container and it's python interpreter.

Here's my docker-compose.yml:

web:
  build: .
  ports:
    - "8000:8000"
  volumes:
    - .:/srv/app
  links:
    - database
    - search
    - cache
  entrypoint: /home/deployer/web-entrypoint.sh


worker:
  build: .
  volumes:
    - .:/srv/app
  command: celery -A core worker -l info
  links:
    - database
    - search
    - cache

database:
  image: postgres:latest
  volumes_from:
    - data
  environment:
    - POSTGRES_USER=app_user
    - POSTGRES_PASSWORD=app_password

data:
  image: busybox
  volumes:
    - /var/lib/postgresql/data

search:
  image: "elasticsearch:1.7"
  command: "elasticsearch --http.bind_host=0.0.0.0"
  ports:
    - "9200:9200"

cache:
  image: "redis:latest"
  ports:
    - "6379"

Unfortunately there is no docker-compose support in pycharm, that's why djangos runserver failed upon connecting to the database. Therefore I copied the (fortunately predictable) aliases from the web container's /etc/host:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'app_db',
        'USER': 'app_user',
        'PASSWORD': 'app_password',
        'HOST': 'docker_database_1',
        'PORT': '5432',
    }
}

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://docker_search_1:9200/',
        'INDEX_NAME': 'app',
    },
}

BROKER_URL = 'redis://docker_cache_1:6379/0'
CELERY_RESULT_BACKEND = BROKER_URL

Now the database connection error is no longer there, but the output of my django server gives me this:

a6993f56e61e:python -u /opt/project/manage.py runserver docker:8001 --traceback
Performing system checks...

System check identified no issues (0 silenced).
November 08, 2015 - 19:54:29
Django version 1.8.6, using settings 'core.settings.dev'
Starting development server at http://docker:8001/
Quit the server with CONTROL-C.
Error: [Errno -2] Name or service not known

Process finished with exit code 1

No stack trace, just this.

What's strange: python -u /opt/project/manage.py - what's this? The folder does not exist on both host and the container.

My Django Server conf:

Django server conf

I tried as well a pure-python conf like this:

Pure Python Implementation

This is like mega-confusing because it tries again to connect via the "database" link, even if I remove it from the settings at all.

What would be the next steps for debugging?

Bonus question: pyCharm does recognize the installed packages in the project settings, but it can't find it in the code, why?

Failed import from dist-packages

UPDATE

I found out that pyCharm is starting the container for itself and is not using the existing docker container. Hence it looks like pyCharm can only work with a single container, which does not seem to be that useful at all.

like image 603
shredding Avatar asked Nov 08 '15 20:11

shredding


People also ask

Can I use PyCharm with Docker?

PyCharm provides Docker support using the Docker plugin. The plugin is bundled and enabled by default in PyCharm Professional Edition. For PyCharm Community Edition, you need to install the Docker plugin as described in Install plugins.

How do I add Docker to PyCharm interpreter?

Configuring Docker as a remote interpreter Click the Python Interpreter selector and choose Add Interpreter. Press Ctrl+Alt+S to open the project Settings/Preferences and go to Project <project name> | Python Interpreter. Click the Add Interpreter link next to the list of the available interpreters.

What does IP 0.0 0.0 mean Docker?

0.0.0.0 means all available interfaces which does include localhost but also others e.g. 192.168.0.123. What you use to make content available matters, e.g 0.0. 0.0 vs 127.0. 0.1 but also what you use to connect too.

Does Docker offer support for IPv6?

Before you can use IPv6 in Docker containers or swarm services, you need to enable IPv6 support in the Docker daemon. Afterward, you can choose to use either IPv4 or IPv6 (or both) with any container, service, or network. Note: IPv6 networking is only supported on Docker daemons running on Linux hosts.


2 Answers

It turns out that pycharm 5 supports only one container per project. Basically that translates to "Docker support is useless in pyCharm 5".

Multi-container management on top of docker compose is requested here and is awaiting YOUR upvote:

https://youtrack.jetbrains.com/issue/IDEA-137765

like image 55
shredding Avatar answered Oct 23 '22 13:10

shredding


I've got a solution. I setup a docker-compose somewhat like yours (postgres, redis, solr). It's set to use a custom DJANGO_SETTINGS_MODULE (called settings.docker) that looks for a DOCKER_IP environment variable to use in the services. All works fine.

Then was trying Pycharm out to get it working with the docker integration since I'm running Windows (hence docker-machine), I can only do a docker-compose run in detached mode. Not very useful for a shell. But the one in Pycharm (as you found out) runs another container and the shell works fine. So you get your django shell even on a windows/mac machine that has to run in detached mode.

Also tests, runserver and any other manage command work fine within PyCharm and debug/breakpoints work.

runserver configuration: runserver configuration

interpreter: interpreter

In settings.docker we have

DOCKER_IP = os.environ.get('DOCKER_IP', '127.0.0.1')
# database
DATABASES = {
    'default': {
        'ENGINE': 'transaction_hooks.backends.postgis',
        'USER': 'postgres',
        'NAME': "dbuser",
        'HOST': DOCKER_IP,
        'POST': 5432,
        'CONN_MAX_AGE': None,
    },
}
# redis
REDIS_URL = "redis://%s:6379/1" % DOCKER_IP

You get the idea

like image 42
dalore Avatar answered Oct 23 '22 15:10

dalore