File: docker.py

package info (click to toggle)
dtrx 8.5.3-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,756 kB
  • sloc: python: 1,841; javascript: 51; makefile: 37; sh: 21
file content (67 lines) | stat: -rw-r--r-- 1,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Invoke docker tasks
"""

import os
import shutil
import sys
from datetime import date

import invoke

from . import utils

DOCKER_IMAGE_NAME = "ghcr.io/dtrx-py/dtrx"


def in_docker():
    """check if we are in a docker container"""
    return os.path.exists("/.dockerenv")


@invoke.task
def build(ctx):
    """build docker image"""
    if in_docker():
        # already in a docker container, probably
        return

    # Make sure we have the docker utility
    if not shutil.which("docker"):
        print("🐋 Please install docker first 🐋")
        sys.exit(1)

    # build the docker image
    with ctx.cd(utils.ROOT_DIR):
        ctx.run(
            f'docker build -t "{DOCKER_IMAGE_NAME}:{date.today().isoformat()}" -t'
            f' "{DOCKER_IMAGE_NAME}:latest" .',
            env={"DOCKER_BUILDKIT": "1"},
            pty=True,
        )


@invoke.task(pre=[build])
def push(ctx):
    """push docker image"""
    ctx.run(f"docker push {DOCKER_IMAGE_NAME}:latest", pty=True)
    ctx.run(f"docker push {DOCKER_IMAGE_NAME}:{date.today().isoformat()}", pty=True)


def run_in_docker(ctx, cmd):
    """run cmd. if in docker already, just run it. if not, run it in docker"""
    if in_docker():
        ctx.run(cmd, pty=True)
    else:
        ctx.run(
            f"docker run --rm -it -v {utils.ROOT_DIR}:/app -w /app"
            f" {DOCKER_IMAGE_NAME} {cmd}",
            pty=True,
        )


collection = invoke.Collection(
    "docker",
    build,
    push,
)