Docker Compose: Updating Image for adrs-ui Service
This Docker Compose file defines a multi-container application with two services: 'adrs-ui' and 'adrs-ui_test'. Both services use the same image: '192.168.2.78:5000/dev/ly-mssp-auth-uiaaaa:2.2'.
The goal is to update the image value for the 'adrs-ui' service to 'TEST'.
x-deploy:
replicas: 1
restart_policy:
condition: on-failure
services:
'adrs-ui':
image: '192.168.2.78:5000/dev/ly-mssp-auth-uiaaaa:2.2'
init: true
ports:
- '15111:80'
depends_on:
- api
environment:
spring.cloud.inetutils.preferred-networks: '10.10.0'
APIGATEWAY: '192.168.35.55:1101'
GATEWAY: '192.168.35.55:1101'
Cas_Enable: 'false'
DOCURL: '192.168.35.210:8888'
networks:
- portainer_agent_network
deploy:
placement:
constraints:
- node.hostname == p1
'adrs-ui_test':
image: '192.168.2.78:5000/dev/ly-mssp-auth-uiaaaa:2.2'
init: true
ports:
- '15121:80'
depends_on:
- api
environment:
spring.cloud.inetutils.preferred-networks: '10.10.0'
APIGATEWAY: '192.168.35.55:1101'
GATEWAY: '192.168.35.55:1101'
Cas_Enable: 'false'
DOCURL: '192.168.35.210:8888'
networks:
- portainer_agent_network
deploy:
placement:
constraints:
- node.hostname == p1
networks:
portainer_agent_network:
external: true
You can use a Java library like SnakeYAML to parse and update the YAML file. Here's a code snippet demonstrating this:
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Representer;
import org.yaml.snakeyaml.resolver.Resolver;
import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;
public class UpdateYaml {
public static void main(String[] args) throws IOException {
String filename = "your-yaml-file.yaml";
String hostnameToUpdate = "adrs-ui";
String newImageValue = "TEST";
// 读取YAML文件
InputStream inputStream = new FileInputStream(new File(filename));
Yaml yaml = new Yaml();
Map<String, Object> data = yaml.load(inputStream);
// 更新hostname为adrs-ui的image值
updateImageValue(data, hostnameToUpdate, newImageValue);
// 将更新后的数据写入到新的YAML文件中
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml updatedYaml = new Yaml(new Representer(), options);
FileWriter writer = new FileWriter(new File("updated-yaml-file.yaml"));
updatedYaml.dump(data, writer);
writer.close();
}
private static void updateImageValue(Map<String, Object> data, String hostname, String newImageValue) {
if (data.containsKey("services")) {
Map<String, Object> services = (Map<String, Object>) data.get("services");
for (Map.Entry<String, Object> entry : services.entrySet()) {
Map<String, Object> service = (Map<String, Object>) entry.getValue();
if (service.containsKey("image") && service.containsKey("depends_on")) {
String serviceName = entry.getKey();
Map<String, Object> dependsOn = (Map<String, Object>) service.get("depends_on");
if (dependsOn.containsKey(hostname)) {
service.put("image", newImageValue);
dependsOn.put(hostname, newImageValue);
}
}
}
}
}
}
Remember to replace "your-yaml-file.yaml" with the actual path to your Docker Compose file. This code will create a new file called "updated-yaml-file.yaml" with the updated image value for the 'adrs-ui' service.
原文地址: https://www.cveoy.top/t/topic/p3Qq 著作权归作者所有。请勿转载和采集!