From 119bdae234a6b53e4ea5adc9f115aa4697a72da6 Mon Sep 17 00:00:00 2001 From: Chris Coley <chris@codingallnight.com> Date: Mon, 21 Apr 2025 22:01:24 -0700 Subject: [PATCH] Improve how we copy files into the user's home directory --- Dockerfile | 14 ++++++++++---- README.md | 4 ++-- entrypoint.sh | 4 ++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index c3f29cf..fb6e25c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,14 +8,20 @@ RUN apk add --update --no-cache \ # Install specific version of ansible-core and latest compatible ansible \ ansible ansible-core~=${ANSIBLE_VERSION} -# Add entrypoint script +# Add an entrypoint script that copies files from the /tmp/home directory into +# the actual home directory +RUN mkdir -p /tmp/home COPY entrypoint.sh / RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] -# Make sure the temporary SSH directory exists since we reference it in the -# entrypoint script -RUN mkdir /tmp/.ssh +# Set VIM as the default editor. Used by 'ansible-vault edit' +ENV EDITOR=/usr/bin/vim + +# Add some useful aliases for instances where we want to log into the container +# and debug stuff +ENV ENV=/etc/profile +RUN echo "alias ll='ls -alFh'" >> /etc/profile.d/aliases.sh WORKDIR /ansible diff --git a/README.md b/README.md index bc9d2bf..c18498f 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,10 @@ The working directory is `/ansible`, so mount your ansible playbooks and invento docker run --rm -it -v $(pwd):/ansible ccoley/ansible:latest ansible -m ping all ``` -If you want to use your local SSH keys, known_hosts, and config in the container then mount them to `/tmp/.ssh` in the container. +If you want to use any files/directories in your home directory from within the container, then you can mount them to `/tmp/home` in the container and they'll be copied to the container user's home directory. This is useful for SSH keys and known_hosts, auth configurations, etc. ```bash -docker run --rm -it -v $(pwd):/ansible -v ~/.ssh:/tmp/.ssh:ro ccoley/ansible:latest ansible -m ping all +docker run --rm -it -v $(pwd):/ansible -v ~/.ssh:/tmp/home/.ssh:ro ccoley/ansible:latest ansible -m ping all ``` ## Building Images Locally diff --git a/entrypoint.sh b/entrypoint.sh index 2ac80a8..94afd58 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,8 +1,8 @@ #!/bin/sh set -e -cp -R /tmp/.ssh /root/.ssh -chown -R root:root /root/.ssh +# Copy and chown files to the root user's home directory +rsync -rlp /tmp/home/ /root/ exec "$@" -- GitLab