github动作docker数据库迁移失败 [英] github action docker database migration fail
问题描述
我正在尝试使用github动作构建CI/CD环境.
I am trying to build CI/CD environment using github action.
下面的脚本可帮助我构建docker映像,但返回错误
Below script helps me to build the docker image, but returning an error
django.db.utils.OperationalError: could not connect to server: Operation timed out
Is the server running on host "db-postgresql-nyc1.b.db.ondigitalocean.com" (157.230.224.47) and accepting
TCP/IP connections on port 23052?
localhost可以成功迁移数据库,而不会发生任何错误.但是,当我使用github操作时,它会失败.
localhost can successfully migrate the database without any errors. However, when I use github action, it fails.
也许我应该尝试操纵来自数字海洋或github的防火墙?如果您有任何建议,请告诉我.
Maybe I should try manipulating firewalls from digital ocean or github? If you have any suggestion, please let me know.
name: Python application
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: docker login
env:
DOCKER_ID: ${{ secrets.DOCKER_USER }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
run: |
docker login -u $DOCKER_ID -p $DOCKER_PASSWORD
- name: docker build
run: |
docker rmi --force "dummy/dummy-community:latest"
docker build -t polls .
- name: docker migrate
run: |
docker run --env-file env polls sh -c "python manage.py makemigrations && python manage.py migrate"
- name: docker tag image
run: |
docker tag polls:latest dummy/dummy-community:latest
- name: docker push
run: |
docker push dummy/dummy-community:latest
- name: kubectl deploy
run: |
kubectl delete pods polls-app
kubectl get deploy polls-app
推荐答案
在启动Postgres和Django之间通常存在竞争条件.当Postgres容器启动时,该容器被列为已启动,但是在后台,实际的数据库服务器进程仍在启动.Django容器启动时,它将立即尝试连接到数据库,但失败.
There is often a race condition between starting up Postgres and Django. When the Postgres container starts up, the container is listed as started, but underneath the hood, the actual database server process is still starting up. When the Django container starts up, it immediately tries to connect to the database but fails.
通常,人们可能会采取以下两种行动之一:
Typically, one might take one of two courses of action:
-
使用较小的Postgres图像.使用更少的东西,数据库服务器进程启动速度更快.有时
postgres:12-alpine
(或与您的版本要求匹配的< version< -alpine
图像)将足够快地旋转postgres,以便Django可以通过以下方式访问它:它启动的时间.但是,这仍然是一场竞赛,您可能会遇到相同的错误
Use a smaller Postgres image. With less stuff to work with, the database server process starts up faster. Sometimes the
postgres:12-alpine
(or the<version>-alpine
image that matches your version requirement) will spin up postgres fast enough so that Django can access it by the time it starts up. But still, it's a race to the finish, and you can encounter the same error
在继续进行 makemigrations
之前,编写一个轮询脚本以确认Postgres已启动并进行查询:
Write a polling script to confirm that Postgres is up and taking queries before moving on to makemigrations
:
#!/bin/bash
while :; do
# Check if database is taking queries
psql -Atc "SELECT 1" -p <port> -U <user> <database>
if [[ $? -eq 0 ]]; then
break
fi
echo "Database is not started, waiting and trying again
sleep 0.5 # or however long you want
done
# broke out of the while loop, proceed to migration
python manage.py makemigrations && python manage.py migrate
当然,上面的代码片段是基本的检查-您可能想写一些更健壮的东西.
Of course, the above code snippet is a rudimentary check -- you may want to write something a bit more robust.
这篇关于github动作docker数据库迁移失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!