From 7f2020f9040fe2378c2797bfa256f77d118be372 Mon Sep 17 00:00:00 2001 From: "Yann E. MORIN" Date: Mon, 14 Feb 2022 21:39:09 +0100 Subject: [PATCH] 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 Cc: Luca Ceresoli Cc: Giulio Benetti Cc: Arnout Vandecappelle (Essensium/Mind) Reviewed-by: Giulio Benetti Tested-by: Giulio Benetti Reviewed-by: Luca Ceresoli Signed-off-by: Arnout Vandecappelle (Essensium/Mind) --- utils/docker-run | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/utils/docker-run b/utils/docker-run index 2bca8d8e3a..bdbc979264 100755 --- a/utils/docker-run +++ b/utils/docker-run @@ -7,4 +7,8 @@ IMAGE=$(grep ^image: "${MAIN_DIR}/.gitlab-ci.yml" | \ sed -e 's,^image: ,,g' | sed -e 's,\$CI_REGISTRY,registry.gitlab.com,g') set -x -exec docker run -v "${MAIN_DIR}:/home/br-user" -it "${IMAGE}" "$@" +exec docker run -it --rm \ + --user $(id -u):$(id -g) \ + --mount "type=bind,src=${MAIN_DIR},dst=${MAIN_DIR}" \ + --workdir "${MAIN_DIR}" \ + "${IMAGE}" "${@}"