Bladeren bron

Allow container restarts with file logging (#73101)

Closes #72702. It wasn't possible to restart an Elasticsearch Docker
container when using `ES_LOG_STYLE=file`, and now it is.
Rory Hunter 4 jaren geleden
bovenliggende
commit
577010740e

+ 3 - 2
distribution/docker/src/docker/bin/docker-entrypoint.sh

@@ -63,8 +63,9 @@ if [[ -n "$ES_LOG_STYLE" ]]; then
       # This is the default. Nothing to do.
       ;;
     file)
-      # Overwrite the default config with the stack config
-      mv /usr/share/elasticsearch/config/log4j2.file.properties /usr/share/elasticsearch/config/log4j2.properties
+      # Overwrite the default config with the stack config. Do this as a
+      # copy, not a move, in case the container is restarted.
+      cp -f /usr/share/elasticsearch/config/log4j2.file.properties /usr/share/elasticsearch/config/log4j2.properties
       ;;
     *)
       echo "ERROR: ES_LOG_STYLE set to [$ES_LOG_STYLE]. Expected [console] or [file]" >&2

+ 15 - 0
qa/os/src/test/java/org/elasticsearch/packaging/test/DockerTests.java

@@ -45,6 +45,7 @@ import static org.elasticsearch.packaging.util.Docker.getImageLabels;
 import static org.elasticsearch.packaging.util.Docker.getJson;
 import static org.elasticsearch.packaging.util.Docker.mkDirWithPrivilegeEscalation;
 import static org.elasticsearch.packaging.util.Docker.removeContainer;
+import static org.elasticsearch.packaging.util.Docker.restartContainer;
 import static org.elasticsearch.packaging.util.Docker.rmDirWithPrivilegeEscalation;
 import static org.elasticsearch.packaging.util.Docker.runContainer;
 import static org.elasticsearch.packaging.util.Docker.runContainerExpectingFailure;
@@ -676,6 +677,20 @@ public class DockerTests extends PackagingTestCase {
         assertThat(result.stderr, containsString("ERROR: ES_LOG_STYLE set to [unknown]. Expected [console] or [file]"));
     }
 
+    /**
+     * Check that it when configuring logging to write to disk, the container can be restarted.
+     */
+    public void test124CanRestartContainerWithStackLoggingConfig() throws Exception {
+        runContainer(distribution(), builder().envVars(Map.of("ES_LOG_STYLE", "file")));
+
+        waitForElasticsearch(installation);
+
+        restartContainer();
+
+        // If something went wrong running Elasticsearch the second time, this will fail.
+        waitForElasticsearch(installation);
+    }
+
     /**
      * Check that the Java process running inside the container has the expected UID, GID and username.
      */

+ 7 - 0
qa/os/src/test/java/org/elasticsearch/packaging/util/Docker.java

@@ -612,4 +612,11 @@ public class Docker {
     public static Shell.Result getContainerLogs() {
         return sh.run("docker logs " + containerId);
     }
+
+    /**
+     * Restarts the current docker container.
+     */
+    public static void restartContainer() {
+        sh.run("docker restart " + containerId);
+    }
 }