- Ensure you have Python and Docker Desktop (or Rancher Desktop) installed on your machine.
- Create a virtual environment for your Python project and install the required packages like Flask.
-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install Flask:
pip install flask
-
Create
app.py
:from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'We are running our first container - from CoderCo' if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
-
Create
requirements.txt
:flask
-
Run the Flask app:
python3 app.py
-
Access the app:
- Open
http://localhost:5000
in your browser - Or run
curl http://localhost:5000
in your terminal
- Open
-
Create a
Dockerfile
:# this is your base image FROM python:3.9 # we set the working dir in yourr container WORKDIR /usr/src/app # copying reqs file to your container so you can install the app reqs ygm COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt # copy your app code fam COPY . . # expose/open up the port on your container so you can access your app ygm EXPOSE 5000 # running app in container CMD ["python", "app.py"]
-
Build the Docker image:
docker build -t coderco .
-
Run the Docker container:
docker run -p 5001:5001 coderco
-
Access the app:
- Open
http://localhost:5000
in your browser - Or run
curl http://localhost:5000
in your terminal
- Open
- Access our challenge here