In this lab we'll use Visual Studio Code remote development features to create a new hello world Flask application in a dockerized development environment.
- Install Docker Desktop
- Install Visual Studio Code Insiders
- Install the VS Code Remote Extensions
First we'll create a new dev container that we can start building our app in:
- Open this folder using Visual Studio Code:
cd 2a-vscode-flask-dev-container code-insiders . - Press
F1and selectRemote-Containers: Create container configuration file... - Select
Python 3from the list - Select the
Reopen in Containerbutton in the notification that appears. If you miss the notification, pressF1and select theRemote-Containers: Re-open Folder in Containercommand
After the dev container builds and installs, you will now be working in a dev container and you can start building your app!
Now let's create a hello world flask app. We'll need to set up the container to install flask and expose port 8000.
-
Take a look at the files in the workspace root:
requirements.txtdefines the Python libraries to install in the dev containerapp.pycontains the minimal code to run a flask web server
-
Open
.devcontainer/.devcontainer.jsonand expose port 5000 by adding"appPort": 5000. The .json file should look as follows:{ "name": "Python 3", "context": "..", "dockerFile": "Dockerfile", "workspaceFolder": "/workspace", "extensions": [ "ms-python.python" ], "appPort": 5000 }NOTE: Don't forget to press Ctrl-S to save!
-
Now let's rebuild the container so that it installs flask via the requirements.txt file and exposes port 5000. Press
F1and selectRemote-Containers: Rebuild container. -
Add a debug configuration,
Debug > Add Configuration. SelectFlask. -
Edit the
launch.jsonfile generated to have the app bind to host0.0.0.0by adding host to args as follows:"args": [ "run", "--host","0.0.0.0", "--no-debugger", "--no-reload" ], -
Press
F5to start debugging, browse to your app at http://localhost:5000. -
Optionally set a breakpoint on line 6 of the app and refresh the page