When I tried to create a project using Python on Docker with PyCharm, it didn't work, but it worked with Docker Compose.

Thing you want to do

When I run a Python program in PyCharm, I want to run it in a container inside Docker instead of Python installed on my local PC. Specifically, I want to run a Flask web application from PyCharm on Docker.

Click here for the environment.

This feature is not available in PyCharm Community Edition.

If you write in 3 lines

--When I create a new project with [New Project] from PyCharm, it doesn't work. --Create folders and files, then open with PyCharm --Use Docker Compose instead of Docker

How it didn't work

First I set it to PyCharm

Start PyCharm and select [Configure]-[Preference]

スクリーンショット 2020-09-25 11.11.17.png

Select [Python Interpreter] and click the gear icon on the right edge of the screen → [Add ...]

スクリーンショット 2020-09-25 11.11.56.png

Select [Docker] → click [New] under [Server]

スクリーンショット 2020-09-25 11.15.11.png

Select [Docker for Mac] → Confirm that [Connection Successful] is displayed at the bottom of the screen, and then click [OK].

スクリーンショット 2020-09-25 11.19.14.png

Enter python: 3.8 in [Image name] and python in [Python interpreter path] → [OK]

スクリーンショット 2020-09-25 11.21.29.png

Confirm that [Python interpreter] is as follows → [OK]

I can't create a new project.

Select [New Project]

スクリーンショット 2020-09-25 11.24.04.png

Select [Existing interpreter] → Select Python in Docker created by [Interpreter]

スクリーンショット 2020-09-25 11.26.13.png

It says [Remote path not provided], so enter an appropriate path. Then, "This interpreter type does not support remote project creation" is said. [Create] cannot be pressed.

スクリーンショット 2020-09-25 11.27.47.png

Search for the cause

For the time being, if you google with the error message that came out earlier, the official support page and YouTrack (JetBrains issue tracking system) will appear, so let's take a look.

スクリーンショット 2020-09-25 11.29.10.png

First, Support Page Seen from, it seems that the same phenomenon occurs.

The answer says, "Create a project in Python on your system and add an interpreter on Docker from your settings!"

However, this page, the one from October 2017, remains the same ...

Then YouTrack. This is also 2017 ...

This answer also says, "I opened the folder with Open and then set the interpreter!"

How it worked

Create a folder, file, etc. written on the JetBrains support page or YouTrack, and then open it with PyCharm.

Referenced URL

I referred to the JetBrains official blog. (Searching for "Flask PyCharm Docker" was a hit)

Docker-Compose: Getting Flask up and running

Creating folders, files, etc.

flask-docker
├── Dockerfile
├── docker-compose.yml
├── main.py
└── requirements.txt

Dockerfile


FROM python:3.8-alpine3.12

RUN mkdir /app
WORKDIR /app

COPY main.py main.py
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

EXPOSE 5000

CMD python main.py

docker-compose.yml


version: '2'
services:
  web:
    build: .
    ports:
    - "5000:5000"

requirements.txt


Flask==1.1.1

main.py will be created later.

Open + Docker Compose settings in PyCharm

On the PyCharm startup screen, select [Open] → the created folder.

Preference → [Project: (project name)]-[Python interpreter] → Click the gear icon on the right side of the screen

スクリーンショット 2020-09-25 14.04.10.png

Select [Docker Compose] → Select [Docker](created in the previous step) in [Server], docker-compose.yml in the project in [Configuration file (s)], and select [web] in [Service]. → [OK]

スクリーンショット 2020-09-25 14.04.48.png

Make sure that the library written in requirements.txt such as Flask is included [OK]

スクリーンショット 2020-09-25 14.07.27.png

Source code creation

main.py


from flask import *

app = Flask(__name__)


@app.route('/')
def hello():
    return "Hello!"


if __name__ == '__main__':
    app.run(host='0.0.0.0', port='5000')

Flask related items can also be complemented on PyCharm! !! !!

If you use Docker instead of Docker Compose, the completion on PyCharm will not work. Probably, in the case of Docker, it seems that it is executed by the docker run command, so I think that the Dockerfile is not used.

