Docker-Compose: Getting Flask up and running

A couple of issues were resolved in PyCharm 2017.1, and Docker for Mac should now work out of the box. In this blog post we’ll show you how to set up a project with Docker Compose on a Mac. If you’re on Linux, the process is very similar. Docker Compose is supported on Windows from PyCharm 2017.2.

In this tutorial we’ll show how to create a very simple ‘Hello World’ Flask application, and then how to run it within a Docker container. For the full code, and to follow along, see GitHub: https://github.com/ErnstHaagsman/flask-compose.

First things first

Before we get started, let’s check a couple of things: make sure you are using PyCharm 2017.1 Professional Edition or later. Docker support is not available in PyCharm Community Edition. Then, please ensure your Docker and Docker Compose are up to date. To check, open a terminal, and run `docker -v`, and `docker-compose -v`:

Docker and Compose Versions

Then, although it is the default setting in PyCharm 2017.1, it never hurts to check that your Docker API URL (Preferences | Build, Execution, Deployment | Docker) is set to `unix:///var/run/docker.sock`.

Docker Settings

Now we can create a new Flask project. Let’s create a virtualenv so that PyCharm can stub out our Flask project before we have Docker configured. You can create a Virtualenv by using the ‘…’ button next to the interpreter dropdown.

Create a Flask Project

Dockerizing Flask

After you click create, you should see the standard “Hello World” Flask template. So let’s see if we can get Flask to show us “Hello World” from a Docker container. To do this, we’ll add four files:

  • requirements.txt, just put “Flask==0.12” here to install Flask
  • Dockerfile, where we will set up the Python environment for the Flask app
  • docker-compose.yml to set up how to run the Dockerfile, and add a database
  • docker-compose.dev.yml to make some changes for local development

In the Dockerfile, we’ll use the ‘python:3’ image, expose port 5000, install packages from requirements.txt, and afterwards copy the rest of our project into /app. See the full file on GitHub.

Then, we’ll create a Compose file where we define the Flask app as our only service (for now). Just use `build: .` to make Docker Compose build the container from the Dockerfile.

Docker will bake our code into the image, and that way the image is self-contained. If we wanted to, we could stop here and rebuild our image whenever we change the code. It makes development quite a bit faster to mount our code with a volume. So we’ll create an additional Compose file to do that while we’re developing.

In docker-compose.dev.yml, all we’re doing is adding a volume mapping for `.:/app` for the `web` service. This overlays the code in the container with a volume, making sure that code changes are applied immediately. Keep in mind that you will need to rebuild the image before pushing it anywhere.

Now that everything is configured, let’s quickly run `docker-compose up` in the Terminal to make sure that works. Open the terminal with Alt+F12 (in PyCharm) and run `docker-compose up`.

Docker-compose up on the terminal

As we can see, Docker built our container, and Flask is running! Let’s go and check out our ‘Hello World’ message!

A trap for young players!

Safari couldn't open the page

What happened here? Didn’t docker say that Flask was running?

For security reasons, many modern web frameworks actually limit incoming connections to only come from localhost. This means that our Flask app is running, but only accessible from within the Docker container, not from our Mac host. The easiest way to fix this is to add `host=’’` as a keyword argument to `app.run()` in flask-compose.py:

Flask, exposed

This change tells Flask to listen not only to requests from localhost (in this case the docker container), but on all network connections. In the case of our container this means we can access it from our host, and depending on configuration from other computers in our network.

If we now stop our container (Ctrl+C in the PyCharm Terminal), and rebuild it by running `docker-compose up –build`. We can see in the Docker Compose output that Flask is now listening on instead of

Flask is listening on

And reloading the page in Safari actually shows ‘Hello World’ now. So let’s stop the container (Ctrl+C in the Terminal), and then configure PyCharm.

Configuring PyCharm

To make our project run using a PyCharm run configuration, we need to set the project interpreter to the Docker Compose service. We can do this from the Project interpreter page in preferences: Preferences | Project: <Project Name> | Project Interpreter.

Click the little button next to the interpreter dropdown (the white one, not the blue one), and choose “Add Remote”. If you don’t see “Add Remote” here, you may be using PyCharm Community Edition, which doesn’t support remote interpreters.

Add Remote Interpreter

Now choose “docker-compose”, and almost everything will be pre-configured, I only had to add the `docker-compose.dev.yml` configuration to the list. We need to add it manually as this isn’t part of a standard docker project. You can do this with the ‘+’ button underneath the list of configuration files.

Compose Configuration in PyCharm

