feat(docker): centralize Docker config and add private registry authentication (#549)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-21 08:45:37 -05:00
1 parent c8057785eb
commit 4f0150870a
14 files changed
+410 -18

No files matched your search

@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.config;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
import java.util.List;
import java.util.Optional;
@ConfigMapping(prefix = "floci")
@@ -543,9 +544,6 @@ public interface EmulatorConfig {
@WithDefault("3")
int defaultTimeoutSeconds();
@WithDefault("unix:///var/run/docker.sock")
String dockerHost();
Optional<String> dockerHostOverride();
@WithDefault("9200")
@@ -665,5 +663,31 @@ public interface EmulatorConfig {
*/
@WithDefault("3")
String logMaxFile();
/** Unix socket or TCP URL for the Docker daemon (e.g. unix:///var/run/docker.sock). */
@WithDefault("unix:///var/run/docker.sock")
String dockerHost();
/**
* Path to a directory containing Docker's config.json (e.g. /root/.docker).
* When set, overrides the system default. Useful when Floci runs inside Docker
* and the host ~/.docker directory is mounted in.
*/
Optional<String> dockerConfigPath();
/**
* Explicit credentials for private Docker registries.
* Each entry maps a registry hostname to a username/password pair.
* Use when mounting the host Docker config is impractical.
*/
@WithDefault("")
List<RegistryCredential> registryCredentials();
interface RegistryCredential {
/** Registry hostname (e.g. myregistry.example.com). */
String server();
String username();
String password();
}
}
}
@@ -30,12 +30,16 @@ public class DockerClientProducer {
@Produces
@ApplicationScoped
public DockerClient dockerClient() {
String dockerHost = config.services().lambda().dockerHost();
String dockerHost = config.docker().dockerHost();
LOG.infov("Creating DockerClient for host: {0}", dockerHost);
DefaultDockerClientConfig clientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost(dockerHost)
.build();
DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost(dockerHost);
config.docker().dockerConfigPath().ifPresent(path -> {
LOG.infov("Using Docker config path: {0}", path);
builder.withDockerConfig(path);
});
DefaultDockerClientConfig clientConfig = builder.build();
ApacheDockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
.dockerHost(clientConfig.getDockerHost())
@@ -2,10 +2,13 @@ package io.github.hectorvent.floci.services.lambda.launcher;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.PullImageResultCallback;
import com.github.dockerjava.api.model.AuthConfig;
import io.github.hectorvent.floci.config.EmulatorConfig;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
@@ -20,12 +23,14 @@ public class ImageCacheService {
private static final Logger LOG = Logger.getLogger(ImageCacheService.class);
private final DockerClient dockerClient;
private final List<EmulatorConfig.DockerConfig.RegistryCredential> registryCredentials;
private final Set<String> pulledImages = ConcurrentHashMap.newKeySet();
private final ConcurrentHashMap<String, Object> locks = new ConcurrentHashMap<>();
@Inject
public ImageCacheService(DockerClient dockerClient) {
public ImageCacheService(DockerClient dockerClient, EmulatorConfig config) {
this.dockerClient = dockerClient;
this.registryCredentials = config.docker().registryCredentials();
}
public void ensureImageExists(String imageUri) {
@@ -40,6 +45,7 @@ public class ImageCacheService {
LOG.infov("Pulling image: {0}", imageUri);
try {
dockerClient.pullImageCmd(imageUri)
.withAuthConfig(resolveAuth(imageUri))
.exec(new PullImageResultCallback())
.awaitCompletion(5, TimeUnit.MINUTES);
pulledImages.add(imageUri);
@@ -50,4 +56,23 @@ public class ImageCacheService {
}
}
}
private AuthConfig resolveAuth(String imageUri) {
String host = extractRegistryHost(imageUri);
for (EmulatorConfig.DockerConfig.RegistryCredential cred : registryCredentials) {
if (cred.server().equals(host)) {
LOG.debugv("Using configured credentials for registry: {0}", host);
return new AuthConfig()
.withUsername(cred.username())
.withPassword(cred.password())
.withRegistryAddress(cred.server());
}
}
return new AuthConfig();
}
static String extractRegistryHost(String imageUri) {
String firstSegment = imageUri.split("/")[0];
return (firstSegment.contains(".") || firstSegment.contains(":")) ? firstSegment : "";
}
}