Run

Right-click on main.py → [Run] or Ctrl + Shift + R (for Mac)

PyCharm console


/usr/local/bin/docker-compose -f /Users/tada/IdeaProjects/flask-docker/docker-compose.yml -f /Users/tada/Library/Caches/JetBrains/PyCharm2020.2/tmp/docker-compose.override.272.yml up --exit-code-from web --abort-on-container-exit web
Recreating flask-docker_web_1 ... 
Attaching to flask-docker_web_1
web_1  |  * Serving Flask app "main" (lazy loading)
web_1  |  * Environment: production
web_1  |    WARNING: This is a development server. Do not use it in a production deployment.
web_1  |    Use a production WSGI server instead.
web_1  |  * Debug mode: off
web_1  |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

Access from curl in the terminal


$ curl localhost:5000
Hello!

You did it! !!

Recommended Posts

When I tried to create a project using Python on Docker with PyCharm, it didn't work, but it worked with Docker Compose.
When I tried to create a virtual environment with Python, it didn't work
I tried to install Docker on Windows 10 Home but it didn't work
I tried to use Java with Termux using Termux Arch but it didn't work
A story that didn't work when I tried to log in with the Python requests module
When I tried to make a VPC with AWS CDK but couldn't make it
A memorandum when I tried to get it automatically with selenium
I tried to create a list of prime numbers with python
I tried to make a todo application using bottle with python
When a Python beginner tried using Bottle, it worked unexpectedly easily.
I want to tweet on Twitter with Python, but I'm addicted to it
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to draw a system configuration diagram with Diagrams on Docker
[Outlook] I tried to automatically create a daily report email with Python
I tried to create a sample to access Salesforce using Python and Bottle
Three things I was addicted to when using Python and MySQL with Docker
I tried to easily create a fully automatic attendance system with Selenium + Python
I tried my best to make an optimization function, but it didn't work.
I tried to use Twitter Scraper on AWS Lambda and it didn't work.
Suddenly I needed to work on a project using Python and Pyramid, so a note of how I'm studying
I tried to create a table only with Django
I tried to implement Minesweeper on terminal with python
I tried to draw a route map with Python
I want to work with a robot in python.
I tried to automatically generate a password with Python3
[Python] I tried to automatically create a daily report of YWT with Outlook mail
I tried scraping food recall information with Python to create a pandas data frame
I tried to make it on / off by setting "Create a plug-in that highlights double-byte space with Sublime Text 2".
I tried to automatically create a report with Markov chain
When I try to push with heroku, it doesn't work
I tried to make a stopwatch using tkinter in python
I made a server with Python socket and ssl and tried to access it from a browser
[5th] I tried to make a certain authenticator-like tool with python
I tried to create a server environment that runs on Windows 10
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
[2nd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "amount" using Python
[Python] A memo that I tried to get started with asyncio
I tried to make a regular expression of "date" using Python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to create Bulls and Cows with a shell program
I want to create a pipfile and reflect it in docker
I tried using "Asciichart Py" which can draw a beautiful graph on the console with Python.
I tried to summarize everyone's remarks on slack with wordcloud (Python)
[4th] I tried to make a certain authenticator-like tool with python
[Python] I tried to make a simple program that works on the command line using argparse.
[1st] I tried to make a certain authenticator-like tool with python
I tried to improve the efficiency of daily work with Python
It is good to create an environment with runtime error => venv when using pyplot backends of macosx on a virtual environment created with virtualenv.
I made a POST script to create an issue on Github and register it in the Project
[Small story] I can't find it even though I apt search to install Python on ubuntu with Docker.
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
Python: I tried to make a flat / flat_map just right with a generator
I want to do it with Python lambda Django, but I will stop
I tried to communicate with a remote server by Socket communication with Python.
When I tried to run Python, it was skipped to the Microsoft Store
Create a tool to automatically furigana with html using Mecab from Python3
I tried to create a plug-in with HULFT IoT Edge Streaming [Development] (2/3)
Steps to create a Python virtual environment with VS Code on Windows
I tried to make a calculator with Tkinter so I will write it