diff --git a/Dockerfile b/Dockerfile index e8f8e06..1688963 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,8 +11,11 @@ RUN pip install --no-cache-dir -r requirements.txt # Copy application code COPY app/ . -# Expose port -EXPOSE 5000 +# Set default port environment variable +ENV PORT=5000 -# Start command -CMD ["python", "app.py"] +# Expose port (optional for documentation) +EXPOSE ${PORT} + +# Start command using shell to allow env substitution +CMD ["sh", "-c", "exec python app.py --port=${PORT}"] diff --git a/app/app.py b/app/app.py index b1e67fe..5e29965 100644 --- a/app/app.py +++ b/app/app.py @@ -64,4 +64,5 @@ def index(): ) if __name__ == "__main__": - app.run(debug=(FLASK_ENV == "development"), host="0.0.0.0", port=5000) + port = int(os.getenv("PORT", 5000)) + app.run(debug=(FLASK_ENV == "development"), host="0.0.0.0", port=port) diff --git a/docker-compose.yml b/docker-compose.yml index 46586f6..d6a00b4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,3 +1,5 @@ +version: '3.8' + services: portfolio: build: @@ -6,7 +8,9 @@ services: image: application-portfolio container_name: portfolio ports: - - "5000:5000" + - "${PORT:-5000}:5000" volumes: - ./app:/app + environment: + - PORT=${PORT:-5000} restart: unless-stopped diff --git a/env.example b/env.example new file mode 100644 index 0000000..3c34789 --- /dev/null +++ b/env.example @@ -0,0 +1,2 @@ +PORT=5000 +FLASK_ENV=production \ No newline at end of file diff --git a/main.py b/main.py index a95e8e6..c839547 100755 --- a/main.py +++ b/main.py @@ -22,6 +22,7 @@ import argparse import subprocess import sys import os +PORT = int(os.getenv("PORT", 5000)) def run_command(command, dry_run=False): """Utility function to run a shell command.""" @@ -94,7 +95,7 @@ def run_dev(args): volume_mapping = f"{current_dir}/app/:/app" command = [ "docker", "run", "-d", - "-p", "5000:5000", + "-p", f"{PORT}:{PORT}", "--name", "portfolio", "-v", volume_mapping, "-e", "FLASK_APP=app.py", @@ -115,7 +116,7 @@ def run_prod(args): """ command = [ "docker", "run", "-d", - "-p", "5000:5000", + "-p", "{PORT}:{PORT}", "--name", "portfolio", "application-portfolio" ]