简体   繁体   English

使用docker-compose在服务器上构建多容器docker映像

[英]Building multi-container docker image on server with docker-compose

I'm working on a multi-service docker image which is working fine locally, but having trouble building it server-side (likely a knowledge gap). 我正在处理一个多服务的docker映像,该映像在本地运行良好,但是在服务器端构建它时遇到了麻烦(可能是知识鸿沟)。 One service is running my mysql database while the other is my main application (each with their own Dockerfile, though I suppose that's a given), both linked through docker-compose and using depends_on to run setup mysql first and ensure it's in a healthy state. 一种服务正在运行我的mysql数据库,另一种服务是我的主应用程序(每个应用程序都有自己的Dockerfile,尽管我认为这是给定的),两者均通过docker-compose链接并使用depends_on首先运行安装mysql并确保其处于正常状态。

I've succeeded in building and running the image locally, and have pulled it to the server after pushing to docker hub, but can't find any guidance on how to properly use my docker-compose.yml file in production. 我已经成功在本地构建和运行映像,并在推送到docker hub后将其拉到服务器,但是找不到有关如何在生产中正确使用docker-compose.yml文件的指导。 It took me a while to realise I'd need to upload the docker-compose file to production separately, but now I'm puzzled about the 'build' settings. 我花了一段时间才意识到我需要将docker-compose文件单独上传到生产环境,但是现在我对“ build”设置感到困惑。

Locally, I'm using a docker-compose.yml file similar to the one below, with the 'build' setting pointing to the db_config folder in the project directory. 在本地,我正在使用类似于以下文件的docker-compose.yml文件,其“ build”设置指向项目目录中的db_config文件夹。

version: '2.1'
services:
    mysql:
        image: username/my_image:latest
        container_name: db
        build: ./db_config
        environment:
            MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
            MYSQL_USER: "dbuser"
            MYSQL_PASSWORD: "12345"
            MYSQL_DATABASE: "my_db"
        healthcheck:
            test: "exit 0"

    my_app:
        container_name: app
        build: .
        depends_on:
            mysql:
                condition: service_healthy
        entrypoint:
            - /usr/local/bin/docker-entrypoint.sh
        ports:
            - "80:8080"

Trying to build ( docker-compose build ) using this file on production gives the following error (names changed): 尝试在生产中使用此文件进行构建( docker-compose build )会产生以下错误(名称更改):

build path /home/user/my_app/db_config either does not exist, is not accessible, or is not a valid URL. 构建路径/ home / user / my_app / db_config不存在,不可访问或不是有效的URL。

What should 'build' point to in production? 生产中“构建”应该指向什么? Should I somehow be pointing to the Dockerfile in the docker image itself, or should this config be completely different in production? 我应该以某种方式指向Docker映像本身中的Dockerfile,还是该配置在生产中完全不同? Do any other configurations need changing too? 是否还有其他配置需要更改?

The thing is that you shouldn't build a docker image for production. 事实是,您不应该为生产构建docker映像。

The usual setup is that you build an image that can run on multiple environments. 通常的设置是构建可在多种环境下运行的映像。 On UAT you test this image and when it's ok, you make the production environment to stop current containers and then you start new ones based on the new image. 在UAT上,您可以测试此映像,如果还可以,则使生产环境停止当前容器,然后根据新映像启动新容器。

To store the image, you can use ie docker registry. 要存储图像,您可以使用docker Registry。

As for your errors, they're happening because on the machine where you execute docker-compose build you do not have the db_config file. 至于您的错误,是因为您执行docker-compose build的机器上没有db_config文件而发生。 Similar to the necessity to upload compose to production, in order to build an image on the production environment, you need to provide all necessary project files that are needed for the build. 与将撰写文件上传到生产环境类似,为了在生产环境上构建映像,您需要提供构建所需的所有必要项目文件。

If you want to make it "clean" then the proper way is to build an image on build environment, push the image to the registry and then on the production just do the deployment. 如果要使其“干净”,则正确的方法是在构建环境中构建映像,将映像推送到注册表,然后在生产环境中进行部署。

And yes, for the production deployment you need the compose file. 是的,对于生产部署,您需要撰写文件。 However it's not needed for you to do the build. 但是,您不需要进行构建。 If the docker registry is accessible you should first stop the running containers and then start them again. 如果可以访问Docker注册表,则应先停止正在运行的容器,然后再次启动它们。

If you operate on swarm, it should be easier since swarm will check if containers have newer image base and will stop them only if image update is required. 如果您对swarm进行操作,应该会更容易,因为swarm会检查容器是否具有较新的映像库,并且仅在需要映像更新时才停止它们。

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

相关问题 运行 docker-compose cmd 时出现错误“使用 Docker 定义和运行多容器应用程序” - Error "Define and run multi-container applications with Docker" when running docker-compose cmd Docker环境多容器结构中PostgreSQL和.Net Core的docker-compose文件应该怎么做? - How should be docker-compose file for PostgreSQL and .Net Core in Docker Environment multi-container structure? 在多容器 docker-compose 设置中,我应该添加什么作为 api 地址? - What should I add as api-address when within a multi-container docker-compose setup? Docker-Compose - 如何扩展多容器应用程序? - Docker-Compose - How can i scale a multi-container application? 在单个 docker-compose 文件中运行多个多容器应用程序 - Running multiple multi-container applications in a single docker-compose file 使用 Docker Compose 运行同一个多容器应用程序的副本 - Running copies of the same multi-container app with Docker Compose AWS 上的多容器 Docker - Multi-Container Docker on AWS Docker-compose没有建立一个容器 - Docker-compose is not building one container 使用 docker-compose 构建 docker 镜像并推送到镜像/容器注册表? - Building a docker image and pushing to a image/container registry using docker-compose? 带有Docker多容器和Node的EB - EB with docker multi-container and Node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM