How to Build and Deploy a Python Flask App with GitLab CI/CD and Docker
This guide walks you through creating a Python Flask project, manually building and running its Docker image, configuring GitLab runners, writing a .gitlab-ci.yml pipeline for style checks, tests, deployment, and release, and managing a private Docker registry for versioned releases.
1. Create a Python project
Clone the open‑source Flask demo from GitHub ( https://github.com/imooc-course/docker-cloud-flask-demo ) into a new GitLab project.
2. Manual deployment
Clone the project locally, build the Docker image, and run the container.
➜ [/Users/mac/PycharmProjects] git clone http://gitlab.example.com/root/flask-demo.git
Cloning into 'flask-demo'...
... (output omitted for brevity) ...
➜ [/Users/mac/PycharmProjects/flask-demo] docker build -t flask-demo .
➜ [/Users/mac/PycharmProjects/flask-demo] docker run -d -p 5000:5000 flask-demoOpen a browser and navigate to http://<i>your‑ip</i>:5000 to verify the service.
3. Deploy with GitLab CI/CD pipelines
Register three GitLab runners using Docker as the executor: one for Python 2.7, one for Python 3.4, and a shell runner tagged demo.
➜ [/Users/mac] gitlab-runner register
Runtime platform arch=amd64 os=darwin ...
Please enter the gitlab-ci coordinator URL:
http://gitlab.example.com/
Please enter the gitlab-ci token:
6-uZ1ndZ2NRGp8_TghnL
Please enter the gitlab-ci tags (comma separated):
python3.4
... (registration output) ... ➜ [/Users/mac] gitlab-runner register
... (similar steps for python2.7) ... ➜ [/Users/mac] gitlab-runner register
... (shell runner with tag demo) ...Verify that the runners are alive:
➜ [/Users/mac] gitlab-runner verify
Verifying runner... is alive runner=NkYLeMbb3.1 Create .gitlab-ci.yml
stages:
- style
- test
pep8:
stage: style
script:
- pip install tox
- tox -e pep8
tags:
- python2.7
unittest-py27:
stage: test
script:
- pip install tox
- tox -e py27
tags:
- python2.7
unittest-py34:
stage: test
script:
- pip install tox
- tox -e py34
tags:
- python3.4Commit and push the file; the pipeline automatically runs the style and test jobs.
git add .
git commit -m "add .gitlab-ci.yml"
git push -u origin masterIn GitLab CI/CD > Pipelines you can see each job’s logs and verify that the Python 2.7 environment is ready.
4. Full CI/CD workflow
Protect the master branch (no direct pushes) and require successful pipelines before merge requests can be merged.
Create a dev branch, push changes, and open a merge request.
git fetch
git checkout dev
... (make changes) ...
git push origin dev4.1 Private Docker registry
Run a local registry container:
docker run -d -v ~/registry:/var/lib/registry -p 5001:5000 --restart=always --name registry registry:2Tag and push an image to the registry:
docker tag busybox registry.example.com:5001/busybox
docker push registry.example.com:5001/busybox4.2 Extend .gitlab-ci.yml for deployment and release
stages:
- style
- test
- deploy
- release
pep8:
stage: style
script:
- pip install tox
- tox -e pep8
tags:
- python2.7
except:
- tags
unittest-py27:
stage: test
script:
- pip install tox
- tox -e py27
tags:
- python2.7
except:
- tags
unittest-py34:
stage: test
script:
- pip install tox
- tox -e py34
tags:
- python3.4
except:
- tags
docker-deploy:
stage: deploy
script:
- docker build -t flask-demo .
- if [ $(docker ps -aq --filter name=web) ]; then docker rm -f web; fi
- docker run -d -p 5000:5000 --name web flask-demo
tags:
- demo
only:
- master
docker-images-release:
stage: release
script:
- docker build -t registry.example.com:5001/flask-demo:$CI_COMMIT_TAG .
- docker push registry.example.com:5001/flask-demo:$CI_COMMIT_TAG
tags:
- demo
only:
- tagsWhen a tag is pushed, the release job builds a versioned image and pushes it to the private registry, completing the CI‑based version release.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
