Attaching to a docker container.

I have just started using the Docker integration but have a query.

I am developing a Django app and have setup the Python interpreter to be in a docker container, this is setup from a docker compose file. Running and debugging is working fine for this.

I was wondering if there is any way to attach to one of the docker containers from the Django run/debug configuration as you can when using the docker compose run/debug configuration.

4 comments
Comment actions Permalink
Hi, Using Django server run/debug configuration with the Docker compose interpreter will automatically start the container and mount your code inside. This also allows you to use the debugger. Using Docker-compose run/debug configuration, on the other hand, is the same as running `docker-compose up` from the terminal. It doesn't allow debugging Python code. Could you please provide more details about what do you want to achieve?
0
Comment actions Permalink

Hi Andrey thanks for responding.

Basically what I have is a docker compose file that brings up a database and python docker container. The python docker container houses the django app. The python interpreter is set up to use the python interpreter within the python container.

What I was hoping to have was while in run or debug mode the terminal would be attached to the command line in the container that python is running under.

The docker compose file contains the database credentials and creates environment variables from them.

I may be thinking this the wrong way round but the reason it would be useful would be to run migrations on the database. This would provide me with the ability to use the same interaction approach and to always interact within the container. My other aim was to make it easier for anyone else to get started with a developer environment

 

0
Comment actions Permalink
I see, thanks for the explanation. 
 
You can enter a terminal shell for any running container from the Services tool window.
 
However, if you're using bind mounts/volumes (and I don't see why wouldn't you), a good way to run migrations is to fire up another container from the terminal, something like:
 
docker-compose run <django_service_name> python manage.py migrate 
 
It will start another instance of your Django service using the environment defined in the compose file, having access to the database container and using the same binds/volumes that your main instance is using, run the migrations and shut down. Hope it makes sense.
0
Comment actions Permalink

Hey Andrey thanks for the response. I wasn’t sure if the terminal window in pycharm could default to being an exec’d /bin/sh in the running main container. Appreciate running docker to bring up another instance is possible. Will also take a look at the services pane. Did t realise that was an option as well.

Cheers again

0

Please sign in to leave a comment.