{"id":21869655,"url":"https://github.com/alich03/docker-compose-for-flask-api-and-sreamlit","last_synced_at":"2026-04-16T10:01:28.673Z","repository":{"id":265025100,"uuid":"894881805","full_name":"alich03/Docker-Compose-for-Flask-API-and-Sreamlit","owner":"alich03","description":"Docker containers for Flask API and Streamlit Frontend composed by Docker-Compose","archived":false,"fork":false,"pushed_at":"2024-11-27T07:55:22.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-21T22:16:12.856Z","etag":null,"topics":["docker","docker-compose","docker-container","docker-image","dockerfile"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alich03.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-27T07:05:01.000Z","updated_at":"2024-11-27T08:05:40.000Z","dependencies_parsed_at":"2024-11-27T08:42:48.442Z","dependency_job_id":null,"html_url":"https://github.com/alich03/Docker-Compose-for-Flask-API-and-Sreamlit","commit_stats":null,"previous_names":["alich03/docker-for-flask-api-and-sreamlit"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alich03/Docker-Compose-for-Flask-API-and-Sreamlit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alich03%2FDocker-Compose-for-Flask-API-and-Sreamlit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alich03%2FDocker-Compose-for-Flask-API-and-Sreamlit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alich03%2FDocker-Compose-for-Flask-API-and-Sreamlit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alich03%2FDocker-Compose-for-Flask-API-and-Sreamlit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alich03","download_url":"https://codeload.github.com/alich03/Docker-Compose-for-Flask-API-and-Sreamlit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alich03%2FDocker-Compose-for-Flask-API-and-Sreamlit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31880882,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T09:23:21.276Z","status":"ssl_error","status_checked_at":"2026-04-16T09:23:15.028Z","response_time":69,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","docker-compose","docker-container","docker-image","dockerfile"],"created_at":"2024-11-28T06:08:40.229Z","updated_at":"2026-04-16T10:01:28.642Z","avatar_url":"https://github.com/alich03.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"Absolutely! I’m happy to guide you through learning **Docker Compose** and show you how to run multiple services, like a **Flask API** and a **Streamlit app**, in separate containers using Docker Compose.\n\n### What is Docker Compose?\n**Docker Compose** is a tool that helps you define and run multi-container Docker applications. Using a **`docker-compose.yml`** file, you can define all the services (containers) needed for your application, their configurations, and how they communicate with each other.\n\nWith Docker Compose, you can:\n- Define and run multiple containers with a single command.\n- Link containers (e.g., Flask API and Streamlit) so they can communicate.\n- Simplify the management of complex multi-container applications.\n\n---\n\n### Basic Docker Compose File Structure\n\nA **`docker-compose.yml`** file defines the configuration of all containers (services) and their interactions. It uses **YAML** syntax, making it human-readable.\n\nA typical structure looks like this:\n\n```yaml\nversion: '3.8'  # Version of Docker Compose syntax\n\nservices:\n  \u003cservice_name\u003e:  # The name of the service (container)\n    build: .  # Path to the Dockerfile (usually '.' if it's in the current directory)\n    ports:\n      - \"\u003chost_port\u003e:\u003ccontainer_port\u003e\"  # Port mapping between host and container\n    environment:\n      - VAR_NAME=value  # Environment variables (if needed)\n    networks:\n      - mynetwork  # Network for communication between containers\n\nnetworks:\n  mynetwork:  # Custom network to allow containers to communicate\n    driver: bridge  # Default network driver\n```\n\n---\n\n### Step-by-Step: Running Flask API and Streamlit App in Separate Containers Using Docker Compose\n\nNow, let’s walk through how to containerize the **Flask API** and **Streamlit app** you already created, and run them in separate containers using Docker Compose.\n\n### Step 1: Project Directory Setup\n\nFirst, let’s structure the project directory to include both your Flask API and Streamlit app, along with the `docker-compose.yml` file.\n\n```\ndocker-compose-project/\n│\n├── flask-api/\n│   ├── app.py                # Flask API code\n│   ├── requirements.txt      # Flask dependencies\n│   └── Dockerfile            # Dockerfile for Flask API\n│\n├── streamlit-app/\n│   ├── app.py                # Streamlit app code\n│   ├── requirements.txt      # Streamlit dependencies\n│   └── Dockerfile            # Dockerfile for Streamlit app\n│\n└── docker-compose.yml        # Docker Compose configuration file\n```\n\n### Step 2: Create Dockerfiles for Both Services\n\n#### Flask API Dockerfile (`flask-api/Dockerfile`):\n\n```Dockerfile\n# Use Python slim image\nFROM python:3.9-slim\n\n# Set working directory inside the container\nWORKDIR /app\n\n# Copy requirements.txt and install dependencies\nCOPY requirements.txt /app/\nRUN pip install --no-cache-dir -r requirements.txt\n\n# Copy the Flask app code\nCOPY . /app/\n\n# Expose Flask's default port\nEXPOSE 5000\n\n# Command to run the Flask app\nCMD [\"python\", \"app.py\"]\n```\n\n#### Streamlit App Dockerfile (`streamlit-app/Dockerfile`):\n\n```Dockerfile\n# Use Python slim image\nFROM python:3.9-slim\n\n# Set working directory inside the container\nWORKDIR /app\n\n# Copy requirements.txt and install dependencies\nCOPY requirements.txt /app/\nRUN pip install --no-cache-dir -r requirements.txt\n\n# Copy the Streamlit app code\nCOPY . /app/\n\n# Expose Streamlit's default port\nEXPOSE 8501\n\n# Command to run the Streamlit app\nCMD [\"streamlit\", \"run\", \"app.py\", \"--server.enableCORS=false\"]\n```\n\n---\n\n### Step 3: Create the `docker-compose.yml` File\n\nNow, create a `docker-compose.yml` file that will run both the Flask API and Streamlit app in separate containers.\n\n```yaml\nversion: '3.8'\n\nservices:\n  flask-api:\n    build: ./flask-api  # Flask API's Dockerfile path\n    ports:\n      - \"5000:5000\"  # Expose Flask API on port 5000\n    networks:\n      - app-network  # Use a custom network to allow communication between containers\n\n  streamlit-app:\n    build: ./streamlit-app  # Streamlit app's Dockerfile path\n    ports:\n      - \"8501:8501\"  # Expose Streamlit app on port 8501\n    depends_on:\n      - flask-api  # Make sure Flask API starts first\n    networks:\n      - app-network  # Same network to allow communication\n\nnetworks:\n  app-network:\n    driver: bridge  # Default network driver, allows containers to communicate\n```\n\n### Explanation of the `docker-compose.yml`:\n\n- **`services`**: Defines the two services (`flask-api` and `streamlit-app`).\n- **`build`**: Path to the respective Dockerfile for each service.\n- **`ports`**: Maps the container port to the host machine port, so we can access the services locally.\n- **`depends_on`**: Ensures that the Flask API starts before the Streamlit app.\n- **`networks`**: Creates a custom network (`app-network`) to allow communication between containers.\n\n---\n\n### Step 4: Building and Running the Containers\n\nWith the `docker-compose.yml` file in place, you can now build and run the services using Docker Compose.\n\n#### 4.1 Build the services\nRun this command from the root directory (`docker-compose-project`):\n\n```bash\ndocker-compose build\n```\n\nThis will build the Docker images for both the Flask API and the Streamlit app based on the Dockerfiles.\n\n#### 4.2 Run the containers\nAfter the build is successful, you can start the containers with:\n\n```bash\ndocker-compose up\n```\n\nThis will start both the Flask API and the Streamlit app containers, and they will be accessible at the following URLs:\n\n- Flask API: `http://localhost:5000`\n- Streamlit app: `http://localhost:8501`\n\n### Step 5: Communicating Between Containers (Optional)\n\nYou may want to have your **Streamlit app** communicate with the **Flask API**. For example, in `streamlit-app/app.py`, you could use Python's `requests` library to call the Flask API.\n\nExample of `streamlit-app/app.py`:\n\n```python\nimport streamlit as st\nimport requests\n\n# Streamlit app title\nst.title(\"Streamlit App Communicating with Flask API\")\n\n# Call the Flask API\nresponse = requests.get(\"http://flask-api:5000\")  # Use Flask service name as hostname\nif response.status_code == 200:\n    st.write(\"Flask API Response:\", response.json())\nelse:\n    st.write(\"Error connecting to Flask API.\")\n```\n\nNotice that in Docker Compose, the **Flask API service** is reachable by the service name (`flask-api`) as the hostname.\n\n### Step 6: Stopping and Cleaning Up\n\nAfter testing, you can stop the running containers with:\n\n```bash\ndocker-compose down\n```\n\nThis stops and removes the containers, networks, and volumes created by `docker-compose up`.\n\n---\n\n### Final Directory Structure:\n\n```\ndocker-compose-project/\n│\n├── flask-api/\n│   ├── app.py                # Flask API code\n│   ├── requirements.txt      # Flask dependencies\n│   └── Dockerfile            # Dockerfile for Flask API\n│\n├── streamlit-app/\n│   ├── app.py                # Streamlit app code\n│   ├── requirements.txt      # Streamlit dependencies\n│   └── Dockerfile            # Dockerfile for Streamlit app\n│\n└── docker-compose.yml        # Docker Compose configuration file\n```\n\n---\n\n### Conclusion:\nYou’ve learned how to:\n- Set up a **multi-container application** with **Docker Compose**.\n- Containerize both a **Flask API** and a **Streamlit app** in separate containers.\n- Allow containers to communicate through a custom network.\n- Build and run the multi-container app using Docker Compose.\n\nThis is just the start. Docker Compose also allows advanced configurations such as volumes for persistent data storage, scaling services, and more.\n\nLet me know if you have any questions or if you’d like to explore other features!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falich03%2Fdocker-compose-for-flask-api-and-sreamlit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falich03%2Fdocker-compose-for-flask-api-and-sreamlit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falich03%2Fdocker-compose-for-flask-api-and-sreamlit/lists"}