Docker撰写:如何设置envvariables以在脚本中使用

我正在通过docker运行webdriverIO( https://github.com/hulilabs/webdriverio )testing:

docker-compose run --rm webdriverio wdio 

现在我需要用这个命令(ENV?)来设置一个variables,这个variables可以在testing文件中使用。

 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') }) }) 

我怎样才能做到这一点?


组态

我的wdio.conf.js

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

的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 

首先,您需要将ENVvariables设置为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 

那么你需要在你的应用程序中读取这个variables

 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') }) }) 

另外,在你的Dockerfile你可以把ENVvariables设置为默认值:

 ENV APP_PROFILE=prod