V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
css3
V2EX  ›  Docker

docker-copmose build 带参数的镜像,并启动容器的命令是什么?

  •  
  •   css3 · 2020-09-22 20:55:34 +08:00 · 3174 次点击
    这是一个创建于 1556 天前的主题,其中的信息可能已经有所发展或是发生改变。

    常用的 docker-compose build 镜像并启动的命令是

    docker-compose up -d --build
    

    今天 build 一个 Dockerfile 中带参的镜像并启动,发现死活不行?是不支持吗?

    docker-compose.yml

    # docker-compose.yml
    version: "3"
    
    services:
      ei:
        build:
          context: .
          dockerfile: Dockerfile
          args:
            - NODE_VERSION
    

    Dockerfile

    # Dockerfile
    ARG NODE_VERSION
    
    FROM node:$NODE_VERSION
    
    RUN echo "-> $NODE_VERSION"
    

    尝试了单独 build 镜像,是能 build 的镜像的

    docker-compose build --build-arg NODE_VERSION=test ei
    

    然后想 buildup 的命令如下:

    docker-compose up -d build --build-arg NODE_VERSION=test ei
    

    报错(我传的 serviceei ,这变成 build 了😳):

    ERROR: No such service: build
    

    那么问题来了,这个是不支持一条命令就能够 build 带参的 buildup

    8 条回复    2020-09-23 10:02:44 +08:00
    powerfulyang
        1
    powerfulyang  
       2020-09-22 21:33:43 +08:00 via iPhone
    --force-recreate 为什么不看看文档呢
    powerfulyang
        2
    powerfulyang  
       2020-09-22 21:35:42 +08:00 via iPhone
    我的错😩我是瞎子
    cheng6563
        3
    cheng6563  
       2020-09-22 21:37:42 +08:00 via Android
    是不是版本问题,3 指的是 3.0 你看 3.1 和之后的版本有没有这参数
    powerfulyang
        4
    powerfulyang  
       2020-09-22 21:40:16 +08:00 via iPhone
    是--build
    jakezh
        5
    jakezh  
       2020-09-22 21:48:57 +08:00
    # docker-compose.yml
    version: "3"

    services:
    ei:
    build:
    context: .
    dockerfile: Dockerfile
    args:
    NODE_VERSION:test
    css3
        6
    css3  
    OP
       2020-09-22 22:01:34 +08:00 via iPhone
    @powerfulyang 不行的,试过了
    css3
        7
    css3  
    OP
       2020-09-22 22:02:44 +08:00 via iPhone
    @jakezh 要 build 的时候命令行传值,不是写死在 cocompose
    css3
        8
    css3  
    OP
       2020-09-23 10:02:44 +08:00
    @powerfulyang

    docker-compose up -d --build --build-arg NODE_VERSION=test ei

    直接弹-h 选项
    Builds, (re)creates, starts, and attaches to containers for a service.

    Unless they are already running, this command also starts any linked services.

    The `docker-compose up` command aggregates the output of each container. When
    the command exits, all containers are stopped. Running `docker-compose up -d`
    starts the containers in the background and leaves them running.

    If there are existing containers for a service, and the service's configuration
    or image was changed after the container's creation, `docker-compose up` picks
    up the changes by stopping and recreating the containers (preserving mounted
    volumes). To prevent Compose from picking up changes, use the `--no-recreate`
    flag.

    If you want to force Compose to stop and recreate all containers, use the
    `--force-recreate` flag.

    Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2710 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 12:12 · PVG 20:12 · LAX 04:12 · JFK 07:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.