dockerdind / start.sh
memex-in's picture
Update start.sh
b6cfdce verified
raw
history blame
3.27 kB
#!/bin/bash
set -eo pipefail # Exit immediately if a command exits with a non-zero status.
# Also exit if any command in a pipeline fails.
# Ensure /usr/local/bin is in PATH for the 'docker' user
export PATH="/usr/local/bin:$PATH"
echo "--- Debugging Environment ---"
echo "Current user: $(whoami)"
echo "Current working directory: $(pwd)"
echo "PATH: $PATH"
echo "--- File System Checks ---"
echo "Listing contents of /usr/local/bin/"
ls -la /usr/local/bin/ || echo "ls failed in /usr/local/bin"
echo "Checking for dockerd-rootless.sh using 'which'"
which dockerd-rootless.sh || echo "dockerd-rootless.sh not found by 'which'"
echo "Checking for dockerd-rootless.sh using 'find' from root"
find / -name "dockerd-rootless.sh" 2>/dev/null || echo "dockerd-rootless.sh not found by 'find'"
echo "--- Starting Docker Daemon ---"
# Start the rootless Docker daemon in the background.
# The `docker:dind-rootless` image's default `ENTRYPOINT` is `dockerd-rootless.sh`.
# Since we are overriding the ENTRYPOINT with `start.sh`, `start.sh` must explicitly
# run `dockerd-rootless.sh`.
# We use `exec` here so that `dockerd-rootless.sh` becomes the main process,
# allowing it to correctly set up the rootless environment and `rootlesskit` re-execution.
# We then put it in the background and wait for it.
# IMPORTANT: The `dockerd-rootless.sh` script itself uses `exec rootlesskit ... $0 $@`
# This means it replaces the current shell process.
# So, we cannot simply run it with `&` and expect the rest of `start.sh` to execute.
# Instead, we need to manage the daemon's lifecycle differently.
# Let's try to run dockerd-rootless.sh directly as the primary process,
# and then use a separate mechanism to run our build/run commands.
# This is problematic for a single CMD/ENTRYPOINT.
# Alternative approach: Run dockerd-rootless.sh in the background, but use a loop to wait for its socket.
# This is the most common pattern for DinD when you need to run other commands.
echo "Attempting to run dockerd-rootless.sh in background and wait for socket..."
/usr/local/bin/dockerd-rootless.sh &
DAEMON_PID=$! # Capture the PID of the background daemon
# Wait for the Docker daemon to be fully ready
echo "Waiting for Docker daemon to become available..."
until docker info >/dev/null 2>&1; do
echo 'Still waiting for Docker daemon to start...'
sleep 1
done
echo 'Docker daemon is up and running!'
echo "--- Performing Inner Docker Operations ---"
# Build the Nginx image using the copied exdocker Dockerfile
echo "Building my-nginx-app image..."
docker build -f exdocker -t my-nginx-app .
# Run the Nginx container in detached mode, mapping its port 80
# to port 8080 *inside this DinD container*.
echo "Running my-inner-nginx container..."
docker run -d -p 8080:80 --name my-inner-nginx my-nginx-app
echo 'Inner Nginx container started. Testing accessibility...'
# Test accessibility of the inner Nginx server
curl http://localhost:8080
echo "--- DinD Container Running ---"
echo "DinD container is running. Access Nginx at http://localhost:8080 (from host)."
# Keep this DinD container running indefinitely by waiting on the daemon process
wait $DAEMON_PID # Wait for the dockerd-rootless.sh process to finish, which keeps the container alive