Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables for docker-compose inside of Jenkins

I've the following setup:

Jenkins environment running docker-compose but I need to pass environment variables to the yml file:

sample docker-compose.yml

version: '2'

services:  
  logon_server:
    build: .
    image: my_server:0.0.1
    ports:
      - "9200:9200"
    command: ${DOCKER_CMD_EXEC}
    networks:
      - my_ntwrk

networks:
  my_ntwrk:

When I run the following script from a shell prompt on my mac, I can pass different commands to be executed and everything works:

exec-tests.sh

#!/bin/bash

chmod 755 docker/exec-*.sh

# Setup env variables
export DOCKER_CMD_EXEC=/ci_e2e.sh

# Optional: record current versions
docker -v && docker-compose -v

echo `whoami` 

echo `sudo -E -u admin printenv`

# Build, deploy and run E2E test cases
sudo -E -u admin docker-compose up --no-color --abort-on-container-exit

When I run the script inside of my jenkins job, under 'Build Environment' -> 'Execute Shell' -> 'Command', here is an image of what I'm talking about:

Example of shell command

I get the following error:

The DOCKER_CMD_EXEC variable is not set. Defaulting to a blank string

I've tried running this as root and as the default user 'admin' which is what jenkins is run as. I've tried to make the environment variables inheritable, tried printing the env's and everything looks good.

I can re-produce the error on my mac by just unsetting the DOCKER_CMD_EXEC but I cant figure out why this happening in jenkins!

Any help would be appreciated!

Thanks.

J

like image 522
user1859465 Avatar asked Oct 18 '22 11:10

user1859465


1 Answers

Here is my solution:

Docker Script:

#!/bin/bash
export COMPOSE_HTTP_TIMEOUT=120
export DOCKER_CMD_EXEC=/app/docker/ci_run.sh
export DOCKER_LINKED_CMD=/app/docker/ci_run.sh
export DOCKER_NODE_ENV=staging
export DOCKER_IMAGE_TAG=${DOCKER_IMAGE_TAG:=staging}
export DOCKER_RELOAD_DB=${PARAM_RELOAD_DB:=true}
export DOCKER_HTTP_PROXY=''

# Optional: record current versions
docker -v && docker-compose -v

# Startup container(s)
cat compose.yml | envsubst | docker-compose -f - up

compose.yml file:

version: '2'

services:   
  app:
    build: .
    image: ${DOCKER_IMAGE_TAG}
    environment:
      - MONGODB_HOST=mongodb
      - NODE_ENV=${DOCKER_NODE_ENV}
      - HTTP_PROXY=${DOCKER_HTTP_PROXY}
    ports:
      - "8000:8000"
    command: ${DOCKER_CMD_EXEC}
    volumes:
      - /dev/shm:/dev/shm
    networks:
      - server-ntwk

networks:
  server-ntwk:

The bash script and compose file is in the same directory:

./docker/exec-build.sh
like image 66
user1859465 Avatar answered Oct 21 '22 04:10

user1859465