如何使用docker-compose与CircleCI测试并部署项目?附配置文件
Great question! Let’s break this down into setting up testing and deployment with your existing docker-compose.yml and CircleCI. I’ll use your current config as a starting point to make this concrete.
1.1 Create a CircleCI Config File
First, add a .circleci/config.yml file to your project root. This will define how CircleCI runs your tests. Here’s a tailored example based on your services:
version: 2.1 jobs: test: docker: - image: docker:20.10.17 steps: - checkout # Install Docker Compose - run: name: Install Docker Compose command: | apk add --no-cache docker-compose # Start all services (PHP, MySQL, Redis, Node) - run: name: Start services with Docker Compose command: docker-compose up -d # Wait for MySQL/Redis to be ready (critical for tests) - run: name: Wait for MySQL to initialize command: | until docker-compose exec -T mysql mysqladmin ping -h localhost --silent; do sleep 2 done - run: name: Wait for Redis to be ready command: | until docker-compose exec -T redis redis-cli ping | grep PONG; do sleep 2 done # Run PHP tests (e.g., PHPUnit) - run: name: Run PHP tests command: docker-compose exec -T php php vendor/bin/phpunit # Run Node.js tests (e.g., Jest) - run: name: Run Node.js tests command: docker-compose exec -T node npm test # Clean up - run: docker-compose down workflows: test-deploy: jobs: - test # We'll add deployment next
1.2 Key Notes for Testing
- Dependency Health Checks: The
waitcommands ensure MySQL and Redis are fully initialized before running tests—this prevents flaky failures from services not being ready. - Run Tests in Containers: Using
docker-compose execruns tests directly in your existing PHP/Node containers, matching your local environment exactly. - Cache Dependencies: To speed up builds, add cache steps for
vendor/(PHP) andnode_modules/(Node). For example:- restore_cache: keys: - php-vendor-{{ checksum "composer.lock" }} - php-vendor- - run: docker-compose exec -T php composer install - save_cache: key: php-vendor-{{ checksum "composer.lock" }} paths: - ./vendor
Once your tests pass, you can extend the workflow to deploy your app. Let’s cover two common scenarios: deploying to a remote server, or pushing images to a container registry.
2.1 Deploy to a Remote Server
If you’re deploying to a VPS or dedicated server, you can use SSH to run docker-compose commands remotely.
First, add your server’s SSH key to CircleCI’s project settings (under SSH Keys). Then update your config.yml:
jobs: # ... (keep the test job) deploy: docker: - image: docker:20.10.17 steps: - checkout - run: apk add --no-cache docker-compose openssh-client # Add server to known hosts to avoid SSH prompts - run: name: Add server to known hosts command: ssh-keyscan -H your-server-ip >> ~/.ssh/known_hosts # Copy docker-compose.yml to server - run: scp docker-compose.yml user@your-server-ip:/path/to/app/ # Deploy with docker-compose - run: name: Deploy to server command: | ssh user@your-server-ip << 'EOF' cd /path/to/app/ docker-compose down docker-compose pull docker-compose up -d EOF workflows: test-deploy: jobs: - test - deploy: requires: - test filters: branches: only: main # Only deploy from main branch
2.2 Push Images to a Container Registry (Optional)
If you want to build and push your custom images (Nginx, PHP, Node) to a registry like Docker Hub or AWS ECR, add a build-push step:
jobs: build-images: docker: - image: docker:20.10.17 steps: - checkout - run: apk add --no-cache docker-compose # Log in to Docker Hub - run: echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin # Build and push images - run: docker-compose build nginx php node - run: docker-compose push nginx php node
Then add this job to your workflow (after tests pass).
docker-compose.yml - Add Health Checks: To make waiting for services more reliable, add health checks to your MySQL and Redis services:
mysql: image: mysql healthcheck: test: ["CMD", "mysqladmin", "ping", "-h", "localhost"] interval: 5s timeout: 5s retries: 5 redis: image: redis healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 3s timeout: 3s retries: 5 - Environment Variables: Use a
.envfile for sensitive data (DB credentials, API keys) and add it to CircleCI’s environment variables (instead of committing it to Git).
内容的提问来源于stack exchange,提问作者Trent




