kumquat-buildroot/utils/docker-run

14 lines
439 B
Plaintext
Raw Normal View History

#!/usr/bin/env bash
set -o errexit -o pipefail
DIR=$(dirname "${0}")
MAIN_DIR=$(readlink -f "${DIR}/..")
# shellcheck disable=SC2016
IMAGE=$(grep ^image: "${MAIN_DIR}/.gitlab-ci.yml" | \
sed -e 's,^image: ,,g' | sed -e 's,\$CI_REGISTRY,registry.gitlab.com,g')
utils/docker-run: improve user experience Currently, the docker-run script starts a container based on the reference image, in which the user has UID:GID set to 1000:1000, which may or may not be the same as local user uses, which may prevent the commands in the container from creating files, which foils the plan of using the container to run a build... Additionally, the paths in the container differ from the paths the user expects, as the current Buildroot tree is mounted over the in-container user home directory. This is a bit confusing... Finally, the container is left hanging around after the command finishes, and thus there are as many lingering containers as the user runs docker-run. This is not very nice... We fix all of that (yeah, should be different patches, but meh): - we use --mount instead of -v, which allows us to bind-mount the Buildroot tree at the same place in the container, as Docker will create the destination if it does not exist, while -v seems to expect it to exist [0]. - as a consequence, we can also set the working directory as the Buildroot top-directory; - use --user to force the same UID:GID in the container as the local user, so that files created in the container belong to the local user, and so that files from the local user are accessible from the container; - use --rm to remove the container once it terminates; starting a new container is very quick anyway, so it is as good as re-using a previous container. [0] the documentation is not clear about that. It clearly states that the host directory (i.e. the origin, the source) is created if missing, but it says nothing of the destination: https://docs.docker.com/engine/reference/commandline/run/#mount-volume--v---read-only Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr> Cc: Luca Ceresoli <luca@lucaceresoli.net> Cc: Giulio Benetti <giulio.benetti@benettiengineering.com> Cc: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be> Reviewed-by: Giulio Benetti <giulio.benetti@benettiengineering.com> Tested-by: Giulio Benetti <giulio.benetti@benettiengineering.com> Reviewed-by: Luca Ceresoli <luca@lucaceresoli.net> Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2022-02-14 21:39:09 +01:00
exec docker run -it --rm \
--user "$(id -u):$(id -g)" \
utils/docker-run: improve user experience Currently, the docker-run script starts a container based on the reference image, in which the user has UID:GID set to 1000:1000, which may or may not be the same as local user uses, which may prevent the commands in the container from creating files, which foils the plan of using the container to run a build... Additionally, the paths in the container differ from the paths the user expects, as the current Buildroot tree is mounted over the in-container user home directory. This is a bit confusing... Finally, the container is left hanging around after the command finishes, and thus there are as many lingering containers as the user runs docker-run. This is not very nice... We fix all of that (yeah, should be different patches, but meh): - we use --mount instead of -v, which allows us to bind-mount the Buildroot tree at the same place in the container, as Docker will create the destination if it does not exist, while -v seems to expect it to exist [0]. - as a consequence, we can also set the working directory as the Buildroot top-directory; - use --user to force the same UID:GID in the container as the local user, so that files created in the container belong to the local user, and so that files from the local user are accessible from the container; - use --rm to remove the container once it terminates; starting a new container is very quick anyway, so it is as good as re-using a previous container. [0] the documentation is not clear about that. It clearly states that the host directory (i.e. the origin, the source) is created if missing, but it says nothing of the destination: https://docs.docker.com/engine/reference/commandline/run/#mount-volume--v---read-only Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr> Cc: Luca Ceresoli <luca@lucaceresoli.net> Cc: Giulio Benetti <giulio.benetti@benettiengineering.com> Cc: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be> Reviewed-by: Giulio Benetti <giulio.benetti@benettiengineering.com> Tested-by: Giulio Benetti <giulio.benetti@benettiengineering.com> Reviewed-by: Luca Ceresoli <luca@lucaceresoli.net> Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2022-02-14 21:39:09 +01:00
--mount "type=bind,src=${MAIN_DIR},dst=${MAIN_DIR}" \
--workdir "${MAIN_DIR}" \
"${IMAGE}" "${@}"