Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Use a persistent container for `ov` (#4491)

sarayourfriendb4dd8a5280bc
diff --git a/docker/dev_env/Dockerfile b/docker/dev_env/Dockerfile
index ea6e18ab7..4336c266c 100644
--- a/docker/dev_env/Dockerfile
+++ b/docker/dev_env/Dockerfile
@@ -60,8 +60,6 @@ RUN bash -c 'chmod -Rv 0777 /opt'
# Avoid overwriting `.venv`'s from the host
ENV PDM_VENV_IN_PROJECT="False"
-COPY entrypoint.sh /entrypoint.sh
-
-ENTRYPOINT ["/entrypoint.sh"]
-
-CMD ["/bin/sh"]
+# The fedora container's default entrypoint + command behaviour is to keep running
+# so we do not need to modify entrypoint and command to use the container as a
+# persistent environment.
diff --git a/docker/dev_env/expand_aliases.py b/docker/dev_env/exec.py
similarity index 71%
rename from docker/dev_env/expand_aliases.py
rename to docker/dev_env/exec.py
index 7d244f16e..a9d24f012 100644
--- a/docker/dev_env/expand_aliases.py
+++ b/docker/dev_env/exec.py
@@ -21,8 +21,12 @@ def expand_aliases(args: list[str]):
while args[0] in aliases:
args = aliases.pop(args[0]) + args[1:]
- print(*args)
+ return args
if __name__ == "__main__":
- expand_aliases(sys.argv[1:])
+ args = expand_aliases(sys.argv[1:])
+ try:
+ os.execvp(args[0], args)
+ except FileNotFoundError:
+ sys.stdout.buffer.write(args[0].encode() + b": command not found\n")
diff --git a/docker/dev_env/run.sh b/docker/dev_env/run.sh
index 666d7bfe1..0aefa4e76 100755
--- a/docker/dev_env/run.sh
+++ b/docker/dev_env/run.sh
@@ -2,22 +2,27 @@
set -e
-volume_name="openverse-dev-env"
+container_name="openverse-dev-env"
+volume_name="$container_name"
if ! docker volume inspect openverse-dev-env &>/dev/null; then
docker volume create "$volume_name" 1>/dev/null
fi
-run_args=(
+shared_args=(
-i
- --rm
--env "OPENVERSE_PROJECT=$OPENVERSE_PROJECT"
--env "TERM=xterm-256color"
+ --workdir "$OPENVERSE_PROJECT"
+)
+
+run_args=(
+ -d
+ --name "$container_name"
--network host
# Bind the repo to the same exact location inside the container so that pre-commit
# and others don't get confused about where files are supposed to be
-v "$OPENVERSE_PROJECT:$OPENVERSE_PROJECT:rw,z"
- --workdir "$OPENVERSE_PROJECT"
# Save the /opt directory of the container so we can reuse it each time
--mount "type=volume,src=$volume_name,target=/opt"
# Expose the host's docker socket to the container so the container can run docker/compose etc
@@ -33,20 +38,38 @@ run_args=(
# In other words, only tell Docker to attach a TTY to the container when
# there's one to attach in the first place.
if [ -t 0 ]; then
- run_args+=(-t)
+ shared_args+=(-t)
+fi
+
+_cmd=("$@")
+
+if [ "$1" == "sudo" ]; then
+ # Remove sudo
+ # Linux hosts will run the command as root instead in the user check below.
+ # macOS hosts don't need to bother, they're always running as root in the container anyway
+ _cmd=("${@:2}")
fi
case "$OSTYPE" in
linux*)
- run_args+=(--user "$UID:$(getent group docker | cut -d: -f3)")
+ docker_group=$(getent group docker | cut -d: -f3)
+ if [ "$1" == "sudo" ]; then
+ user_id="0"
+ else
+ user_id="$UID"
+ fi
+ shared_args+=(--user "$user_id:$docker_group")
;;
+
darwin*)
# noop, just catching them to avoid the fall-through error case
;;
+
*)
printf "Openverse development is only supported on Linux and macOS hosts. Please use WSL to run the Openverse development environment under Linux on Windows computers." >/dev/stderr
exit 1
;;
+
esac
if command -v pnpm &>/dev/null; then
@@ -55,10 +78,8 @@ if command -v pnpm &>/dev/null; then
# Share the pnpm cache with the container, if it's available locally
if [ "$host_pnpm_store" != "" ]; then
pnpm_home="$(dirname "$host_pnpm_store")"
- run_args+=(
- --env PNPM_HOME="$pnpm_home"
- -v "$pnpm_home:$pnpm_home:rw,z"
- )
+ shared_args+=(--env PNPM_HOME="$pnpm_home")
+ run_args+=(-v "$pnpm_home:$pnpm_home:rw,z")
fi
fi
@@ -68,11 +89,19 @@ if command -v pdm &>/dev/null; then
# if they're enabled
if [ "$(pdm config --quiet install.cache)" == "True" ]; then
host_pdm_cache="$(pdm config --quiet cache_dir)"
- run_args+=(
- --env "PDM_CACHE_DIR=$host_pdm_cache"
- -v "$host_pdm_cache:$host_pdm_cache:rw,z"
- )
+ shared_args+=(--env "PDM_CACHE_DIR=$host_pdm_cache")
+ run_args+=(-v "$host_pdm_cache:$host_pdm_cache:rw,z")
fi
fi
-docker run "${run_args[@]}" openverse-dev-env:latest "$@"
+existing_container_id=$(docker ps -a --filter name="$container_name" -q)
+
+if [ -z "$existing_container_id" ]; then
+ docker run "${shared_args[@]}" "${run_args[@]}" openverse-dev-env:latest
+else
+ # Do not need to bother checking if the container is already running, docker start
+ # is a noop in that case with no adverse effects
+ docker start "$existing_container_id" 1>/dev/null
+fi
+
+docker exec "${shared_args[@]}" "$container_name" python3 docker/dev_env/exec.py "${_cmd[@]}"
diff --git a/docker/dev_env/entrypoint.sh b/docker/dev_env/setup_env.sh
similarity index 80%
rename from docker/dev_env/entrypoint.sh
rename to docker/dev_env/setup_env.sh
index d8156b570..73ebcbb4b 100755
--- a/docker/dev_env/entrypoint.sh
+++ b/docker/dev_env/setup_env.sh
@@ -9,10 +9,10 @@ fi
cd "$OPENVERSE_PROJECT" || exit 1
-corepack install 1>/dev/null
+corepack install
if [ -z "$(n ls 2>/dev/null)" ]; then
- printf "Installing the specific Node JS version required by Openverse frontend; this is only necessary the first time the toolkit runs\n"
+ printf "Installing the specific Node JS version required by Openverse frontend\n"
n install auto
fi
@@ -26,7 +26,7 @@ pdm config venv.location "/opt/pdm/venvs"
_python3s=(/opt/pdm/python/[email protected].*/bin/python3)
if [ ! -x "${_python3s[0]}" ]; then
- printf "Installing the specific Python version required for pipenv environments; this is only necessary the first time the toolkit runs\n"
+ printf "Installing the specific Python version required for pipenv environments\n"
pdm python install 3.11
_python3s=(/opt/pdm/python/[email protected].*/bin/python3)
fi
@@ -49,6 +49,3 @@ fi
if [ -n "$PDM_CACHE_DIR" ]; then
pdm config install.cache on
fi
-
-expanded_args=$(python3 "$OPENVERSE_PROJECT"/docker/dev_env/expand_aliases.py "$@")
-bash -c "$expanded_args"
diff --git a/ov b/ov
index 368ab831b..376432d94 100755
--- a/ov
+++ b/ov
@@ -17,29 +17,43 @@ if [[ $_cmd == "help" || -z $_cmd ]]; then
Openverse development toolkit
USAGE
- ov <subcommand> [args]
+ ov <subcommand> [...ARGS]
COMMANDS
ov init
Initialise the Openverse development toolkit for the first time
Alias for:
- ov build && ov just install-hooks
+ ov build && ov setup-env && ov just install-hooks && ov just init-ov-aliases
- ov build
- Build the Openverse development toolkit Docker image
+ ov build [...ARGS]
+ Build the Openverse development toolkit Docker image.
+ ARGS are passed to `docker build`. See `docker build --help` for options.
+
+ ov setup-env
+ Setup the environment inside the container. `ov init` automatically runs this,
+ and in most cases you should not need to run this manually. However, you may
+ occasionally need to run it manually if tool requirements inside the container
+ change. This can also be a helpful debugging step if tools in the container are
+ not working as expected.
+
+ ov stop
+ Stop the persistent `ov` Docker container.
ov clean
- Remove the Openverse development toolkit Docker image and volume
+ Remove the Openverse development toolkit Docker container, image and volume.
Use in conjunction with `ov init` to recreate the environment from
scratch:
ov clean && ov init
+ Warning: running `ov clean` will result in needing to fully rebuild the environment
+ including downloading tools handled by `ov setup-env` and pre-commit environments.
+
ov hook HOOK
Run Git hooks through pre-commit inside the development toolkit container
- ov COMMAND
+ ov COMMAND [...ARGS]
Run COMMAND inside the development toolkit container
Hints: The toolkit comes loaded with many tools for working with Openverse! Try
some of the following:
@@ -57,14 +71,27 @@ fi
case "$_cmd" in
init)
"$_self" build
- "$_self" 'just install-hooks && just init-ov-aliases'
+ "$_self" setup-env
+ "$_self" just install-hooks
+ "$_self" just init-ov-aliases
;;
build)
- docker build -t openverse-dev-env:latest "$dev_env"
+ docker build "${@:2}" -t openverse-dev-env:latest "$dev_env"
+ ;;
+
+setup-env)
+ "$_self" bash "$dev_env"/setup_env.sh
+ ;;
+
+stop)
+ docker stop openverse-dev-env
;;
clean)
+ docker kill openverse-dev-env
+ docker rm openverse-dev-env
+ docker rmi openverse-dev-env
docker volume rm openverse-dev-env
;;