Test with a Custom Port
For this step, you will run a custom Python application inside a Docker container and expose a custom port.
Start by creating a simple Python application in a file named app.py
:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Docker!'
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)
Create a Dockerfile
with the following content to build an image for the Python application:
FROM python:3.8
WORKDIR /app
COPY . /app
RUN pip install flask
EXPOSE 5000
CMD ["python", "app.py"]
Now, build the Docker image run it with the custom port 5000 exposed, and map the exposed port to 8081 in the host
docker build -t my-python-app .
docker run -itd -p 8081:5000 --name python-app my-python-app
Once the container is running, test if the application is accessible by add a web service mapping on the top menu and visit the URL.