Docker: permission denied while trying to connect to Docker Daemon with local CircleCI build
I've created a workaround for myself.
In the very first step of the config.yml, I run this command:
if [[ $CIRCLE_SHELL_ENV == *"localbuild"* ]]; then
echo "This is a local build. Enabling sudo for docker"
echo sudo > ~/sudo
else
echo "This is not a local build. Disabling sudo for docker"
touch ~/sudo
fi
Afterwards, you can do this:
eval `cat ~/sudo` docker build .
Explanation:
The first snippet checks if the CircleCI-provided environment variable CIRCLE_SHELL_ENV
contains localbuild
. This is only true when running circleci build
on your local machine.
If true, it creates a file called sudo
with contents sudo
in the home directory.
If false, it creates a file called sudo
with NO contents in the home directory.
The second snippet opens the ~/sudo
file, and executes it with the arguments you give afterwards. If the ~/sudo
file contains "sudo
", the command in this example will become sudo docker build .
, if it doesn't contain anything, it will become docker build .
, with a space before it, but that will be ignored.
This way, both the local (circleci build
) builds and remote builds will work.
To iterate on the answer of Jeff Huijsmans,
an alternative version is to use a Bash variable for docker
:
- run:
name: Set up docker
command: |
if [[ $CIRCLE_SHELL_ENV == *"localbuild"* ]]; then
echo "export docker='sudo docker'" >> $BASH_ENV
else
echo "export docker='docker'" >> $BASH_ENV
fi
Then you can use it in your config
- run:
name: Verify docker
command: $docker --version
You can see this in action in my test for my Dotfiles repository
Documentation about environment variables in CircleCi