简体   繁体   English

如何将对 docker-compose.yml 所做的更改实施到分离的运行容器

[英]How to implement changes made to docker-compose.yml to detached running containers

The project is currently running in the background from this command:该项目当前正在通过此命令在后台运行:

docker-compose up -d

I need to make two changes to their docker-compose.yml :我需要对他们docker-compose.yml做两个更改:

  • Add a new container添加一个新容器
  • Update a previous container to have a link to the new container更新以前的容器以链接到新容器

After changes are made:更改后:

NOTE the " <-- " arrows for my changes注意我的更改的“ <-- ”箭头

web:
        build: .
        restart: always
        command: ['tini', '--', 'rails', 's']
        environment:
            RAILS_ENV: production
            HOST: example.com
            EMAIL: admin@example.com
        links:
                - db:mongo
                - exim4:exim4.docker # <-- Add link
        ports:
                - 3000:3000
        volumes:
                - .:/usr/src/app
db:
        image: mongo
        restart: always
exim4: # <-------------------------------- Add new container
        image: exim4
        restart: always
        ports:
            - 25:25
        environment:
            EMAIL_USER: user@example.com
            EMAIL_PASSWORD: abcdabcdabcdabcd

After making the changes, how do I apply them?进行更改后,如何应用它们? (without destroying anything) (不破坏任何东西)

I tried docker-compose down && docker-compose up -d but this destroyed the Mongo DB container... I cannot do that... again... :sob:我试过docker-compose down && docker-compose up -d但这破坏了 Mongo DB 容器......我不能这样做......再次...... :sob:

docker-compose restart says it won't recognize any changes made to docker-compose.yml (Source: https://docs.docker.com/compose/reference/restart/) docker-compose restart表示它不会识别对docker-compose.yml所做的任何更改(来源: https : docker-compose.yml

docker-compose stop && docker-compose start sounds like it'll just startup the old containers without my changes? docker-compose stop && docker-compose start听起来它只会在没有我更改的情况下启动旧容器?


Test server:测试服务器:

  • Docker version: 1.11.2, build b9f10c9/1.11.2 Docker 版本: 1.11.2,构建 b9f10c9/1.11.2
  • docker-compose version: 1.8.0, build f3628c7 docker -compose 版本: 1.8.0,构建 f3628c7

Production server is likely using older versions, unsure if that will be an issue?生产服务器可能使用旧版本,不确定这是否会成为问题?

If you just run docker-compose up -d again, it will notice the new container and the changed configuration and apply them.如果您再次运行docker-compose up -d ,它会注意到新容器和更改的配置并应用它们。

But:但:

(without destroying anything) (不破坏任何东西)

There are a number of settings that can only be set at container startup time.有许多设置只能在容器启动时设置。 If you change these, Docker Compose will delete and recreate the affected container.如果您更改这些,Docker Compose 将删除并重新创建受影响的容器。 For example, links are a startup-only option, so re-running docker-compose up -d will delete and recreate the web container.例如,链接是仅启动选项,因此重新运行docker-compose up -d将删除并重新创建web容器。

this destroyed the Mongo DB container... I cannot do that... again...这破坏了 Mongo DB 容器......我不能那样做......再次......

db:
    image: mongo
    restart: always

Add a volumes: option to this so that data is stored outside the container.添加一个volumes:选项,以便数据存储在容器外。 You can keep it in a named volume, possibly managed by Docker Compose, which has some advantages, but a host-system directory is probably harder to accidentally destroy.您可以将它保存在一个命名卷中,可能由 Docker Compose 管理,这有一些优点,但主机系统目录可能更难意外销毁。 You will have to delete and restart the container to change this option.您必须删除并重新启动容器才能更改此选项。 But note that you will also have to delete and restart the container if, for example, there is a security update in MongoDB and you need a new image.但请注意,例如,如果 MongoDB 中存在安全更新并且您需要一个新映像,则您还必须删除并重新启动容器。

Your ideal state here is:您在这里的理想状态是:

  • Actual databases (like your MongoDB container) store data in named volumes or host directories实际数据库(如 MongoDB 容器)将数据存储在命名卷或主机目录中
  • Applications (like your Rails container) store nothing locally, and can be freely destroyed and recreated应用程序(如 Rails 容器)在本地不存储任何内容,可以自由销毁和重新创建
  • All code is in Docker images, which can always be rebuilt from source control所有代码都在 Docker 镜像中,始终可以从源代码控制中重建
  • Use volumes as necessary to inject config files and extract logs根据需要使用卷来注入配置文件和提取日志

