### These below sample files will build an image and install the required package for python Flask and host a web server as a container.
-> Docker file and required resurces (Create file named as Dockerfile and add below)
1. Dockerfile (file1)
-------------------
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 8001:8001
# Define environment variable
#ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
-> Create a requirements.txt on same directory and add below (usually the required package)
2. requirements.txt (file2)
--------------------
Flask
-> Cretae a Flask app with name app.py on same directory and add the below
3. app.py (file4)
-----------
import os
from flask import Flask
app = Flask(__name__)
@app.route("/")
def main():
return "Welcome!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=int('8001'), debug=True,threaded=True, use_reloader=True)
-------------------------------------xxxxx---------------------------
-> Build the image
Command# docker build -t <some_image_name> .
* The above command will build an image for docker locally ('docker images' will list out that)
-> Run a container with the above created image with port forward..
Command# docker run -d -ti -p 8001:8001 < some_image_name >
Now you can see the web server has been hosted with port number 8001. Use a web browser IP:8001 (the host's IP where the container placed )
-> Docker file and required resurces (Create file named as Dockerfile and add below)
1. Dockerfile (file1)
-------------------
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 8001:8001
# Define environment variable
#ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
-> Create a requirements.txt on same directory and add below (usually the required package)
2. requirements.txt (file2)
--------------------
Flask
-> Cretae a Flask app with name app.py on same directory and add the below
3. app.py (file4)
-----------
import os
from flask import Flask
app = Flask(__name__)
@app.route("/")
def main():
return "Welcome!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=int('8001'), debug=True,threaded=True, use_reloader=True)
-------------------------------------xxxxx---------------------------
-> Build the image
Command# docker build -t <some_image_name> .
* The above command will build an image for docker locally ('docker images' will list out that)
-> Run a container with the above created image with port forward..
Command# docker run -d -ti -p 8001:8001 < some_image_name >
Now you can see the web server has been hosted with port number 8001. Use a web browser IP:8001 (the host's IP where the container placed )
No comments:
Post a Comment