Now, we should set up the path mapping. We are inserting all our project code into the container’s “/app” directory. So let’s add this on the Project Interpreter screen, use the ‘…’ button next to the Path mappings field to change them.

Path mappings

After you close the preferences window, you can just use the regular Run and Debug buttons to start and debug your project:

Debugging Flask within Docker

To celebrate, let’s change “Hello World!” to “Hello from Docker within PyCharm!”

Flask in Docker-Compose

And that’s all there’s to it! Have fun developing with Docker Compose! If you’d like to learn more, you can read the second part of this tutorial about adding a database service.

PyCharm Team
-The Drive to Develop

Comments below can no longer be edited.

63 Responses to Docker-Compose: Getting Flask up and running

  1. Avatar

    yangyoungil says:

    March 29, 2017

    is pycharm will support windows with docker later ?

    • Avatar

      Christian Gilbert says:

      March 29, 2017

      You can use pycharm with docker on windows but you have to mount the folder that you are working via windows share and cifs on with the hyper-v machine. I can share you a screenshot if you want

      • Avatar

        NPann says:

        April 14, 2017

        Hi Christian. I would appreciate if you can share this screenshot. Thanks.

      • Avatar

        Pezo says:

        May 5, 2017

        Yes, please share, would help a lot!

      • Avatar

        G says:

        August 1, 2017

        please share, would really help a lot!!

    • Avatar

      Ernst Haagsman says:

      August 1, 2017

      PyCharm 2017.2 supports Docker on Windows.

  2. Avatar

    Reinoud says:

    March 29, 2017


    When I try this I get

    TLS configuration is invalid – make sure your DOCKER_TLS_VERIFY and DOCKER_CERT_PATH are set correctly.
    You might need to run `eval “$(docker-machine env default)”`

    (might be some leftovers from old docker installations…)
    How to fix this?

    (docker-machine env default gives: Host does not exist: “default”)

    • Avatar

      Reinoud says:

      March 30, 2017

      found it: make the cert path empty in the config

      • Avatar

        Wil Black says:

        April 11, 2017

        Hey, i am running into this problem as well. How do you “make the cert path empty in the config”?

        • Avatar

          Ernst Haagsman says:

          April 12, 2017

          Open Settings | Build, Execution, Deployment | Docker, and then empty out the textbox for ‘Certificates folder’ there.

          • Avatar

            Gary Harpaz says:

            March 4, 2019

            I’m using pycharm pro on linux, docker daemon is via unix socket. Textbox is grayed and empty in my case. I don’t even have docker-machine installed.
            Still getting this strange error:
            : TLS configuration is invalid – make sure your DOCKER_TLS_VERIFY and DOCKER_CERT_PATH are set correctly.
            You might need to run `eval “$(docker-machine env default)”`

            Process finished with exit code 1

  3. Avatar

    Christian Sauer says:

    March 29, 2017

    Wow, very nice feature!
    Unfortunately, we are using windows, so we cannot use it 🙁

  4. Avatar

    Johann Visagie says:

    March 29, 2017

    The arrival of “proper” Compose support in 2017.1 has been as frustrating as it has been long-awaited. Essentially, it arrived just in time to be obsolete.

    For one thing, PyCharm doesn’t support the new Version 3 of the compose file format, which is the now the version recommended by Docker, Inc. I had to roll back a few projects to Version 2 compose files for them to work with PyCharm. (Basically it’s forcing me to maintain git branches with both Version 2 and Version 3 compose files.)

    And secondly, Version 3 compose files are themselves a stepping stone to the complete phasing out of Docker Compose in favour of the newer Docker Stacks — which, obviously, aren’t supported by PyCharm at all yet.

    I’m not blaming the JetBrains guys. Docker is currently iterating at high speed… much faster than an IDE’s release cycle.

  5. Avatar

    Jp says:

    March 29, 2017

    Does this also work on Windows with the new docker windows client?

    • Avatar

      Ernst Haagsman says:

      March 29, 2017

      Unfortunately, it doesn’t. It’s hard for us to do things with Docker on Windows because of Hyper-V. After installing Hyper-V on Windows you can’t run other virtualization software anymore. We’d essentially need to use separate hardware to develop our support for Docker on Windows.

      How are you using Docker on Windows? Are you using it natively or are you connecting to a docker server running remotely, or in VMWare, or Virtualbox?

      • Avatar

        Christian Gilbert says:

        March 29, 2017

        You can use pycharm with docker on windows but you have to mount the folder that you are working via windows share and cifs on with the hyper-v machine. I can share you a screenshot if you want

      • Avatar

        Jonathan says:

        April 4, 2017

        I’m using docker-compose on Windows (natively with hyper-v, no virtual machine) but hit a wall with PyCharm. Right now, I setup my app to provide only dependencies and run the app on Windows, but I would much rather have the app also run in docker as it is possible on Linux.

        • Avatar

          Ernst Haagsman says:

          April 11, 2017

          Hi Jonathan, currently we don’t support Docker Compose on Windows unfortunately. Hyper-V is incompatible with other virtualization, and therefore we’d need to work on separate hardware to develop Docker for Windows. This means it’s very hard for us to work on supporting Docker for Windows.

          • Avatar

            Aleksey says:

            May 17, 2017

            Just curious – what’s specific about Compose? You do support “bare” Docker for Windows (or at least in works for me, huh), just not the Compose. And Compose doesn’t really care about the OS (sans a few oddities), it mostly just talks to the Docker API to spawn stuff as described, (almost) blindly passing the params there.

          • Avatar

            Yannick Majoros says:

            May 24, 2017


            Still, that’s what we’re using. Not supporting Windows is a major drawback that could lead us back to Netbeans.

  6. Avatar

    Ryan Bowlby says:

    March 30, 2017

    I’m running into an issue where the docker-compose in pycharm is not sharing the local volumes that I have specified in the compose file.

    I was able to quit out of pycharm, run docker-compose up manually, stop the containers, and then for some reason pycharm worked on next run. Perhaps a bug? Oddity for sure.

    • Avatar

      Ernst Haagsman says:

      April 11, 2017

      Could be, if you run into the situation again, please send us your idea.log file so we can investigate what went wrong. Thank you very much!

      • Avatar

        Daniel Diaz says:

        May 13, 2017

        I was having an issue where it seemed like the docker-comose.dev.yml was not mounting my app, so I couldn’t see my changes reflected without restarting the app every time. I changed the file name to docker-compose.override.yml, and that seems to have fixed the problem.

        • Avatar

          Ernst Haagsman says:

          May 14, 2017

          That’s strange, if you can reproduce the problem, could you perhaps send your idea.log to pycharm-support@jetbrains.com?

          • Avatar

            Daniel Diaz says:

            August 30, 2017

            I was able to figure it out. The flask app needs to be in DEBUG mode to auto reload code changes. Thanks

          • Avatar

            Guillaume Troupel says:

            October 21, 2017

            As Daniel suggested, flask needs to be in DEBUG mode.
            It should be `app.run(host=’′, debug=True)`

  7. Avatar

    Aleksander Czarnowski says:

    April 1, 2017

    Honestly speaking selecting flask app without database support for demonstrating Docker related IDE functionality is a bad choice IMHO since it is trivial to do it with simple Dockerfile and no IDE at all in the first place. The only possible (and trivial) catch to the whole – already noted in the post – is setting up host=’′ so the app can be accessible outside of container.

    Agree with comments that it is way too late, not to mention I am still using Windows so the whole thing is double useless.

  8. Avatar

    Henry says:

    April 10, 2017

    Thanks for the article, the binding to localhost instead of was tripping me up.
    Also, running python inside a virtualenv inside a docker container *is* possible, but it requires a wrapper script to be created which activates the virtualenv and executes whatever python command. This is then specified as the python interpreter instead of any particular binary.

  9. Avatar

    Josh says:

    April 10, 2017


    I am using Docker for Windows with pycharm. Would love to see docker-compose support.

  10. Avatar

    Pezo says:

    May 5, 2017

    Dear JetBrains/PyCharm guys!

    I’m a mac/linux user, but the fact PyCharm can’t support Windows effects even me. In every team, where platform independency is a must, due windows platform it’s not evident to go with PyCharm, if the team can not use it as a whole. You know how it works… if anybody who is a bit afraid of PyCharm will tell the “management” about this leak, or just argue due that and the whole boat with mac and linux people will sink as well.

    “There is always a windows guy in the team.”

    Can you say anything about its support? Even if you can mark a date until it’s not going to happen or a date until it might happen could help.

  11. Avatar

    Vilmos Ágoston says:

    May 14, 2017


    I am using Docker for Windows with pycharm. Would love to see docker-compose support.

  12. Avatar

    Or says:

    May 14, 2017

    Thanks for this awesome info!
    Could you do one for minikube? Making pycharm work with kubernetes/minikube is one of my life goal!

  13. Avatar

    Patrick says:

    May 17, 2017

    What’s Happening i am new to this, I stumbled upon this I have found It absolutely useful and it has helped me out loads. I hope to contribute & help other users like its aided me. Good job.

  14. Avatar

    cecemel says:

    May 23, 2017


    awesome feature. I just managed to make it work (with pyramid). However, when debugging, it seems I am unable to use the console in a breakpoint. (even though the variables etc. seem to be known by the debugger and breakpoints work)
    Any ideas?

  15. Avatar

    Ilya says:

    May 25, 2017


    I am using Docker for Windows with pycharm. Would love to see docker-compose support.

  16. Avatar

    Pedro Liévana says:

    May 30, 2017

    Firstable, thanks to Ernst Haagsman per this post. For us is very usefull. I have follow every step and have this app up and running. Only thing I found is when I click on “python console” tool, I get an error:
    “Error: Unable to locate container name for service “web” from docker-compose output”

    I also found a user in stackoverflow with the same issue:

    Python console is a very powerfull tool for us.

    We are running pycharm 2017.1.3 on ubuntu 14.04 and:
    – docker 17.03.1-ce
    – docker-compose 1.13.0
    – docker-py: 2.2.1

    Any help?

  17. Avatar

    Tangent Lin says:

    July 11, 2017

    I am an IntelliJ IDEA Ultimate developers, and I have been trying to follow this tutorial to make Python debugging with Docker using IntelliJ, but there are a few steps that I couldn’t quite locate in IntelliJ Ultimate 17.2 EAP. For example, I cannot seem to be able to locate Project Path Mapping dialog, and add remote interpreter was an option quite hidden in IntelliJ as well. I wonder if you could point me the directions to get the same functionality working in IntelliJ IDEA, thanks!

  18. Avatar

    Mykola says:

    July 29, 2017

    PyCharm has terrible performance while updating skeletons for docker-compose.

    For example it takes 7+ mins for this hello-world project on Intel i7-4790K 4.0 GHz (4-core), 16 Gb RAM, SSD 500 MB/s.

    During updating skeletons step PC is almost idle (CPU usage is 1-2%).

    Other interesting bugs:

    – PY-25410 [docker-compose] Impossible to Configure Timeouts for Stop / Restart

    – PY-25411 [docker-compose] Double click on “Stop” button Doesn’t Kill Containers

    – PyCharm Doesn’t Support docker-compose format version 3+ (recommended by Docker)

    Conclusion: support of docker-compose in Pycharm is ugly.

    Think twice before buying Pycharm / IntelliJ Professional for software development with docker-compose support!

    • Avatar

      Ernst Haagsman says:

      July 31, 2017

      Thank you very much for reporting these issues. We’ll look into these issues to see why your hardware isn’t getting you better results.

      We’re currently working on Docker Compose version 3, please stay tuned for the PyCharm 2017.3 EAP.

  19. Avatar

    cecemel says:

    September 6, 2017


    solution works great!

    Is there a way to debug the dependencies installed, without having to each time copy these files locally too?

    • Avatar

      Ernst Haagsman says:

      September 6, 2017

      Unfortunately, no. If we were to keep the dependencies only in the container, access to them would be very slow, and it would impede the development experience. We’re currently working on ways to optimize the synchronization process, however, you’ll always need a local copy of the files.

      • Avatar

        cecemel says:

        September 7, 2017


        So if see how it works now, each time you start a run a new cache folder is created with a copy of the library files.
        It would be cool, if pycharm would create this once and keep it it the same, until you ask pycharm to update these. That way, if you need to put breakpoints before pycharm dowloaded the files (e.g. on init of a module), you can do so without manually putting the files somewhere and mapping it etc…

  20. Avatar

    cecemel says:

    September 7, 2017

    other question: what would be the easiest way to debug two services a the same time. e.g. Service A calling Service B.

  21. Avatar

    Kopei says:

    September 14, 2017

    Hi, I followed this post but stuck at when I tried to add another docker-compose-dev.yml in configuring remote python interpreter. It has a Java Error on GUI:

    Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token at balabala…

    any ideas?

  22. Avatar

    Sri Hari says:

    November 7, 2017


    I’m trying to installing Python with Docker compose file in docker.
    I’m using docker in my Windows 10 system.
    So please help me.. How can i resolve this issue


    • Avatar

      Ernst Haagsman says:

      November 7, 2017

      Hi! Which issue are you facing?

      • Avatar

        Sri Hari says:

        November 8, 2017

        I worte the docker file like this
        FROM python:3.6.3

        WORKDIR /usr/src/app

        ADD requirements.txt /app/requirements.txt
        RUN pip install sfdclib
        Run pip install -r requirements.txt

        COPY . .

        CMD [ “python”, “SfdcData.py” ]

        May i know this is right process or could you please tell how to write the docker file for python

  23. Avatar

    Sri Hari says:

    November 8, 2017


    If possible could you please send me any referral link for how to write the python compose file or python docker file..

    My requirement i have to install python in my system through docker with docker compose or docker file…

    Because i wrote some python scripts i need dockerize all the scripts for this i need python in docker..

    I’m running docker in my windows 10..

    please help me…


  24. Avatar

    blackcater says:

    November 30, 2017


  25. Avatar

    Anthoni Gardner says:

    December 1, 2017

    [PyCharm 2017.3, Linux Mint 18 Sonya)

    OK I followed everything and got it working brilliantly. Some things have changed, but that’s progress. For example I did not have to do the linux socket thing, as it was now just a radio button to select 😀

    However, I added
    app.run(host=’′, debug=True)

    Then ran it plainly from PyCharm and tried to modify the returned results. It did not reload automatically. I tried debugging and modify the result again, but still it refused to refresh.

    What I ended up doing was stopping the process, then in the terminal ran
    docker-compose build

    then ran it as normal in order to see the changes.
    Why is not refreshing ?
    Is this a limitation of the system or am I missing something ?


    • Avatar

      Ernst Haagsman says:

      December 4, 2017

      Hi Anthoni!

      To make file changes visible in the container, you need to use a volume to overlay the code you’ve added with an ‘ADD’ statement in the Dockerfile. In this example, we’re configuring this in the docker-compose.dev.yml file. Did you add this file in the interpreter settings? Without that addition you’d need to rebuild your containers after every change.

      Let us know how things go!

  26. Avatar

    Pitt says:

    January 30, 2018

    Could you please share your run/debugging configuration in PyCharm?

    I’m having trouble getting this to work in PyCharm 2017.4.
    I can run app fine via my commandline and running docker-compose, however when I run the debugger in PyCharm it only prints in the console that it is ‘Waiting for connection…’ and after a minute the docker container is killed:

    “`4818833b71c5:python -u /opt/.pycharm_helpers/pydev/pydevd.py –multiprocess –qt-support=auto –port 55223 –file /app/service/__init__.py

    Process finished with exit code 137 (interrupted by signal 9: SIGKILL)“`

    I created a Python config to run the app with the remote docker-compose interpreter, and I set up the path mapping to be equivalent to my Dockerfile and docker-compose files.
    When I run `docker ps` while the debugger is `waiting for connection..` I can see that the docker container is running and exposing the debugging port (55223 in this case).

    Any idea how I can get my debugger to work?

  27. Avatar

    Yair Fried says:

    February 7, 2018

    Nice tutorial. Thank you.
    If you are using RedHat based Linux and get a Permission Denied error – try to disable SELinux

  28. Avatar

    Zhiliang says:

    August 22, 2018

    The last step (use docker compose as the project interpreter) doesn’t seem to work on Pycharm 2018.2 with Docker 18.06.
    The mapped port doesn’t response anything on the host machine.

  29. Avatar

    Monit says:

    October 19, 2018


    Thanks for the informative blog. But, I am running into a strange error and not able to find any fix so far. While configuring the docker-compose python interpreter, I get the following error:

    : Couldn’t find ‘docker’ binary. You might need to install docker.

    This is strange as I am already running the project from the docker container. Any help would be really appreciated. Thanks!

    • Avatar

      Ernst Haagsman says:

      October 24, 2018

      Could you please check the settings in Settings | Build, Execution, Deployment | Docker?

  30. Avatar

    Rares says:

    October 20, 2018

    Same here.
    The guide doesn’t work with 2018.2 and Docker 18.06.1-ce-mac73 (26764)
    Specifically, starting flask from PyCharm with the docker remote interpreter binds to and port 5000 regardless of the host and port kwargs

    • Avatar

      Ernst Haagsman says:

      October 24, 2018

      There have been some changes in Flask since this blog post was written. Use a Flask run configuration rather than a Python run configuration, and provide the new host and port in ‘Additional Options’. See the Flask run configuration docs for more details

      • Avatar

        Alex says:

        November 23, 2018

        Worked like a “charm”! Thank you!

  31. Avatar

    Toni says:

    February 1, 2019

    Thank you for this article, it really helped me to make my debugger work.
    I forgot to add the path mapping to the remote interpreter and the debugging was not working (I assumes that the path mapping is taken from docker-compose file).

Discover more