If you lose your entire /var/lib/docker directory (which happens!) you shouldn't actually lose any state, though you will probably wind up with some application downtime.如果您丢失了整个/var/lib/docker目录(发生了!),您实际上不应该丢失任何状态,尽管您可能会导致一些应用程序停机。

Just docker-compose up -d will do the job.只需docker-compose up -d就可以完成这项工作。

Output should be like输出应该像

> docker-compose up -d
Starting container1 ... done

> docker-compose up -d
container1 is up-to-date
Creating container2 ... done

As a side note, docker-compose is not really for production.附带说明一下,docker-compose 并不是真正用于生产。 You may want to consider docker swarm.您可能需要考虑 docker swarm。

the key here is that up is idempotent.这里的关键up是幂等的。

  • if you update configuration in docker-compose.yaml如果您更新 docker-compose.yaml 中的配置

    docker compose up -d
  • If compose is building images before run it, and you want to rebuild them:如果 compose 在运行之前正在构建图像,并且您想重建它们:

     docker compose up -d --build

The project is currently running in the background from this command:该项目当前正在此命令在后台运行:

docker-compose up -d

I need to make two changes to their docker-compose.yml :我需要对其docker-compose.yml进行两项更改:

  • Add a new container添加一个新的容器
  • Update a previous container to have a link to the new container更新以前的容器以具有指向新容器的链接

After changes are made:进行更改后:

NOTE the " <-- " arrows for my changes注意我的更改的“ <-- ”箭头

web:
        build: .
        restart: always
        command: ['tini', '--', 'rails', 's']
        environment:
            RAILS_ENV: production
            HOST: example.com
            EMAIL: admin@example.com
        links:
                - db:mongo
                - exim4:exim4.docker # <-- Add link
        ports:
                - 3000:3000
        volumes:
                - .:/usr/src/app
db:
        image: mongo
        restart: always
exim4: # <-------------------------------- Add new container
        image: exim4
        restart: always
        ports:
            - 25:25
        environment:
            EMAIL_USER: user@example.com
            EMAIL_PASSWORD: abcdabcdabcdabcd

After making the changes, how do I apply them?进行更改后,如何应用它们? (without destroying anything) (不破坏任何东西)

I tried docker-compose down && docker-compose up -d but this destroyed the Mongo DB container... I cannot do that... again... :sob:我尝试了docker-compose down && docker-compose up -d但这破坏了Mongo DB容器...我再也不能这样做...:sob:

docker-compose restart says it won't recognize any changes made to docker-compose.yml (Source: https://docs.docker.com/compose/reference/restart/) docker-compose restart表示无法识别对docker-compose.yml所做的任何更改(来源: https : docker-compose.yml

docker-compose stop && docker-compose start sounds like it'll just startup the old containers without my changes? docker-compose stop && docker-compose start听起来像只是在不做任何更改的情况下启动旧容器?


Test server:测试服务器:

  • Docker version: 1.11.2, build b9f10c9/1.11.2 Docker版本: 1.11.2,构建b9f10c9 / 1.11.2
  • docker-compose version: 1.8.0, build f3628c7 docker -compose版本: 1.8.0,构建f3628c7

Production server is likely using older versions, unsure if that will be an issue?生产服务器可能使用较旧的版本,不确定是否会出现问题?

暂无
暂无

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

相关问题 如何在全球范围内显示由 docker-compose 创建的所有正在运行的容器,而不考虑 docker-compose.yml - How to show all running containers created by docker-compose, globally, regardless of docker-compose.yml 如何获取 docker 组合以读取对 docker-compose.yml 的更改? - How to get docker compose to read changes to docker-compose.yml? 如何找到用于启动容器的 docker-compose.yml? - How to find the docker-compose.yml that used to start containers? 在docker-compose.yml中了解容器的主机名 - Understanding Hostname of Containers in docker-compose.yml 如何在Docker撰写文件(docker-compose.yml)中链接容器? - How do I link containers in a docker compose file (docker-compose.yml)? 如何链接两个用两个不同的 docker-compose.yml 生成的 docker 容器 - How to link two docker containers which are spawned with two different docker-compose.yml 如何使用 docker-compose.yml 而不是运行命令来配置这些 docker 容器? - How can I configure these docker containers using a docker-compose.yml instead of a run command? 如何在 docker 桌面中重命名 docker-compose.yml 容器组? - How can I rename docker-compose.yml containers group in docker desktop? docker-compose.yml在多个VM上启动容器 - docker-compose.yml to start containers on multiple VM's "更新后容器未重启,docker-compose.yml 中始终重启" - Containers not restarted after update with restart always in docker-compose.yml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM