Stef
Stef

Reputation: 33

Why does docker-compose launch different image when run with the '-p' flag

We have a setup with our Jenkins server running within a Docker container which I have taken over from a colleague who has left. I am seeing some behaviour which I do not understand and have not been able to work out what is going on from the documentation. My folder structure looks like this:

└── Master
    ├── docker-compose.yml
    └── jenkins-master
        └── Dockerfile

My docker-compose.yaml file looks like this (this is just a snippet of the relevant part):

version: '3'

services:
  master:
    build: ./jenkins-master

I have updated the version of the base Jenkins image in jenkins-master/Dockerfile and then rebuilt using docker-compose build. This succeeds and results in an image called master_master If I run docker images I see this new image as well as a previous image:

REPOSITORY        TAG      IMAGE ID    CREATED          SIZE
master_master     latest   <id1>       16 hours ago     704MB
jenkins_master    latest   <id2>       10 months ago    707MB

As I understand it, the name master_master is as a result of the base folder name (i.e. Master) and the service name of master in the docker-compose.yaml file. I don't know how the existing image ended up with the name jenkins_master. Would the folder name have had to be Jenkins rather than Master, or is there another way that would have resulted in this name?

When I run docker-compose up -d it uses the master_master image to launch a container (called master_master_1).

When I run docker-compose -p jenkins up -d it uses the jenkins_master image to launch a container (called jenkins_master_1).

Apart from the different container names, the resultant running containers are different as I can see that the Jenkins versions are different (as per the change I made in the Dockerfile). I do not change the docker-compose file at all between running these 2 commands and yet different images are run.

The documentation that I have found for specifying the -p (--project-name) flag states:

Sets the project name. This value is prepended along with the service name to the container on start up. For example, if your project name is myapp and it includes two services db and web, then Compose starts containers named myapp_db_1 and myapp_web_1 respectively.

Setting this is optional. If you do not set this, the COMPOSE_PROJECT_NAME defaults to the basename of the project directory.

There is nothing that leads me to believe that the -p flag will result in a different image being run.

So what is going on here? How does docker-compose choose which image to run? Is this happening due to the names of the images master_master vs jenkins_master?

Upvotes: 1

Views: 570

Answers (2)

David Maze
David Maze

Reputation: 158758

If you're going to use the docker-compose -p option, you need to use it with every docker-compose command, including docker-compose build.

If your docker-compose.yml file doesn't specify an image:, Compose constructs an image name from the current project name and the Compose service name. The project name and Docker object metadata are the only way it has to remember anything. So what's happening here is that the plain docker-compose build builds the image for the master service in the master project, but then docker-compose -p jenkins up looks for the master service in the jenkins project, and finds the other image.

docker-compose -p jenkins build
docker-compose -p jenkins up -d

It may or may not be easier to set the COMPOSE_PROJECT_NAME environment variable, possibly putting this in a .env file. In a Jenkins context, I also might consider using Jenkins's Docker integration to build (and push) the image, and only referring to image: in the docker-compose.yml file.

Upvotes: 1

snehal
snehal

Reputation: 478

Add image option in the docker-compose.yml file. It will create the container with a specified docker image.

build: ./jenkins-master
image: dockerimage_name:tag

Upvotes: 0

Related Questions