P
P
puker-ti2016-02-26 16:11:48
Django
puker-ti, 2016-02-26 16:11:48

How to effectively develop Python applications in Pycharm using Docker?

I use pycharm to develop Django applications.
The standard development process: we take a virtualenv, put everything there, in pycharm we specify this virtual machine as a SDK for the project, everything is great.
Having decided to use docker, I ran into some problems. In particular, I'm interested in the following:
0.1. In various guides, they put nginx into a container, it is not clear why this is done, if the production server will still have its own nginx.
1. If we refuse virtualenv and the whole environment is in our container, then how to specify the path to it in pycharm-e, and to be more precise, how to add containers from the local machine to docker-machine, because in pycharme the environment can only be taken through docker machine.
2. I also haven't found a solution yet - why pdb.set_trace() crashes immediately when triggered, when launched from a container.
3. And in general, I’m interested in how best to organize the directory structure for dockerfiles, compos, sorts, statics, media

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
sim3x, 2016-02-26
@sim3x

0. nginx (proxy server) is inserted into the container if the main proxy cannot be specified where you have the static
schema

#### server
балансер(прокси) | {твой контейнер: nginx -> [static_files, dynamic_django]
                 | {другой контейнер: nginx -> [static_files, dynamic]
....
#### server

1. https://github.com/docker-library/python/blob/12db...
python compiles to its default path /usr/local/bin/python
1. 2. blog.jetbrains.com/pycharm/2015/10/announcing-pych...
check paycharm for features or install EAP
3. It is desirable to make the structure as flat as possible - without a bunch of nested directories
https://docs.docker.com/compose/django/

P
puker-ti, 2016-02-29
@puker-ti

Well, either I explain very poorly, or the previous speaker could not understand me, or something else, but I found an answer to some of the questions:

  • 0.1.
    Based on a previous answer and judging by this link https://www.digitalocean.com/community/tutorials/d...
    Adding nginx to the container is just another layer for even more security, even more isolation.
  • 1.
    If we already have docker-engine installed in the system, and we want to start our own system for docker machine provisioning, then follow the same docs
    https://docs.docker.com/machine/overview/
    https://docs .docker.com/machine/drivers/generic/
    do
    those. connect via ssh to yourself.
    However, it is necessary to check whether there is a server in the ssh system which sshdand if not, then put one, for example. sudo apt-get install openssh-server
    However, that's not all, it will also be necessary to configure the ability to connect via ssh without a password.
    Perhaps the key --generic-ssh-keyfor will help here docker-machine create, but I didn’t understand this, but through I sudo visudoallowed my user to do everything without a password by adding the line username ALL=(ALL) NOPASSWD: ALL
    And now, finally, we have added our own system to docker-machine, and now we select an external sdk in paycharm, in the docker list machines, select ours and see all the containers in the system.
    ...However, pacharm will still decrement all imports in red
  • 2.
    judging by these questions
    stackoverflow.com/questions/13715725/python-pdb-ex...
    stackoverflow.com/questions/9178751/use-pdb-set-tr...
    I understand that stdin is busy with something and there is no place for our miserable debugger. Of the possible solutions:
    redirect the input of the pdb somewhere else, or use another debugger that allows this out of the box ... just everything is somehow inconvenient. Although if someone can formally describe the steps on how to do this, preferably with ipdb, it will be cool.
  • 3.
    https://realpython.com/blog/python/django-developm...
    here, in my opinion, is quite an adequate tree, if you can, throw repositories with good projects where you can see it live.
    ├── docker-compose.yml
    ├── nginx
    │   ├── Dockerfile
    │   └── sites-enabled
    │       └── django_project
    ├── production.yml
    └── web
        ├── Dockerfile
        ├── docker_django
        │   ├── __init__.py
        │   ├── apps
        │   │   ├── __init__.py
        │   │   └── todo
        │   │       ├── __init__.py
        │   │       ├── admin.py
        │   │       ├── models.py
        │   │       ├── templates
        │   │       │   ├── _base.html
        │   │       │   └── home.html
        │   │       ├── tests.py
        │   │       ├── urls.py
        │   │       └── views.py
        │   ├── settings.py
        │   ├── urls.py
        │   └── wsgi.py
        ├── manage.py
        ├── requirements.txt
        └── static
            └── main.css

F
funca, 2016-03-03
@funca

Two options:
1. Use PyCharm's native docker support.
The integration seems to be designed for docker-machine and boot2docker. But if you want to use docker from the host system, then setting up docker-machine and ssh is not necessary. Instead, you can use the stub script from https://youtrack.jetbrains.com/issue/PY-17454#comm... Specify the path to the docker-machine stub in the PyCharm settings.
You need to add the user "docker" with the password "tcuser" to the system. These details are used by the debugger.

sudo useradd --system --create-home --home /var/lib/docker docker
echo 'docker:tcuser' | chpasswd

If the debugger cannot connect to the container, it fixes it by adding the IP address 10.0.2.2/8 to the docker0 interface:
sudo iptables -A INPUT -i docker0 -j ACCEPT
sudo ip addr add 10.0.2.2/8 dev docker0 \
        && (sudo ip link set dev docker0 down; sudo ip link set dev docker0 up) ||:

There is no integration with docker-compose. PyCharm will run the containers on its own, so you will have to tie with external services separately. Through .env files, for example.
2. Use normal remote access via SSH.
In this case, for development, you need to create a separate image, with pre-installed sshd and development tools.
# за основу берется проектный image
FROM myproject

# sshd
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
           openssh-server \
           vim \
    && rm -rf /var/lib/apt/lists/* \
    && mkdir -p -m0755 /var/run/sshd \
    && echo 'root:screencast' | chpasswd \
    && sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config \
    && sed '[email protected]\s*required\s*[email protected] optional [email protected]' -i /etc/pam.d/sshd \
    && echo "export VISIBLE=now" >> /etc/profile

# PyCharm хочет исходники в /opt/project
WORKDIR /opt/project

# автоматически активировать virtualenv при логине root 
RUN echo "\ncd /opt/project; if [ ! -f env/bin/activate ]; then virtualenv env; fi; . env/bin/activate" >> /root/.bashrc

# ssh, django runserver
EXPOSE 22 8000
CMD ["/usr/sbin/sshd", "-D"]

After that, the project and dependencies can be run once via docker-compose up -d, and in PyCharm, you can configure integration with the container via normal remote access via SSH.
# docker-compose-dev.yml
# vim: sw=2 ts=2
version: '2'
services:
  web:
    build: .
    links:
      - db
    volumes:
      - ./:/opt/project
      - ./data/env:/opt/project/env
    ports:
      - "127.0.0.1:2222:22"
      - "127.0.0.1:8000:8000"
  db:
    image: mysql
    volumes:
      - ./data/mysql:/var/lib/mysql
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_DATABASE=mydb
    ports:
      - "3306:3306"

In the example, the sources, virtualenv, and data directory for MySQL are mounted via volume so that they are not lost when recreating containers.
I use this option because it is noticeably faster.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question