How to fix GitHub Actions error docker build requires exactly 1 argument.
I’ve been working on migrating my the remote jobs platform I built in the past months from weremote.ro to weremote.eu.
The platform’s backend is a GraphQL API built over Express.js which runs inside a Docker container. I was initially building the image myself, whenever I pushed some changes worth deploying to production, but I very quickly switched to GitHub Actions once I learned about this feature.
With this migration to .eu, I also added some features and most importantly, I moved a lot of config into env vars. So before I could actually deploy, I had to update main.workflow
which is used by GitHub Actions to define the steps of the build process.
I went on and added the env vars, the secrets, and everything looked right. Except for the fact that I was getting this error when the build was running:
# [...] docker pull logs over here
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
### FAILED Build Docker image 13:23:26Z (41.66s)
I thought it was because I had a lot of build args that I was passing at build time.
Basically this:
args = "build --build-arg db_host=${DATABASE_HOST} --build-arg db_user=${DATABASE_USER} --build-arg db_pass=${DATABASE_PASSWORD} --build-arg db_name=${DATABASE_NAME} --build-arg database_authentication_source=${DATABASE_AUTHENTICATION_SOURCE} --build-arg cookie_signing_secret=${COOKIE_SIGNING_SECRET} --build-arg jwt_sign_key=${JWT_SIGN_KEY} --build-arg netlify_build_hook_url=${NETLIFY_BUILD_HOOK_URL} --build-arg mailgun_api_key=${MAILGUN_API_KEY} --build-arg bt_environment=${BT_ENVIRONMENT} --build-arg bt_merchant_id=${BT_MERCHANT_ID} --build-arg bt_public_key=${BT_PUBLIC_KEY} --build-arg bt_private_key=${BT_PRIVATE_KEY} --build-arg bt_merchant_account_id=${BT_MERCHANT_ACCOUNT_ID} -t ${IMAGE_NAME} ."
But as it turns out, my brain works better at 12:30 AM than it works at 2:00 PM.
While I was looking at the values of my env vars, I realised that two variables were storing strings with spaces — bad move.
So here’s an example
$ SOME_ENV_VAR="This is a nice sentence!"
$ docker build --build-arg some_arg=${SOME_ENV_VAR} .
If this happens (and this is what happens when GitHub Actions set your env vars and secrets, you’ll get this error:
[...]
"docker build" requires exactly 1 argument.
See 'docker build --help'.
[...]
That’s because when interpreted the result is your sentence, but without the quotes:
echo $SOME_ENV_VAR
This is a nice sentence!
So when you run your docker build
command, and pass the variable as a build argument, it looks like this:
docker build --build-arg some_arg=This is a nice sentence! .
That’s a lot of arguments for docker build
which it cannot identify.
And that, folks, is why you get weird errors when running docker build
in GitHub Actions.
The solution
- You don’t use phrases in your environment variables
- You add quotes around those variables
docker build --build-arg some_arg='${SOME_ENV_VAR}' .
- Use single quotes because the whole “build” command is wrapped around with double quotes
Out!