Skip to content

Commit

Permalink
added docker compose to all chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
zenx committed May 5, 2024
1 parent 1448e7b commit 0708cbe
Show file tree
Hide file tree
Showing 48 changed files with 2,423 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Chapter01/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.12.3-slim

RUN apt-get update -qq \
&& rm -rf /var/lib/apt/lists/*

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /code

COPY requirements.txt .
RUN python -m pip install -r requirements.txt

COPY . .
113 changes: 113 additions & 0 deletions Chapter01/do.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/bin/bash

COLUMNS="`tput cols`"
LINES="`tput lines`"
BOLD=$(tput bold)
NORMAL=$(tput sgr0)

COMPOSE_FILES="-f docker-compose.yml"

DOCKER_COMPOSE="docker compose ${COMPOSE_FILES}"

# Game-specific commands:
_requires() {
service="$1"
$DOCKER_COMPOSE ps -q $service &> /dev/null
if [[ "$?" == 1 ]]; then
echo "'$service' service is not running. Please run \`start\` first."
exit 1
fi
}

build() {
$DOCKER_COMPOSE build --force-rm "${@:3}"
}

compose() {
$DOCKER_COMPOSE "$@"
}

start() {
$DOCKER_COMPOSE up "$@"
}

stop() {
$DOCKER_COMPOSE down "$@"
}

shell() {
_requires web_run
exec -w /code/mysite web_run /bin/bash
}

migrate() {
_requires web_run
exec -w /code/mysite web_run ./manage.py migrate "$@"
}

makemigrations() {
_requires web_run
exec -w /code/mysite web_run python manage.py makemigrations "$@"
}

check() {
_requires web_run
exec -w /code/mysite web_run ./manage.py check
}

exec() {
$DOCKER_COMPOSE exec -e COLUMNS -e LINES "$@"
}

_usage() {
cat <<USAGE
Convenience wrapper around docker compose.
Usage:
${BOLD}build${NORMAL} [<arg>]
Builds all the images (or the ones specified).
${BOLD}exec${NORMAL} [<arg>]
Execute a command in a container
${BOLD}compose${NORMAL}
Minimal wrapper around docker-compose, just ensures the correct config files are loaded.
${BOLD}migrate${NORMAL} [<arg>]
Apply any unapplied django migrations
${BOLD}makemigrations${NORMAL} [<arg>]
Create a new Django migration, using the given args
${BOLD}check${NORMAL}
Validate django settings
${BOLD}shell${NORMAL}
Opens a bash terminal in web_run
${BOLD}start${NORMAL} [<arg>]
Start the django server (and dependent services)
You can pass `-d` for running detached.
${BOLD}stop${NORMAL} [<arg>]
Stop the django server (and dependent services)
USAGE
}

if [ "$1" == "" ]; then
_usage
fi

$*
17 changes: 17 additions & 0 deletions Chapter01/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
web:
build: .
volumes:
- .:/code
web_migrate:
extends:
service: web
command: python /code/mysite/manage.py migrate
web_run:
extends:
service: web
command: python /code/mysite/manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"
depends_on:
- web_migrate
16 changes: 16 additions & 0 deletions Chapter02/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.12.3-slim

RUN apt-get update -qq \
&& rm -rf /var/lib/apt/lists/*

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /code

COPY requirements.txt .
RUN python -m pip install -r requirements.txt

COPY . .
113 changes: 113 additions & 0 deletions Chapter02/do.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/bin/bash

COLUMNS="`tput cols`"
LINES="`tput lines`"
BOLD=$(tput bold)
NORMAL=$(tput sgr0)

COMPOSE_FILES="-f docker-compose.yml"

DOCKER_COMPOSE="docker compose ${COMPOSE_FILES}"

# Game-specific commands:
_requires() {
service="$1"
$DOCKER_COMPOSE ps -q $service &> /dev/null
if [[ "$?" == 1 ]]; then
echo "'$service' service is not running. Please run \`start\` first."
exit 1
fi
}

build() {
$DOCKER_COMPOSE build --force-rm "${@:3}"
}

compose() {
$DOCKER_COMPOSE "$@"
}

start() {
$DOCKER_COMPOSE up "$@"
}

stop() {
$DOCKER_COMPOSE down "$@"
}

shell() {
_requires web_run
exec -w /code/mysite web_run /bin/bash
}

migrate() {
_requires web_run
exec -w /code/mysite web_run ./manage.py migrate "$@"
}

makemigrations() {
_requires web_run
exec -w /code/mysite web_run python manage.py makemigrations "$@"
}

check() {
_requires web_run
exec -w /code/mysite web_run ./manage.py check
}

exec() {
$DOCKER_COMPOSE exec -e COLUMNS -e LINES "$@"
}

_usage() {
cat <<USAGE
Convenience wrapper around docker compose.
Usage:
${BOLD}build${NORMAL} [<arg>]
Builds all the images (or the ones specified).
${BOLD}exec${NORMAL} [<arg>]
Execute a command in a container
${BOLD}compose${NORMAL}
Minimal wrapper around docker-compose, just ensures the correct config files are loaded.
${BOLD}migrate${NORMAL} [<arg>]
Apply any unapplied django migrations
${BOLD}makemigrations${NORMAL} [<arg>]
Create a new Django migration, using the given args
${BOLD}check${NORMAL}
Validate django settings
${BOLD}shell${NORMAL}
Opens a bash terminal in web_run
${BOLD}start${NORMAL} [<arg>]
Start the django server (and dependent services)
You can pass `-d` for running detached.
${BOLD}stop${NORMAL} [<arg>]
Stop the django server (and dependent services)
USAGE
}

if [ "$1" == "" ]; then
_usage
fi

$*
19 changes: 19 additions & 0 deletions Chapter02/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:
web:
build: .
env_file:
- .env
volumes:
- .:/code
web_migrate:
extends:
service: web
command: python /code/mysite/manage.py migrate
web_run:
extends:
service: web
command: python /code/mysite/manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"
depends_on:
- web_migrate
17 changes: 17 additions & 0 deletions Chapter03/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM python:3.12.3-slim

RUN apt-get update -qq \
&& rm -rf /var/lib/apt/lists/*

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /code

COPY requirements.txt .
RUN python -m pip install -r requirements.txt
RUN python -m pip install psycopg[binary]

COPY . .
Loading

0 comments on commit 0708cbe

Please sign in to comment.