Migrating Docker Compose encounters yaml structure errors
I was recently migrating servers and ran into a problem. The docker-compose.yml file, which I had been using, encountered an exception after migrating to the new server.
yaml: unmarshal errors:
line 155: mapping key "<<" already defined at line 154
line 192: mapping key "<<" already defined at line 191
The docker-compose.yml file looks like this:
version: "3.8"
x-proxy: &default-proxy
environment:
http_proxy: http://clash:10808
https_proxy: http://clash:10808
depends_on:
- clash
networks:
- proxy
x-timesync: &time
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
nginx:
<<: *default-proxy
<<: *time
This is a configuration for nginx that inserts two configurations, proxy and timezone. It’s been working fine, no problems.
Solution
The only difference between the two sides is the version of docker compose. The version on the old server was 2.4.1
, and the new server has the latest version installed, which is 2.21.0
.
After some searching, the reason was found.
According to the yaml specification, it is not normal for a key to appear multiple times like this. According to the official way of writing it, it should be written as follows:
nginx:
<<: [*default-proxy, *time]
According to the docker compose issue, the yaml parser has been improved in version 2.17 so that it no longer allows this kind of irregularity.
So, all you need to do is standardize the way you write it.