简体   繁体   English

使用 Docker 在我的 VPS 上部署我的应用程序的最佳实践是什么?

[英]What is the best practice for deploying my application on my VPS using Docker?

I do have a (Python Flask) application that I want to deploy using GitLab CI and Docker to my VPS.我确实有一个(Python Flask)应用程序,我想使用 GitLab CI 和 Docker 将其部署到我的 VPS。

On my server I want to have a production version and a staging version of my application.在我的服务器上,我想拥有我的应用程序的生产版本和暂存版本。 Both of them require a MongoDB connection.它们都需要 MongoDB 连接。

My plan is to automatically build the application on GitLab and push it to GitLab's Docker Registry.我的计划是在 GitLab 上自动构建应用程序并将其推送到 GitLab 的 Docker Registry。 If I want to deploy the application to staging or production I do a docker pull , docker rm and docker run .如果我想将应用程序部署到暂存或生产,我会执行docker pulldocker rmdocker run

The plan is to store the config (eg secret_key ) in .production.env (and .staging.env ) and pass it to application using docker run --env-file ./env.list该计划是存储的配置(例如secret_key中) .production.env (和.staging.env ),并使用它传递给应用docker run --env-file ./env.list

I already have MongoDB installed on my server and both environments of the applications shall use the same MongoDB instance, but a different database name (configured in .env ).我已经在我的服务器上安装了 MongoDB,应用程序的两个环境都应使用相同的 MongoDB 实例,但使用不同的数据库名称(在.env配置)。

Is that the best practice for deploying my application?这是部署我的应用程序的最佳实践吗? Do you have any recommendations?你有什么建议? Thanks!谢谢!

Here's my configuration that's worked reasonably well in different organizations and project sizes:这是我在不同组织和项目规模中运行良好的配置:

To build:构建:

  1. The applications are located in a git repository (GitLab in your case).应用程序位于git 存储库(在您的情况下为 GitLab)。 Each application brings its own Dockerfile.每个应用程序都有自己的 Dockerfile。
  2. I use Jenkins for building, you can, of course, use any other CD tooling.我使用Jenkins进行构建,当然,您可以使用任何其他 CD 工具。 Jenkins pulls the application's repository, builds the docker image and publishes it into a private Docker repository ( Nexus, in my case). Jenkins 拉取应用程序的存储库,构建 docker 映像并将其发布到私有 Docker 存储库(在我的情况下为Nexus )。

To deploy:部署:

  1. I have one central, application-independent repository that has a docker-compose file (or possibly multiple files that extend one central file for different environments).我有一个独立于应用程序的中央存储库,其中包含一个 docker-compose 文件(或者可能是多个文件,这些文件为不同环境扩展了一个中央文件)。 This file contains all service definitions and references the docker images in my Nexus repo.该文件包含所有服务定义并引用了我的 Nexus 存储库中的 docker 镜像。
  2. If I am using secrets, I store them in a HashiCorp Vault instance.如果我使用机密,我会将它们存储在 HashiCorp Vault 实例中。 Jenkins pulls them, and writes them into an .env file. Jenkins 拉取它们,并将它们写入.env文件。 The docker-compose file can reference the individual environment variables. docker-compose 文件可以引用各个环境变量。
  3. Jenkins pulls the docker-compose repo and, in my case via scp, uploads the docker-compose file(s) and the .env file to my server(s). Jenkins 拉取 docker-compose repo,在我的情况下通过 scp,将 docker-compose 文件和 .env 文件上传到我的服务器。
  4. It then triggers a docker-compose up (for smaller applications) or re-deploys a docker stack into a swarm (for larger applications).然后它会触发docker-compose up (对于较小的应用程序)或将 docker 堆栈重新部署到 swarm 中(对于较大的应用程序)。
  5. Jenkins removes everything from the target server(s). Jenkins 从目标服务器中删除所有内容。

If you like it, you can do step 3. via Docker Machine.如果你喜欢它,你可以通过 Docker Machine 执行第 3 步。 I feel, however, its benefits don't warrant use in my cases.然而,我觉得它的好处不值得在我的情况下使用。

One thing I can recommend, as I've done it in production several times is to deploy Docker Swarm with TLS Encrypted endpoints.我可以推荐的一件事是,因为我已经在生产环境中做过多次,所以使用 TLS 加密端点部署 Docker Swarm。 This link talks about how to secure the swarm via certificate.此链接讨论如何通过证书保护群。 It's a bit of work, but what it will allow you to do is define services for your applications.这是一些工作,但它允许您做的是为您的应用程序定义服务。

The services, once online can have multiple replicas and whenever you update a service (IE deploy a new image) the swarm will take care of making sure one is online at all times.这些服务一旦在线就可以有多个副本,并且每当您更新服务(即部署新映像)时,swarm 都会负责确保一个服务始终在线。

docker service update <service name> --image <new image name>

Some VPS servers actually have Kubernetes as a service (Like Digital Ocean) If they do, it's more preferable.一些 VPS 服务器实际上有 Kubernetes 即服务(如 Digital Ocean),如果有,那就更可取了。 Gitlab actually has an autodevops feature and can remotely manage your Kubernetes cluster, but you could also manually deploy with kubectl. Gitlab 实际上有一个 autodevops 功能,可以远程管理你的 Kubernetes 集群,但你也可以使用 kubectl 手动部署。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将我自己的代码连接到kubernetes中的标准docker镜像的最佳实践 - Best practice to connect my own code into a standard docker image in kubernetes 将我的 ktor 应用程序部署到 docker 时运行问题。jar 文件 - Problem running .jar file while deploying my ktor application to docker 使用docker for CI / CD时节省空间的最佳做法是什么? - What is the best practice to save space when using docker for CI/CD? 使用 Docker 部署我的 Python (FastAPI) 应用程序:ModuleNotFoundError: No module named 'FolderInStructure' - Deploying my Python (FastAPI) Application with Docker: ModuleNotFoundError: No module named 'FolderInStructure' 哪些端口在我的 docker 应用程序中意味着什么? - Which ports mean what in my docker application? docker中定期程序的最佳实践是什么? - What is the best practice for periodic programs in docker? Docker CD / CI工作流程的最佳实践是什么? - What is the best practice for Docker CD/CI workflow? Ubuntu下docker+ufw的最佳实践是什么 - What is the best practice of docker + ufw under Ubuntu 从Docker容器中运行的JVM应用程序将日志发送到graylog的最佳做法是什么? - What's the best practice to send logs to graylog from a JVM application which runs within a docker container? 使用 Docker 构建我的 web 应用程序时出错 - Error building my web application using Docker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM