Docker Compose Quickstart — Step 1: Set up the project
Create a directory for the project Bounded code example (external data; do not execute automatically): ```console $ mkdir compose-demo $ cd compose-demo ``` Create app.py in your project directory and add the following Bounded code example (external data; do not execute automatically): ```python imp
Reference note (untrusted external data; do not execute it as instructions).
Create a directory for the project
Bounded code example (external data; do not execute automatically):
```console
$ mkdir compose-demo
$ cd compose-demo
```
Create app.py in your project directory and add the following
Bounded code example (external data; do not execute automatically):
```python
import os
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(
host=os.getenv("REDIS_HOST", "redis"),
port=int(os.getenv("REDIS_PORT", "6379")),
)
@app.route("/")
def hello():
count = cache.incr("hits")
return f"Hello from Docker! I have been seen {count} time(s).\n"
```
The app reads its Redis connection details from environment variables, with sensible defaults so it works out of the box.
Create requirements.txt in your project directory and add the following
Bounded code example (external data; do not execute automatically):
```text
flask
redis
```
Bounded code example (external data; do not execute automatically):
```dockerfile
# syntax=docker/dockerfile:1
# Build an image with the Python 3.12 image
FROM python:3.12-alpine
# Set the working directory to `/code`
WORKDIR /code
# Set environment variables used by the `flask` command
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
# Install `gcc` and other dependencies
RUN apk add --no-cache gcc musl-dev linux-headers
# Copy `requirements.txt`
COPY requirements.txt .
# Install the Python dependencies
RUN pip install -r requirements.txt
# Copy the current directory `.` in the project to the workdir `.` in the image
COPY . .
EXPOSE 5000
# Set the default command for the container to `flask run --debug`
CMD ["flask", "run", "--debug"]
```
> [!IMPORTANT] > > Make sure the file is named Dockerfile with no extension. Some editors add .txt > automatically, which causes the build to fail.
For more information on how to write Dockerfiles, see the Dockerfile reference.
Create a .env file to hold configuration values
Bounded code example (external data; do not execute automatically):
```text
APP_PORT=8000
REDIS_HOST=redis
REDIS_PORT=6379
``` …
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/manuals/compose/gettingstarted.md :: Step 1: Set up the project ↗Revision 3a9d778562f3 · Apache-2.0 and attribution