简体   繁体   English

如何将端口映射动态传递给 docker-compose up 命令?

[英]How to pass port mapping dynamically to the docker-compose up command?

I have two container images and doing compose and running the docker using " docker-compose up -d ".我有两个容器映像,并使用“ docker-compose up -d ”进行组合和运行 docker。 This works fine.这工作正常。 I want to run the same container image in another port say 8081. Can we pass port mapping as a command line parameter docker-compose up -port novnc :8081:8080?我想在另一个端口运行相同的容器镜像,比如 8081。我们可以将端口映射作为命令行参数 docker-compose up -port novnc :8081:8080 传递吗? How to pass port mapping dynamically to the docker-compose up command?如何将端口映射动态传递给 docker-compose up 命令?

version: '2'
services:
  ide:
    image: myApp
    image: myImage:latest
    environment:
      - DISPLAY=novnc:0.0
    depends_on:
      - novnc
    networks:
      - x11
  novnc:
    image: myImageTwo:latest
    environment:
      - DISPLAY_WIDTH=1600
      - DISPLAY_HEIGHT=968
    ports:
      - "8080:8080"
    networks:
      - x11
networks:
  x11:

use a ${VAR} in your docker-compose.yml在 docker-compose.yml 中使用 ${VAR}

eg例如

version: '2'
services:
  apache:
    image: httpd:2.4
    volumes:
      - .:/usr/local/apache2/htdocs/
    ports:
      - ${APP_PORT}:80

then use environment variable:然后使用环境变量:

$ export APP_PORT=8080
$ docker-compose up

or inline version:或内联版本:

$ APP_PORT=8080 docker-compose up

You can do it using the .env file.您可以使用.env文件来完成。
For example, you'll have something like this :例如,你会有这样的事情:

$ cat .env
TAG=v1.5

$ cat docker-compose.yml
version: '3'
services:
  web:
    image: "webapp:${TAG}"

In the example, you can see that the tag value is in a variable that is set in the env file.在示例中,您可以看到标记值位于 env 文件中设置的变量中。

You can find out more in the official doc您可以在官方文档中找到更多信息

Change your ports section to:将您的端口部分更改为:

    ports:
      - "${MY_PORT}:8080"

and then just use MY_PORT=8081 docker-compose up -d然后只需使用MY_PORT=8081 docker-compose up -d

you can also use a port range您还可以使用端口范围

ports:
  - "8080-8081:8080"

so you don't have to pass as parameter所以你不必作为参数传递

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM