3

I'm trying to get the variable from the command line using:

 sudo docker-compose -f docker-compose-fooname.yml run -e BLABLA=hello someservicename

My file looks like this:

version: '3'

services:
  someservicename:
    environment:
      - BLABLA
    image: docker.websitename.com/image-name:latest
    volumes:
      - /var/www/image-name
    command: ["npm", "run", BLABLA]

All of this is so that I can run a script defined by what I use as BLABLA in the command line, I've tried going with official documentation.

Tried several options including:

sudo COMPOSE_OPTIONS="-e BLABLA=hello" docker-compose -f docker-compose-fooname.yml run someservicename

UPDATE: I have to mention that as it is, I always get:

WARNING: The FAKE_SERVER_MODE variable is not set. Defaulting to a blank string.

Even when I just run the following command (be it remove, stop..):

  sudo docker-compose -f docker-compose-fooname.yml stop someservicename

For the record: I'm pulling the image first, I never build it but my CI/CD tool does (gitlab), does this affect it?

I'm using docker-compose version 1.18, docker version 18.06.1-ce, Ubuntu 16.04

sab
  • 1,454
  • 1
  • 14
  • 26
  • what are you trying to achieve? Where do you want to use the environment variable? – Ntwobike Oct 30 '18 at 17:13
  • Hello @Ntwobike, I want to use it for my "command" line as I stated, so I can control which script gets executed inside my docker container (start; dev-start, test, foo-script.. etc) – sab Oct 31 '18 at 09:02

1 Answers1

5

That docker-compose.yml syntax doesn't work the way you expect. If you write:

command: ["npm", "run", BLABLA]

A YAML parser will turn that into a list of three strings npm, run, and BLABLA, and when Docker Compose sees that list it will try to run literally that exact command, without running a shell to try to interpret anything.

If you set it to a string, Docker will run a shell over it, and that shell will expand the environment variable; try

command: "npm run $BLABLA"

That having been said, this is a little bit odd use of Docker Compose. As the services: key implies the more usual use case is to launch some set of long-running services with docker-compose up; you might npm run start or some such as a service but you wouldn't typically have a totally parametrizable block with no default.

I might make the docker-compose.yml just say

version: '3'
services:
  someservicename:
    image: docker.websitename.com/image-name:latest
    command: ["npm", "run", "start"]

and if I did actually need to run something else, run

docker-compose run --rm someservicename npm run somethingelse

(or just use my local ./node_modules/.bin/somethingelse and not involve Docker at all)

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Hello @David Maze, thanks for your time, well I actually think that's the way to go (two different services) but as it is I'm forced to do it that way because being the purpose that if we had let's say 5 different starting scripts we don't have 5 services, I see the present need only for 2 but here I'm (those are the specs I need to accomplish), btw I tried running it with your suggestion : command: "npm run $BLABLA" but it didn't work, I had the same output. – sab Oct 31 '18 at 09:04
  • The last suggestion does work like a charm though "npm run somethingelse" after the service, thanks a lot, I'm just not happy that if at the end I'd need to use an environment variable passed through the command in the terminal it still doesn't work so I can't close the question yet but your answer is definitely useful and as I've seen it may help a lot of people (including me) with the same command problem for now, thanks a lot for your time. – sab Oct 31 '18 at 09:15
  • 1
    Update, as of 9th Nov, as Docker won't expand the environment variable in a string in the "command" line, the only solution would be using @David Maze 2th suggestion. – sab Nov 09 '18 at 09:39