简体   繁体   English

Docker撰写:如何设置环境变量以在脚本中使用

[英]Docker compose: How to set env variable to use in a script

I'm running a webdriverIO ( https://github.com/hulilabs/webdriverio ) test via docker: 我正在通过docker运行webdriverIO( https://github.com/hulilabs/webdriverio )测试:

docker-compose run --rm webdriverio wdio

Now I need to set a variable with this command (ENV?) which can be used in the test file. 现在,我需要使用此命令(ENV?)设置一个变量,该变量可以在测试文件中使用。

describe('my awesome website', function () {
  it('should do some chai assertions', function () {
    browser.url(url) // <-- I need to set the variable (dev vs. prod)
    browser.getTitle().should.be.equal('Website title')
  })
})

How can I do that? 我怎样才能做到这一点?


Configuration 组态

My wdio.conf.js : 我的wdio.conf.js

exports.config = {
  host: 'hub',
  port: 4444,
  specs: [
    './specs/**/*.js'
  ],
  capabilities: [
    { browserName: 'chrome' },
    { browserName: 'firefox' }
  ]
}

My docker-compose.yml looks like this: 的docker-compose.yml看起来像这样:

version: '2'
services:
    webdriverio:
        image: huli/webdriverio:latest
        depends_on:
            - chrome
            - firefox
            - hub
        environment:
            - HUB_PORT_4444_TCP_ADDR=hub
            - HUB_PORT_4444_TCP_PORT=4444
        volumes:
            - /app:/app

    hub:
        image: selenium/hub
        ports:
            - 4444:4444

    firefox:
        image: selenium/node-firefox
        ports:
            - 5900
        environment:
            - HUB_PORT_4444_TCP_ADDR=hub
            - HUB_PORT_4444_TCP_PORT=4444
        depends_on:
            - hub

    chrome:
        image: selenium/node-chrome
        ports:
            - 5900
        environment:
            - HUB_PORT_4444_TCP_ADDR=hub
            - HUB_PORT_4444_TCP_PORT=4444
        depends_on:
            - hub

First you need to set ENV variable into docker-compose.yml 首先,您需要将ENV变量设置为docker-compose.yml

services:
    webdriverio:
        image: huli/webdriverio:latest
        depends_on:
            - chrome
            - firefox
            - hub
        environment:
            - HUB_PORT_4444_TCP_ADDR=hub
            - HUB_PORT_4444_TCP_PORT=4444
            - APP_PROFILE=dev # <- here new variable
        volumes:
            - /app:/app

Then you need to read this varuiable in your app 然后,您需要在您的应用中阅读此变量

describe('my awesome website', function () {
  it('should do some chai assertions', function () {
    browser.url(process.env.APP_PROFILE)
    browser.getTitle().should.be.equal('Website title')
  })
})

Also, in your Dockerfile you can put ENV variable with default value: 另外,在Dockerfile您可以将ENV变量设置为默认值:

ENV APP_PROFILE=prod

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

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