Skip to content

Instantly share code, notes, and snippets.

@atenni
Last active July 25, 2018 05:31
Show Gist options
  • Save atenni/dad0657e0dd317021a68a8086f4256d7 to your computer and use it in GitHub Desktop.
Save atenni/dad0657e0dd317021a68a8086f4256d7 to your computer and use it in GitHub Desktop.
Quickstart: Docker backed PyCharm project

Docker backed Python project using PyCharm

This quickstart assumes OS X 10.13, PyCharm 2018.1, Docker 18.03, and Python 3.7.

  1. Start Docker and PyCharm
  2. Create new project - "Pure Python"
  3. Set project name and temporarily set the project interpreter to Existing interpreter > Python 3.x (local)
  4. Create Dockerfile and/or docker-compose.yml (see samples of each below), and optionally an "output" folder.
  5. Now reset the project interpreter to Docker by going to Preferences > Project Interpreter > [Cog wheel] Add... and choose Docker Compose.
    • You may need to "Run" the docoker-compose.yml file in order to create the containers initially. I'm not 100% - I was having some Docker/PyCharm challenges when writing this...

Now when you run a .py file it will execute in the Docker container, not the local interpreter.


Sample Dockerfile

This will start a Python 3 container with a /code/ directory in it. It will then copy and install your requirements, and then copy in your code.

FROM python:3
ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code

## Uncomment if using requirements.txt
COPY requirements.txt /code/
RUN pip install -r requirements.txt

COPY . /code/

Sample docker-compose.yml

This will build a container called "app" based on the Dockerfile above and mirror the local "output" folder to /code/output/.

version: '3'

services:

  app:
    build: .
    volumes:
      - ./output/:/code/output/

    # Debug
    tty: true
    stdin_open: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment