如何在Dockerfile中添加GITHUB_KEY与GITHUB_SECRET?命令执行报错求助
Hey there! Let's break down what's going wrong with your CMD command and get it working correctly.
First, let's spot the issues in your original line:
- You’re using Chinese quotation marks (
“”instead of standard English""), which the shell doesn’t recognize as valid quote delimiters—this is the immediate cause of the "Unterminated quoted string" error. - The
CMD ["bash", ...]exec format doesn’t support inline environment variable assignments likeGITHUB_KEY="..."directly in the argument list. Exec mode runs the command directly without a shell, so shell-specific syntax like variable assignment won’t be parsed here.
Solution 1: Use Shell Format for CMD
The simplest fix is to switch to the shell format of CMD (no square brackets), which lets you use shell syntax for environment variables:
CMD GITHUB_KEY="######" GITHUB_SECRET="##########" ./script/server
This runs the command through /bin/sh, which handles the variable assignments correctly. Just make sure to stick with English double quotes for your secrets.
Solution 2: Use ENV + Exec Format (Recommended for PID 1 Behavior)
If you prefer using the exec format (better practice because it avoids extra shell processes and lets your script run as PID 1), set the environment variables explicitly first with ENV, then run your command:
ENV GITHUB_KEY="######" ENV GITHUB_SECRET="##########" CMD ["./script/server"]
This keeps your environment variables clearly defined and makes your Dockerfile more readable.
Solution 3: Exec Format with Shell Wrapper
If you need to set variables only for the specific command (not globally in the container), wrap the command in sh -c within the exec format:
CMD ["sh", "-c", "GITHUB_KEY='######' GITHUB_SECRET='##########' ./script/server"]
Here, we use single quotes inside the command string to avoid conflicting with the outer double quotes, and sh -c tells the shell to execute the entire command line.
Any of these three approaches will fix the syntax error and get your container running properly.
内容的提问来源于stack exchange,提问作者Zero

