Skip to content

Commit

Permalink
feat(docker): run specified commands on container creation. This feat…
Browse files Browse the repository at this point in the history
…ure can be used for example to persist installed packages between updates
  • Loading branch information
vanutp committed Mar 9, 2024
1 parent 212069c commit 456f503
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
8 changes: 7 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
**/__pycache__/
venv/
guide/site
.idea/
/.github/
/data/
/guide/site
/guide/.cache
flake.lock
flake.nix
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ RUN --mount=type=cache,target=/root/.cache/pip \

COPY . .
RUN sed -i "s/\(COMMIT_HASH *= *\).*/\1'$(git rev-parse HEAD)'/" tgpy/version.py
RUN rm -rf .git guide poetry.lock pyproject.toml .dockerignore .gitignore README.md

FROM base as runner
COPY --from=builder /venv /venv
ENV PATH="/venv/bin:$PATH"

COPY --from=builder /app/tgpy tgpy
COPY --from=builder /app /app

ENV TGPY_DATA=/data
VOLUME /data

ENTRYPOINT ["/venv/bin/python", "-m", "tgpy"]
ENTRYPOINT ["/app/entrypoint.sh"]
31 changes: 31 additions & 0 deletions docker/install_mods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import logging
from subprocess import Popen

from tgpy.api.config import config

logging.basicConfig(
format='[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.INFO,
)
logger = logging.getLogger('install_mods')


def main():
config.load()
command_groups: dict = config.get('docker.setup_commands', {})
command_groups['_core'] = 'apt-get update'
for name, commands in sorted(command_groups.items()):
if isinstance(commands, str):
commands = [commands]
logger.info(f"Running setup command group '{name}'")
for command in commands:
logger.info(f"Running setup command '{command}'")
p = Popen(command, shell=True)
p.wait()
if p.returncode != 0:
logger.info("Running setup command failed")


if __name__ == '__main__':
main()
8 changes: 8 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -e
if [ ! -f container_setup_completed ]; then
PYTHONPATH=. python /app/docker/install_mods.py
touch container_setup_completed
fi

exec python -m tgpy

0 comments on commit 456f503

Please sign in to comment.