test-gitea-actions/docker-in-docker-example.sh
Your Name 031d552374
All checks were successful
Docker-in-Docker Example / docker-example (push) Successful in 7s
test-actions / build (push) Successful in 3s
'Generic Commit'
2025-06-01 11:53:25 +12:00

85 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
echo "=== Docker-in-Docker Example for Gitea Actions ==="
echo "JOB_CONTAINER_NAME: ${JOB_CONTAINER_NAME:-not set}"
echo "GITHUB_WORKSPACE: ${GITHUB_WORKSPACE:-$(pwd)}"
echo ""
# Create a simple Dockerfile for testing
cat > Dockerfile.test << 'EOF'
FROM alpine:latest
RUN apk add --no-cache bash
WORKDIR /app
COPY . .
CMD ["bash", "-c", "echo 'Hello from Docker container!' && ls -la"]
EOF
# Create a test file
echo "This is a test file created in Gitea Actions" > test-data.txt
if [ -n "${JOB_CONTAINER_NAME:-}" ]; then
echo "Running in Gitea Actions - using --volumes-from for Docker operations"
echo ""
# Build the Docker image
echo "1. Building Docker image..."
docker build -f Dockerfile.test -t test-image:latest .
echo ""
# Run container with volumes from the job container
echo "2. Running Docker container with shared volumes..."
docker run --rm \
--volumes-from="${JOB_CONTAINER_NAME}" \
-w "${GITHUB_WORKSPACE}" \
test-image:latest
echo ""
# Example: Running a command that needs access to workspace files
echo "3. Running Alpine container to process workspace files..."
docker run --rm \
--volumes-from="${JOB_CONTAINER_NAME}" \
alpine:latest sh -c "cd ${GITHUB_WORKSPACE} && echo 'Files in workspace:' && find . -type f -name '*.txt' | head -10"
echo ""
# Example: Using docker-compose (if needed)
echo "4. Creating docker-compose.yml for multi-container setup..."
cat > docker-compose.test.yml << EOF
version: '3'
services:
app:
image: alpine:latest
volumes_from:
- ${JOB_CONTAINER_NAME}
working_dir: ${GITHUB_WORKSPACE}
command: ["sh", "-c", "echo 'Hello from docker-compose!' && ls -la"]
EOF
# Note: docker-compose with volumes_from requires special handling
echo "Note: For docker-compose, you may need to use docker run instead or configure the runner differently"
else
echo "Running locally - using standard volume mounts"
echo ""
# Build the Docker image
echo "1. Building Docker image..."
docker build -f Dockerfile.test -t test-image:latest .
echo ""
# Run container with local volume mount
echo "2. Running Docker container with local volume..."
docker run --rm -v "$(pwd):/app" test-image:latest
echo ""
# Example: Running Alpine with local mount
echo "3. Running Alpine container with local mount..."
docker run --rm -v "$(pwd):/workspace" -w /workspace alpine:latest sh -c "echo 'Files in workspace:' && find . -type f -name '*.txt' | head -10"
fi
# Cleanup
rm -f Dockerfile.test test-data.txt docker-compose.test.yml
echo ""
echo "=== Example completed successfully ==="