A comprehensive Docker-based build and deployment solution for Astro applications, featuring:
- β Automated Docker image builds with 8-character SHA tags
- β Push to GitHub Container Registry (ghcr.io)
- β GitHub Actions CI/CD workflows
- β
One-click deployment scripts (
deploy.sh) - β Docker Compose orchestration
.
βββ Dockerfile # Multi-stage Docker build
βββ docker-compose.yml # Docker Compose configuration
βββ deploy.sh # Deployment script (Linux/Mac)
βββ deploy.ps1 # Deployment script (Windows)
βββ .dockerignore # Docker build context optimization
βββ .env.example # Environment variables template
βββ .gitignore # Git ignore rules
βββ .github/
βββ workflows/
βββ docker-build.yml # GitHub Actions workflow
Ensure you have the following installed and configured:
- Astro project
- Docker and Docker Compose
- GitHub account
GitHub Actions will automatically use GITHUB_TOKEN in your repositoryβno additional configuration required.
For manual GHCR login, create a Personal Access Token:
- Go to GitHub Settings > Developer settings > Personal access tokens
- Generate a new token with
write:packagespermission - Add to repository Settings > Secrets and variables > Actions:
GHCR_USERNAME: Your GitHub usernameGHCR_TOKEN: Generated token
cp .env.example .envEdit .env file with your GitHub username and repository information.
If your Astro project has specific requirements, modify Dockerfile:
- Change Node.js version (currently using node:20-alpine)
- Add additional build steps
- Modify port number (default 4321)
Note: The Dockerfile uses npm install instead of npm ci to support projects without a package-lock.json file. For faster, reproducible builds in production, you can:
- Generate a package-lock.json:
npm install - Commit it:
git add package-lock.json - Update Dockerfile to use
npm cifor better performance
See BUILD_TROUBLESHOOTING.md for more details.
When pushing to main/master branch, GitHub Actions will automatically:
- Build Docker image
- Generate tag with 8-character commit SHA
- Push to ghcr.io
Example: ghcr.io/username/repo:abc12345
# Auto-detect and deploy latest version
./deploy.sh
# Deploy specific SHA version (8 characters)
./deploy.sh abc12345
# Use latest tag
./deploy.sh latestThe deployment script will:
- Automatically pull image from ghcr.io
- Stop old containers
- Start new container using Docker Compose
# Set environment variables
export IMAGE_TAG=abc12345
export REPO_OWNER=your-username
export REPO_NAME=your-repo
# Pull image
docker pull ghcr.io/${REPO_OWNER}/${REPO_NAME}:${IMAGE_TAG}
# Start container
docker compose up -ddocker run -d \
--name astro-app \
-p 4321:4321 \
--restart unless-stopped \
ghcr.io/your-username/your-repo:abc12345| Variable | Description | Default |
|---|---|---|
REGISTRY |
Image registry URL | ghcr.io |
REPO_OWNER |
GitHub username or organization | From git remote |
REPO_NAME |
Repository name | From git remote |
IMAGE_TAG |
Image tag (SHA or latest) | latest |
By default, Astro application runs on port 4321.
To modify, edit docker-compose.yml:
ports:
- "8080:4321" # Map external port 8080 to internal port 4321# Check container status
docker ps
# View logs
docker logs astro-app
# Enter container
docker exec -it astro-app sh
# Health check
curl http://localhost:4321Use deployment script to auto-fetch latest version:
./deploy.sh# Stop old container
docker compose down
# Pull new image
docker pull ghcr.io/your-username/your-repo:newsha
# Start container
docker compose up -d# Redeploy with previous SHA
./deploy.sh oldsha123Ensure you're logged into GHCR:
docker login ghcr.io
# Enter GitHub username and Personal Access TokenCheck logs:
docker logs astro-appModify port mapping in docker-compose.yml:
ports:
- "NEW_PORT:4321"Workflow file: .github/workflows/docker-build.yml
Triggers:
- Push to main/master branch
- Tag creation
- Pull Request
Build outputs:
- Multi-platform images (amd64, arm64)
- 8-character SHA tags
- Latest commit tag
- Semver tags (if triggered by tag)
- Never commit sensitive information: Ensure
.envis in.gitignore - Use Secrets: Manage credentials via GitHub Actions Secrets
- Limit package visibility: Control Container packages visibility in repository settings
- Rotate tokens regularly: Periodically update Personal Access Tokens
Issues and Pull Requests are welcome!
MIT License