I'm new to Makefiles, I'm trying to use one to help me automate the build of my docker images.
I'm struggling to write, what feels like, a simple recipe that does the following:
- Check to see if there are running containers (
docker ps -a -q
) - If running containers exist remove them (
docker rm $(docker ps -a -q)
)
This is what I've manage to cobble together so far through various Google searches:
DOCKER_CONTAINER_LIST := $(shell docker ps -a -q)
clean-docker:
if [[ ! $(DOCKER_CONTAINER_LIST) == "" ]];then \
docker rm $$(docker ps -a -q) \
fi
Which yields the following output:
if [[ de6e694da4f9 == "" ]];then \
/bin/sh: -c: line 0: conditional binary operator expected
/bin/sh: -c: line 0: syntax error near 'de6e694da4f9'
/bin/sh: -c: line 0: 'if [[ de6e694da4f9 == "" ]];then \ '
make: *** [clean-docker] Error 2
Would be great if someone could show me how to achieve this please
UPDATE
Thank @I0b0 for your answer, I think I'm almost there now. The Makefile
now looks like this:
DOCKER_CONTAINER_LIST := $(shell docker ps -a -q)
clean-docker:
if [ -n "$(DOCKER_CONTAINER_LIST)" ] \
then \
docker rm "$(DOCKER_CONTAINER_LIST)" \
fi
But I get the following error when running it:
if [ -n "6778d35e5c95" ] \
then \
docker rm "6778d35e5c95" \
fi
/usr/bin/sh: -c: line 4: syntax error: unexpected end of file
make: *** [clean-docker] Error 1
I had a look at this answer regarding line endings, but that made no difference. I am using Makefile
on both Mac and Windows.