I am using docker
and docker compose
for my python web application. Everything was running smooth, but few days back, I am unable to build and up
custom and standard images.
- I reinstalled the version I was using but failed (docker-compose) # from ubuntu repository using apt
- I updated to the latest version but still failed (docker compose) # offical site using apt
I have added the user to docker group.
I checked docker compose config
working correctly as values properly substituted.
Below is my code:
# File: .env
# General settings
PROJECT_NAME=titanic
# File: docker-compose.yml
version: "3.7"
services:
redis:
container_name: ${PROJECT_NAME}_redis_container
build:
context: .
dockerfile: redis/Dockerfile
args:
- REDIS_IMG=${REDIS_IMG}
image: ${PROJECT_NAME}_redis_image
# restart: always
ports:
- "6379:6379"
networks:
- internal_network
user: redis
volumes:
- redis_volume:/data
volumes:
redis_volume:
name: ${PROJECT_NAME}_redis_volume
I am getting following error as below:
invalid tag ""titanic"_redis_image": invalid reference format
I am using MakeFile
to create my project, I run just make
.PHONY: all
all: dc-build dc-up
@echo "Process finished..."
.PHONY: dc-build
dc-build:
@echo "Building all images..."
docker compose build
.PHONY: dc-up
dc-up:
@echo "Launching all services..."
docker compose up
More strange is that if I use sudo
infornt of docker compose command, it runs successfully with both
my previous docker and docker-compose version and also with the latest one.
Added: I am needing sudo
to run from/Via MakeFile
. If I run(docker-compose build or up –build) directly on terminal it works.
I am unable to figure out the cause, what I am missing?
2
Answers
Given the error
invalid tag ""titanic"_redis_image": invalid reference format
Images are identified by image references, which can be a repository name or a image hash. So it is telling you that the string you are providing to
image:
is wrong.That looks like this:
So by doing a reverse substitution, we can see that
PROJECT_NAME
is set to""titanic""
which does not a valid path, or image repository reference, make.Don’t double escape your strings. If this double escaped string has not somehow polluted your environment, then it seems a bug has been introduced in compose, and this is worth a bug report. But the fact it works with sudo implies it works fine when given a clean environment.
Other comments and answers are suggesting a problem around the
$PROJECT_NAME
environment variable. Your Compose file uses it in three places, but you don’t need any of these. If you just delete these three lines you can avoid the substitution and quoting problems you’re describing.You’re asking Compose to do three things on your behalf here: build an image, create a named volume, and start a container. For all three of these things, Compose can pick its own name, and you don’t need to override these in most cases. If the variable substitution here is causing a problem, just deleting the lines that use it should solve it.
It turns out that the default Compose names are very similar to what you’re showing in any case. Compose has its own notion of a "project name" –
$COMPOSE_PROJECT_NAME
, defaulting to the base name of the current directory – and when Compose generates names it includes this project name at the start of its names. If the current directory is namedtitanic
, then on its own Compose will create an image namedtitanic_redis
, a volume namedtitanic_redis_volume
, and a container namedtitanic_redis_1
. That’s pretty close to what you are trying to specify by hand.