docker with pycharm 5

2019-04-08 11:39发布

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.

2条回答
放我归山
2楼-- · 2019-04-08 11:58

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

查看更多
我命由我不由天
3楼-- · 2019-04-08 12:03

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

查看更多
登录 后发表回答