-
Notifications
You must be signed in to change notification settings - Fork 303
Docker setup for ToDo app #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
91dffd6
e4867c1
7dc1f49
505a1d2
56344c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| .github | ||
| .venv | ||
| __pycache__ | ||
| .git | ||
| .gitignore | ||
| Dockerfile | ||
| docker-compose.yml | ||
| INSTRUCTION.md | ||
| README.md | ||
| .dockerignore | ||
| .pre-commit-config.yaml | ||
| .travis.yml |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| ### Builder stage | ||
| ARG PYTHON_VERSION=3.8 | ||
|
|
||
| FROM python:${PYTHON_VERSION}-slim AS builder | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| COPY requirements.txt . | ||
|
|
||
| # Install system build dependencies and Python build | ||
| RUN apt-get update \ | ||
| && apt-get install -y --no-install-recommends \ | ||
| build-essential \ | ||
| gcc \ | ||
| python3-dev \ | ||
| libbz2-dev \ | ||
| libssl-dev \ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The image tag mentioned is
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| libffi-dev \ | ||
| && rm -rf /var/lib/apt/lists/* \ | ||
| && pip install --upgrade pip wheel \ | ||
| && pip install --no-cache-dir \ | ||
| --prefix=/install \ | ||
| -r requirements.txt | ||
|
|
||
| ### Runtime stage | ||
| FROM python:${PYTHON_VERSION}-slim | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The image is built with tag
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| ENV PYTHONUNBUFFERED=1 | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Create non-root user | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Image tag should be
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| RUN useradd --create-home --uid 1001 appuser | ||
|
|
||
| # Copy installed packages | ||
| COPY --from=builder /install /usr/local | ||
|
|
||
| # Copy application source | ||
| COPY . . | ||
|
|
||
| # Set ownership | ||
| RUN chown -R appuser:appuser /app | ||
|
|
||
| # Switch to non-root user | ||
| USER appuser | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Image tag should be
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
|
||
| RUN python manage.py migrate | ||
|
|
||
| # Application port | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Image tag should be
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| EXPOSE 8080 | ||
|
|
||
| # Start app | ||
| CMD ["python", "manage.py", "runserver", "0.0.0.0:8080"] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| Docker Hub image | ||
| --------------- | ||
|
|
||
| This repository has a prebuilt Docker image available on Docker Hub: | ||
|
|
||
| https://hub.docker.com/r/verteiger/todoapp | ||
|
|
||
| Quick start — pull and run | ||
| -------------------------- | ||
|
|
||
| 1. Pull the image from Docker Hub: | ||
|
|
||
| docker pull verteiger/todoapp:1.0.0 | ||
|
|
||
| 2. Run the container, mapping the container port 8080 to a host port (example uses host port 8000): | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The pull command uses
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| docker run --rm -p 8000:8080 todoapp:1.0.0 | ||
|
|
||
| Build the image locally | ||
| ----------------------- | ||
|
|
||
| If you prefer to build the image from source locally: | ||
|
|
||
| 1. Build the image (run from the repository root): | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The task requirement #9 specifies the image should be pushed with the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| docker build -t todoapp:1.0.0 . | ||
|
|
||
| 2. Run the locally-built image: | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The run command uses
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| docker run --rm -p 8000:8080 todoapp:1.0.0 | ||
| ------------------ | ||
|
|
||
| To publish your local build to Docker Hub (you must own the `verteiger/todoapp` repo or change the tag to your own): | ||
|
|
||
| 1. Log in to Docker Hub: | ||
|
|
||
| docker login | ||
|
|
||
| 2. Tag the image (if needed): | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tag command uses
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| docker tag todoapp:1.0.0 verteiger/todoapp:1.0.0 | ||
|
|
||
| 3. Push: | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The push command uses
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| docker push verteiger/todoapp:1.0.0 | ||
|
|
||
| Notes about the container | ||
| ------------------------- | ||
|
|
||
| - The container runs the Django development server on port `8080` inside the container (see `Dockerfile`). | ||
| - We recommend mapping it to a non-conflicting host port, e.g. `8000` as shown above. | ||
| - The container runs migrations at start (`python manage.py migrate`) so the first run may take longer. | ||
|
|
||
| Access the application in a browser | ||
| ---------------------------------- | ||
|
|
||
| After running the container with `-p 8000:8080`, open your browser and go to: | ||
|
|
||
| http://localhost:8000/ | ||
|
|
||
| If you mapped to a different host port, replace `8000` with that port. | ||
|
|
||
| Optional: Docker Compose | ||
| ------------------------ | ||
|
|
||
| Create a `docker-compose.yml` file with a service like: | ||
|
|
||
| ```yaml | ||
| version: '3.8' | ||
| services: | ||
| todoapp: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docker-compose service uses
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| image: todoapp:1.0.0 | ||
|
|
||
| Then run: | ||
|
|
||
| ```bash | ||
| docker-compose up -d | ||
| ``` | ||
|
|
||
|
|
||
| Troubleshooting | ||
| --------------- | ||
|
|
||
| - If the site is unreachable, check Docker logs: | ||
|
|
||
| docker logs <container-id> | ||
|
|
||
| - Ensure no other service is already listening on the chosen host port. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Docker Hub link shows
verteiger/todoappwhich appears to be a personal repository, but the image should be namedtodoappwith tag1.0.0as per requirements. Verify this matches your actual Docker Hub repository or update accordingly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed