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. Unfortunately, we don’t support Docker Compose on Windows at this time. If you’re using Docker Compose on Windows, please let us know in the comments.

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:

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
  • 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, 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 in

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 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

This entry was posted in Tutorial and tagged . Bookmark the permalink.

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

  1. yangyoungil says:

    is pycharm will support windows with docker later ?

    • Christian Gilbert says:

      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

  2. Reinoud says:


    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”)

  3. Christian Sauer says:

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

  4. Johann Visagie says:

    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. Jp says:

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

    • Ernst Haagsman says:

      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?

      • Christian Gilbert says:

        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

      • Jonathan says:

        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.

        • Ernst Haagsman says:

          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.

  6. Ryan Bowlby says:

    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.

    • Ernst Haagsman says:

      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!

  7. Aleksander Czarnowski says:

    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. Henry says:

    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. Josh says:


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

  10. Pingback: Docker-Compose: Adding a Database Service | PyCharm Blog

  11. Pingback: PyCharm: Docker-Compose: Adding a Database Service | Adrian Tudor Web Designer and Programmer

Leave a Reply

Your email address will not be published. Required fields are marked *