Search code examples
dockerdocker-composecontainers

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". 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? How to pass port mapping dynamically to the docker-compose up command?

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:

Solution

  • use a ${VAR} in your docker-compose.yml

    e.g.

    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