If you use the flask executable to start your server, you can use flask run -host=0.0.0.0 to change the default from 127.0.0.1 and open it up to non local connections. The config and app.run methods that the other answers describe are probably better practice but this can be handy as well. If you use the flask executable to start your server, you can use flask run -host=0.0.0.0 to change the default from 127.0.0.1 and open it up to non local connections. The config and app.run methods that the other answers describe are probably better practice but this can be handy as well. Well, Flask is a Python framework for backend systems, so you pretty much deploy it like you deploy any Python code. Since Flask supports a RESTful API (Representational state transfer), you can pretty much choose to front-end it with a web p. All we have to do now is configure our run.py file so we can start the Flask server. # run.py from app import app if name 'main': app. Run To use the command flask run like we did before, we would need to set the FLASKAPP environment variable to run.py, like so: $ export FLASKAPP = run.py $ flask run.
Adding Docker to your Python andFlaskdevelopment environmentcan be confusing when you are just getting started with containers. Let'squickly get Docker installed and configured for developing Flask webapplications on your local system.
Our Tools
This tutorial is written for Python 3. It will work withPython 2 but I have not tested it with thesoon-to-be deprecated 2.7 version.
Docker for Mac is necessary.I recommend the stable release unless you have an explicit purpose for the edgechannel.
Within the Docker container we will use:
- Python 3, specifically theslim-3.6.5 versionfrom Docker Hub
- Flask version 1.0.2
All of the code for the Dockerfile and the Flask app are available open sourceunder the MIT license on GitHub under thedocker-flask-mac directoryof theblog-code-examplesrepository. Use the code for your own purposes as much as you like.
Installing Docker on macOS
We need to install Docker before we can spin up our Docker containers. If youalready have Docker for Mac installed and working, feel free to jump to thenext section.
On your Mac,download the Docker Community Edition (CE) for Macinstaller.
Find the newly-downloaded install within Finder and double click on the file.Follow the installation process, which includes granting administrative privilegesto the installer. https://xyfqhv.weebly.com/blue-stacks-mac-app.html.
Open Terminal when the installer is done. Test your Docker installation with the
--version
flag:If Docker is installed correctly you should see the following output:
Note that Docker runs through a system agent you can find in the menu bar.
I have found the Docker agent to take up some precious battery lifeon my Macbook Pro. If I am not developing and need to max battery time I willclose down the agent and start it back up again when I am ready to code.
Now that Docker is installed let's get to running a container and writingour Flask application.
Dockerfile
Docker needs to know what we want in a container, which is where the
Dockerfile
comes in. Save the Dockerfile so that we can run our next command with the completedcontents of the file. On the commandline run:
The above
docker build
file uses the -t
flag to tag the image withthe name of flaskdock
.If the build worked successfully we can see the image in with the
docker image ls
command. Give that a try now:We should then see our tag name in the images list:
Our image is ready to load up as a container so we can write a quickFlask app that we will use to test our environment by running it withinthe container.
Coding A Simple Flask app
Time to put together a super simple 'Hello, World!' Flask web app to testrunning Python code within our Docker container. Within the currentproject directory, create a file named
app.py
with the following contents:The above 7 lines of code (not counting blank PEP8-compliant lines) inapp.pyallow our application to return a simple message when run with theFlask development server.
We need just one more file to specify our
Flask
dependency. Createa requirements.txt
file within the same directory as app.py
:Make sure both the
app.py
and requirements.txt
file are saved thenwe can give the code a try.Running the Container
Now that we have our image in hand along with the Python code in a filewe can run the image as a container with the
docker run
command. Executethe following command, making sure to replace the absolute path for thevolume to your own directory.If you receive the error
python: can't open file 'app.py': [Errno 2] No such file or directory
thenyou likely forgot to chance /Users/matt/devel/py/flaskdocker
to thedirectory where your project files, especially app.py
, are located.Everything worked when you see a simple text-based HTTP response like whatis shown above in the screenshot of my Chrome browser.
What's Next?
We just installed Docker and configured a Flask application to run inside acontainer. That is just the beginning of how you can integrate Docker intoyour workflow. I strongly recommend reading theDjango with PostgreSQL quickstartthat will introduce you to Docker Swarm as well as the core Docker containerservice.
Next up take a look at the Docker anddeployment pages for more related tutorials.
Questions? Let me know via a GitHubissue ticket on the Full Stack Python repository,on Twitter@fullstackpythonor @mattmakai.
Do you see a typo, syntax issue or just something that's confusing in thisblog post? Forkthis page's source on GitHuband submit a pull request with a fix orfile an issue ticket on GitHub.
Adding Docker to your Python andFlaskdevelopment environmentcan be confusing when you are just getting started with containers. Let'squickly get Docker installed and configured for developing Flask webapplications on your local system.
Our Tools
Flask App Mac
This tutorial is written for Python 3. It will work withPython 2 but I have not tested it with thesoon-to-be deprecated 2.7 version.
Docker for Mac is necessary.I recommend the stable release unless you have an explicit purpose for the edgechannel.
Within the Docker container we will use:
- Python 3, specifically theslim-3.6.5 versionfrom Docker Hub
- Flask version 1.0.2
All of the code for the Dockerfile and the Flask app are available open sourceunder the MIT license on GitHub under thedocker-flask-mac directoryof theblog-code-examplesrepository. Use the code for your own purposes as much as you like.
Installing Docker on macOS
We need to install Docker before we can spin up our Docker containers. If youalready have Docker for Mac installed and working, feel free to jump to thenext section.
Zoom meeting app for mac free download. On your Mac,download the Docker Community Edition (CE) for Macinstaller.
Find the newly-downloaded install within Finder and double click on the file.Follow the installation process, which includes granting administrative privilegesto the installer. https://xyfqhv.weebly.com/best-free-photo-editing-apps-on-mac.html.
Open Terminal when the installer is done. Test your Docker installation with the
--version
flag:If Docker is installed correctly you should see the following output:
Note that Docker runs through a system agent you can find in the menu bar.
I have found the Docker agent to take up some precious battery lifeon my Macbook Pro. If I am not developing and need to max battery time I willclose down the agent and start it back up again when I am ready to code.
Flash Mac Pro 4 1 To 5 1
Now that Docker is installed let's get to running a container and writingour Flask application.
Dockerfile
Docker needs to know what we want in a container, which is where the
Dockerfile
comes in. Save the Dockerfile so that we can run our next command with the completedcontents of the file. On the commandline run:
The above
docker build
file uses the -t
flag to tag the image withthe name of flaskdock
.If the build worked successfully we can see the image in with the
docker image ls
command. Give that a try now:We should then see our tag name in the images list:
Our image is ready to load up as a container so we can write a quickFlask app that we will use to test our environment by running it withinthe container.
Coding A Simple Flask app
Time to put together a super simple 'Hello, World!' Flask web app to testrunning Python code within our Docker container. Within the currentproject directory, create a file named
app.py
with the following contents:The above 7 lines of code (not counting blank PEP8-compliant lines) inapp.pyallow our application to return a simple message when run with theFlask development server.
We need just one more file to specify our
Flask
dependency. Createa requirements.txt
file within the same directory as app.py
:Make sure both the
app.py
and requirements.txt
file are saved thenwe can give the code a try.Running the Container
Now that we have our image in hand along with the Python code in a filewe can run the image as a container with the
docker run
command. Executethe following command, making sure to replace the absolute path for thevolume to your own directory.If you receive the error
python: can't open file 'app.py': [Errno 2] No such file or directory
thenyou likely forgot to chance /Users/matt/devel/py/flaskdocker
to thedirectory where your project files, especially app.py
, are located.Everything worked when you see a simple text-based HTTP response like whatis shown above in the screenshot of my Chrome browser.
Flask To Mac App Download
What's Next?
We just installed Docker and configured a Flask application to run inside acontainer. That is just the beginning of how you can integrate Docker intoyour workflow. I strongly recommend reading theDjango with PostgreSQL quickstartthat will introduce you to Docker Swarm as well as the core Docker containerservice.
Next up take a look at the Docker anddeployment pages for more related tutorials.
Questions? Let me know via a GitHubissue ticket on the Full Stack Python repository,on Twitter@fullstackpythonor @mattmakai.
Do you see a typo, syntax issue or just something that's confusing in thisblog post? Forkthis page's source on GitHuband submit a pull request with a fix orfile an issue ticket on GitHub.