Is there a way to set the stack name when using docker-compose?
Currently it takes the folder name (which is a horrible area) and that leads to some confusion.
For example, we have several projects that have a database folder that contains the database stack. When running these on a single host, we have now several database stacks.
There are several ways to do it:
1. Using --project-name (or -p) option when calling docker-compose:
docker-compose -p "my-app" up
Caution: -p "my-app" must come before up.
2. COMPOSE_PROJECT_NAME environment variable:
export COMPOSE_PROJECT_NAME=my-app
docker-compose up
3. COMPOSE_PROJECT_NAME in .env file
Create a file named .env in the project root and set the COMPOSE_PROJECT_NAME environment variable there:
COMPOSE_PROJECT_NAME=some_app
and then:
docker-compose up
The .env file is read from the folder where the docker-compose command
is executed, NOT from the folder of the docker-compose.yml file.
Assuming the following project structure:
root
- database
- docker-compose.yml
- .env
- app
- docker-compose.yml
- .env
- ...
The command below, executed in the root folder, will not give desired effects:
# Stack name 'database' (the folder name). The root/database/.env file not read.
docker-compose -f ./database/docker-compose.yml up
The docker-compose command needs to be executed in the root/database folder:
# Stack name from the root/database/.env
cd database
docker-compose up
If you use option 2 or 3, the project name is applied to all docker-compose commands, as if it were specified with the -p option.
It looks like the project name can be set using the name top level element in the docker-compose file as described here: https://docs.docker.com/compose/compose-file/#name-top-level-element
Top-level name property is defined by the specification as project name to be used if user doesn’t set one explicitly. Compose implementations MUST offer a way for user to override this name, and SHOULD define a mechanism to compute a default project name, to be used if the top-level name element is not set.
This doesn't seem to be well documented anywhere else, and I didn't see a reference to it in the spec changelog, so I'm not sure if this has always been part of the spec, or if compose just didn't support it until recently.
Example
My current directory (pwd):
/Users/brahmlower/development/compose-test
My compose file (cat docker-compose.yml):
version: "3.8"
name: my-project
services:
hello-world:
image: "hello-world:latest"
Without the name property, we would expect to see the stack started with the prefix compose-test since that's the name of the directory. However when I bring the stack up, we see compose names the stack my-project as expected.
The stack output (docker compose up -d):
[+] Running 2/2
⠿ Network my-project_default Created
⠿ Container my-project-hello-world-1 Started
Versions
In case it's helpful, my docker versions are:
compose (docker compose version):
Docker Compose version v2.10.2
docker (docker version):
Client:
Cloud integration: v1.0.29
Version: 20.10.17
API version: 1.41
Go version: go1.17.11
Git commit: 100c701
Built: Mon Jun 6 23:04:45 2022
OS/Arch: darwin/arm64
Context: default
Experimental: true
Server: Docker Desktop 4.12.0 (85629)
Engine:
Version: 20.10.17
API version: 1.41 (minimum version 1.12)
Go version: go1.17.11
Git commit: a89b842
Built: Mon Jun 6 23:01:01 2022
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: 1.6.8
GitCommit: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Related
Versions of Docker tooling:
Docker Compose version v2.6.0
Docker Engine Version 20.10.17 (client)
Docker Engine Version 20.10.17 (server)
I've got a compose file that starts a swarm with networks and secrets, so I'm using Docker Compose File Version 3.9.
One of my services is a GPU resource, so I added this based on current docs:
version: "3.9"
services:
my-app:
image: my-app:latest
deploy:
resources:
reservations:
devices:
- capabilities: [gpu]
count: all
. . .
My IDE is complaining with "expected scalar value" indicating that it doesn't recognize the key or value of capabilities and count and when I run the command:
docker stack deploy --compose-file docker-compose.yml my-stack
Docker throws an error:
services.prosit-app.deploy.resources.reservations Additional property devices is not allowed
Based on the versions of my Docker tools and the schema I'm using, this should all work (I think). What am missing?
I've tried various file versions and I can get the IDE warning to go away but then Docker can't support the file version. I've tried with and without quotes ["gpu"], different options, etc, to no avail.
This question was asked already (my bad) and the answer was missed by me and others:
Docker Compose returns error about property devices when trying to enable GPU
Reference:
https://docs.docker.com/compose/compose-file/compose-file-v3/#devices
I have docker compose yml file in my project file.
When try to run the below commend i get the below error.
docker-compose up
I get the below error message
invalid variable name "docker-compose.yml"
I have installed and uninstalled docker multiple times.
my Docker version - Docker version 20.10.8, build 3967b7d
my docker-compose version - Docker Compose version v2.0.0-rc.3
Check that your environment variables or .env file does not contain multiline variables (e.g. SSH keys). After flattening them, error should disappear. Source: https://github.com/Azure/aci-deploy/issues/29
I'm trying to deploy a service which uses docker-compose files and I've been seeing the following error:
Invalid top-level property "x-...". Valid top-level sections for this Compose file are: secrets, version, volumes, services, configs, networks, and extensions starting with "x-".
You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions ...
I reproduced the error using this simple docker-compose.yml file:
version: "3.5"
x-secrets: &secrets
secrets:
- foo
services:
a:
<<: *secrets
image: a-image
secrets:
foo:
external: true
The command
docker-compose -f docker-compose.yml up
Gives the following output:
ERROR: The Compose file './docker-compose.yml' is invalid because:
Invalid top-level property "x-secrets". Valid top-level sections for this Compose file are: secrets, version, volumes, services, configs, networks, and extensions starting with "x-".
You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/
Versions:
docker --version
Docker version 19.03.6, build 369ce74a3c
docker-compose --version
docker-compose version 1.17.1, build unknown
Running on Ubuntu 18.04.
Thanks for any input!
Resolved this by upgrading docker-compose to
docker-compose version 1.27.4, build 40524192
This means that either the format of your docker-compose.yml file is not correct or your version of docker-compose is too old.
It's always a good practice to upgrade to the latest version of docker-compose - you can do it like this:
Get the latest version
VERSION=$(curl https://api.github.com/repos/docker/compose/releases/latest | jq .name -r)
Install it
sudo curl -L "https://github.com/docker/compose/releases/download/${VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Give system permissions
sudo chmod +x /usr/local/bin/docker-compose
Logout, then login again
Now that you have the latest version, check if the format of your docker-compose.yml file is correct by comparing it to the examples in the official documentation (again, check the format for the latest version). Note that small things like single- vs double-space and different special characters matter a lot!
i'm new to docker. I run docker "natively" from a Windows server 2016 with a Windows container, there is no intermediate VM (no docker machine) in between and no docker toolbox, so the "host" is the actual Windows Server that I run docker on.
Docker version:
PS C:> docker version
Client:
Version: 17.03.1-ee-3
API version: 1.27
Go version: go1.7.5
Git commit: 3fcee33
Built: Thu Mar 30 19:31:22 2017
OS/Arch: windows/amd64
Server:
Version: 17.03.1-ee-3
API version: 1.27 (minimum version 1.24)
Go version: go1.7.5
Git commit: 3fcee33
Built: Thu Mar 30 19:31:22 2017
OS/Arch: windows/amd64
Experimental: false
PS C:>
i pulled the image from docker hub. I need to replace the files inside the docker image while running and commit changes to the image.
Lets say i have Sample.java and datafile.properties inside the docker image which i pulled from docker hub.
i want to replace that with Hello.java and data.properties[ i pulled these files from github]
how would i do that in an automated way? Any advise and some examples on this would he helpful. Thanks in advance.
The best way to build an image automated, is to use a Dockerfile. Some information can be found in the documentation, for example; https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/
If you have your Hello.java and datafile.properties in a directory, create a Dockerfile in the same directory, e.g.;
FROM the-base-image-on-docker-hub
RUN rm /path/to/Sample.java
COPY ./Hello.java /path/to/
COPY ./datafile.properties /path/to/
You can then build your image, and "tag" it as myimage:latest with;
docker image build -t myimage:latest .
(the period at the end (.) indicates; use the current directory as "build context" - the build context is uploaded to the docker daemon, and everything in it will be accessible to add to your docker image using the COPY or ADD Dockerfile instructions)
This is a very naive example, just to illustrate the concept; I suggest reading the documentation, to understand the concept, and searching for more examples.
docker-compose --version
docker-compose version 1.11.1, build 7c5d5e4
I have secret 'my_secret_data' added to my swarm cluster:
The start of my compose file looks like:
version: "3.1"
secrets:
my_secret_data:
external: true
services:
master:
image: jenkins-master
secrets:
- my_secret_data
ports:
- "8080:8080"
- "50000:50000"
'docker stack deploy' continually gives the error:
secrets Additional property secrets is not allowed
I have followed how do you manage secret values with docker-compose v3.1? to the letter as far as I can tell and have the correct versions installed but keep getting the above error. Any help greatly appreciated.
Change compose file version to latest version.
In short, version '3' is not resolved to the latest '3.x' version. Find what the latest version is here https://docs.docker.com/compose/compose-file/#compose-and-docker-compatibility-matrix
The "Additional property secrets is not allowed" error can be caused either by:
running Docker Engine < 1.13.1, or
using a compose file version number < '3.1' in a docker-compose file such as docker-compose.yml or docker-cloud.yml
If you are experiencing this problem confirm that both are correct.
This also applies to other Docker interfaces and tools.
For examples, in Portainer, yml with secrets lines pasted into the Create Stack dialog should begin with the line version: '3.1' or you will encounter the same error -- even with an up-to-date Docker Engine 1.13.1+.
In my case, Service: had an extra tab prior. Moment I removed tab prior to it, it worked.