The docs for 3.7 say that:
Starting with the 3.7 format (for the 3.x series) and 2.4 format (for the 2.x series), extension fields are also allowed at the root of service, volume, network, config and secret definitions.
So I thought I could do this:
version: '3.7'
services:
x-logging: &default-logging # within services, rather than document root
driver: json-file
web:
image: myapp/web:latest
logging: *default-logging
db:
image: mysql:latest
logging: *default-logging
But when I try something like this, I get the error:
Top-level object must be a mapping
Related
While running docker-compose up getting the below error:
ERROR: The Compose file './docker-compose.yml' is invalid because:
Invalid top-level property "wordpress". Valid top-level sections for this Compose file are: version, services, networks, volumes, 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/
Infra Details:
Docker
Docker version 23.0.0, build e92dd87
docker-compose (installed using apt-get install docker-compose)
docker-compose version 1.25.0, build unknown
docker-compose.yml
version: '3'
wordpress:
image: wordpress
links:
- db:mysql
db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: '<a secure password>'
On the top level you should have services and only then the services themselves. Something like this:
version: '3'
services:
wordpress:
image: wordpress
links:
- db:mysql
db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: '<a secure password>'
I am trying to understand how I access containers between each other through their container name. Specifically when using a pgadmin container and connecting to a postgresql container through dns.
In docker-compose V3 , I cannot link them, nor does networks: seem to be available either.
The main reason to need this is when the containers spin up they don't have a static IP address, so in pgadmin I can't connect to the postgresql DB using the same IP every time , so a dns name would work better (ie: the container name).
Can we do this with docker-compose or at least set a static ip address for a specific container?
I have tried creating a user defined network:
networks:
backed:
and then using it in the service:
app:
networks:
- backend
This causes a docker-compose error regarding an invalid option of "networks" in the app.
docker-compose.yml
version: "0.1"
services:
devapi:
container_name: devapi
restart: always
build: .
ports:
- "3000:3000"
api-postgres-pgadmin:
container_name: api-postgres-pgadmin
image: dpage/pgadmin4:latest
ports:
- "5050:80"
environment:
- PGADMIN_DEFAULT_EMAIL=stuff#stuff.com
- PGADMIN_DEFAULT_PASSWORD=12345
api-postgres:
container_name: api-postgres
image: postgres:10
volumes:
- ./data:/data/db
ports:
- "15432:5432"
environment:
- POSTGRES_PASSWORD=12345
Actually, I spot one immediate problem:
version: "0.1"
Why are you doing this? The current version of the compose file format is 3.x. E.g:
version: "3"
See e.g. the Compose file version 3 reference.
The version determines which feature are available. It's entirely possible that by setting version: "0.1" you are explicitly disabling support for the networks parameter. You'll note that the reference shows examples using the networks attribute.
As an aside, unless there is a particular reason you ened it, I would drop the use of the container_name in your compose file, since this makes it impossible to run multiple instances of the same compose file on your host.
networks are available from docker-compose version 3 but you are using version:"0.1" in your docker-compose file.
Change the version: "0.1" to version: "3" in docker-compose.yml.
I have multiple services running each a mysql database.
I'm using docker-compose to deploy my apps in docker.
I need that all these services are in the same network.
I use different docker-compose file for each service.
My issue is:
How can I prevent to container from being named the same (in docker dns) if they have the same name in different compose files.
Exemple:
service1.yml
version: '2'
services:
mysql:
image: "mysql"
network:
- anetwork
[...]
service2.yml
version: '2'
services:
mysql:
image: "mysql"
network:
- anetwork
[...]
This two files are in separate folders. After launching all docker-compose up. The containers appears with docker ps. On named service1_mysql_1 and the other service2_mysql_1.
But when I ping mysql dns name inside the network anetwork, both responds...
How should I fix this ? I am using bad practices ?
I have already tried:
- Changing the name in each compose files
I think that the options are either:
Change the name to be different in each compose file, which I understand you have already tried, e.g:
version: '2'
services:
mysql1:
image: "mysql"
[...]
Use a fully-qualified name when connecting to each container, i.e. service1_mysql_1 and service2_mysql_1 in this case. Not ideal as it is generated but this name can be fixed by setting it explicitly using the container_name option:
version: '2'
services:
mysql:
image: "mysql"
container_name: my-service1-mysql
[...]
Then a container that needs to connect to this database on the anetwork can connect using the hostname my-service1-mysql
I have the following docker-compose file content:
version: '3.4'
services:
local-app:
build: ./app/
command: node app
ports:
- '7001:7001'
links:
- search-svc
networks:
docker_app-network:
external: true
external_links:
-search-svc
Basically what I 'm trying to do is to link the ' local-app ' container with another already running container the ' search-svc '. By running the docker compose I get the following error:
The Compose file './docker-compose.yaml' is invalid because:
Invalid top-level property "external_links". 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.
I have read the documentation but I can't find any solution to my problem.
Can anyone suggest anything that might help?
Thanks in advance
Yaml files are space sensitive. You tried to define external_links at the top level of the file rather than as part of a service. This should by syntactically correct:
version: '3.4'
services:
local-app:
build: ./app/
command: node app
ports:
- '7001:7001'
links:
- search-svc
external_links:
- search-svc
networks:
docker_app-network:
external: true
That said, linking is deprecated in docker, it is preferred to use a common network (excluding the default bridge network named bridge) and then use the integrated DNS server for service discovery. It looks like you have defined your common network but didn't use it. This would place your service on that network and rely on DNS:
version: '3.4'
services:
local-app:
build: ./app/
command: node app
ports:
- '7001:7001'
networks:
- docker_app-network
networks:
docker_app-network:
external: true
I am trying to specify the max file size for json-file in docker-compose.yml, like this,
log-opt:
max-size=50m
but when I tried to docker-compose up, it threw me an error,
ERROR: In file './docker-compose.yml', service 'log-opt' must be a mapping not a string.
How to fix it?
ps. I am using docker 1.11.2
Your yaml syntax isn't quite correct. The documentation says it should be options not log-opt (https://docs.docker.com/compose/compose-file/#logging). Try this?
services:
service_name:
logging:
driver: "json-file"
options:
max-size: "50m"
You should define logging section in each one of your services not directly in root of docker-compose.
max-size store log files until they reach a max-size of VALUE (eg: "2048m").
Example docker-compose file config with max-size.
version: '3.2'
services:
logstash:
image: docker.elastic.co/logstash/logstash:7.8.0
command: --config.reload.automatic
user: savio:savio
restart: unless-stopped
logging:
driver: "json-file"
options:
max-size: "2048m"
ports:
- "9600:9600"
If you are making a change for an already running container, you need to stop and remove the container and start again.
check log settings
]# docker container inspect -f '{{.HostConfig.LogConfig}}' <ContainerName>
For more detail: link