Skip to content

Instantly share code, notes, and snippets.

@fdmysterious
Last active November 27, 2024 02:07
Show Gist options
  • Save fdmysterious/09a9419d77ea867e9e7921c9a2df5d53 to your computer and use it in GitHub Desktop.
Save fdmysterious/09a9419d77ea867e9e7921c9a2df5d53 to your computer and use it in GitHub Desktop.
Justfile for docker

Justfile for docker

This gist shows how to use the just command runner for a docker based project. The trick is to give the ability to call the recipes inside or outside the container, making it compatible for example with pipelines, or Devcontainers.

image_name := env_var_or_default("IMAGE_NAME", "buildenv:componentTesting")

# https://stackoverflow.com/questions/23513045/how-to-check-if-a-process-is-running-inside-docker-container
is_in_docker := `test -f /.dockerenv && echo 1 || echo 0`

# run_cmd is the docker run command used to launch your env.
run_cmd := if is_in_docker == "0" {
    'docker run -it --rm --mount type=bind,src="$(pwd)",target="/project" -w /project ' + image_name
} else {""}

#################

echo-run-cmd:
    @echo run_cmd={{run_cmd}}

check-in-docker:
    @echo {{if is_in_docker == "1" {"In docker container!"} else  {"Not in docker container!"} }}

ensure-folders:
    @mkdir -p build-tests
    @mkdir -p logs

build-docker force="no": ensure-folders
    #!/usr/bin/env sh
    if [ {{is_in_docker}} -eq 1 ] ; then
        echo "Running in docker container, skipping image generation !"
    else
        if [ -z `docker image ls {{image_name}} --format '1'`] || [ {{force}} = "force" ] ; then
            [ {{force}} = "force" ] && echo "Forcing build"
            docker build -t {{image_name}} -f docker/Dockerfile_componentTesting docker
        fi
    fi



#################

run-tests: (build-docker "0")
    {{run_cmd}} /bin/ash -c "scripts/run_tests.sh"

shell: (build-docker "0")
    {{run_cmd}} /bin/ash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment