0

I have a very simple Node.js app that takes in a location as a command line argument (const location = process.argv[2];) and logs the weather of that location to the console. I'm trying to run it in a docker container just to get practice with docker.

Here is the Dockerfile:

FROM node:10

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

ENV location=San\ Diego

CMD ["sh", "-c", "node app.js ${location}"]

The problem is, I can't pass in an environment variable that is more than one word. For example, San Diego gets run as just San. This is the case with both the default environment variable in the Dockerfile and any environment variables I pass it when I run the image.

How can I make it so that I can pass in something like Los Angeles and have the app fetch the data for Los Angeles, for both the ENV variable in the Dockerfile, as well as any environment variable passed in as an arg when I run the image?

5
  • ENV location="San Diego" work? Commented Jan 17, 2020 at 21:45
  • @Geuis No, that doesn't work. I tried it. Commented Jan 17, 2020 at 21:47
  • Bit rusty on this, but I don't think you need to pass location in via node app.js ${location}". You should be able to access the envvar in app.js via process.env Commented Jan 17, 2020 at 21:52
  • Does this answer your question? Declare env variable which value include space for docker/docker-compose Commented Jan 17, 2020 at 22:02
  • @mikeb No. But @Geuis got me on the right track to find a workaround. I just added an if statement at the beginning of the app to check if process.env.location exists, then set location to that variable, otherwise, set location to process.argv[2] so that it can also run locally. Commented Jan 17, 2020 at 22:07

1 Answer 1

1

In your shell command:

ENV location=San\ Diego
CMD ["sh", "-c", "node app.js ${location}"]

You're not quoting the variable expansion, so it ultimately runs

sh -c 'node app.js San Diego'

which expands to two words. You need to quote that in your command. (Conversely, you can ask Docker to supply the sh -c wrapper for you.)

ENV location=San\ Diego
CMD node app.js "$location"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.