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.
--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
Start PyCharm and select [Configure]-[Preference]
Select [Python Interpreter] and click the gear icon on the right edge of the screen → [Add ...]
Select [Docker] → click [New] under [Server]
Select [Docker for Mac] → Confirm that [Connection Successful] is displayed at the bottom of the screen, and then click [OK].
Enter python: 3.8
in [Image name] and python
in [Python interpreter path] → [OK]
Confirm that [Python interpreter] is as follows → [OK]
Select [New Project]
Select [Existing interpreter] → Select Python in Docker created by [Interpreter]
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.
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.
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!"
Create a folder, file, etc. written on the JetBrains support page or YouTrack, and then open it with PyCharm.
I referred to the JetBrains official blog. (Searching for "Flask PyCharm Docker" was a hit)
Docker-Compose: Getting Flask up and running
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.
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
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]
Make sure that the library written in requirements.txt such as Flask is included [OK]
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.
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