docker_commander 1.0.7
docker_commander: ^1.0.7 copied to clipboard
A Docker manager, for local and remote host machins. Works with personalized containers and pre-configured popular containers.
docker_commander #
Docker manager, for personalized containers and pre-configured popular containers.
Usage #
Local Docker #
Here's a simple usage example for a local host machine:
import 'package:docker_commander/docker_commander.dart';
import 'package:docker_commander/src/docker_commander_local.dart';
void main() async {
// Creates a `DockerCommander` for a local host machine:
var dockerCommander = DockerCommander(DockerHostLocal());
// Initialize `DockerCommander`:
await dockerCommander.initialize();
// Ensure that Docker daemon is running.
await dockerCommander.checkDaemon();
// Run Docker image `hello-world`:
var dockerContainer = await dockerCommander.run('hello-world');
// Waits container to be ready (ensure that the container started).
await dockerContainer.waitReady();
// Waits the container to exit, and gets the exit code:
var exitCode = await dockerContainer.waitExit();
// Gets all the STDOUT as [String].
var output = dockerContainer.stdout.asString;
print(output);
print('EXIT CODE: $exitCode');
}
Remote Docker #
Here's another usage example for a remote host machine:
Server
Start DockerHostServer
:
import 'package:docker_commander/docker_commander_vm.dart';
void main() {
// A simple username and password table:
var authenticationTable = AuthenticationTable({'admin': '123'});
// A `DockerHost` Server at port 8099:
var hostServer = DockerHostServer(
(user, pass) async => authenticationTable.checkPassword(user, pass),
8099);
// Starts the server and wait initialization:
await hostServer.startAndWait();
}
Client
Client side using DockerHostRemote
:
import 'package:docker_commander/docker_commander_vm.dart';
void main() async {
// Connect to a `DockerHost` running at '10.0.0.52:8099'
var dockerHostRemote = DockerHostRemote('10.0.0.52', 8099, username: 'admin', password: '123')
// Creates a `DockerCommander` for a remote host machine:
var dockerCommander = DockerCommander(dockerHostRemote);
// Initialize `DockerCommander` (at remote server):
await dockerCommander.initialize();
// Ensure that Docker daemon is running (at remote server):
await dockerCommander.checkDaemon();
// Run Docker image `hello-world` (at remote server):
var dockerContainer = await dockerCommander.run('hello-world');
// Behavior is the same of example using `DockerHostLocal`.
// Internal `DockerRunner` will sync remote output automatically!
// ...
// Gets all the STDOUT as [String].
var output = dockerContainer.stdout.asString;
print(output);
// ...
}
PostgreSQLContainer #
A pre-configured PostgreSQL Container:
import 'package:docker_commander/docker_commander_vm.dart';
void main() async {
// Creates a `DockerCommander` for a local host machine:
var dockerCommander = DockerCommander(DockerHostLocal());
// Start PostgreSQL container:
var dockerContainer = await PostgreSQLContainer().run(dockerCommander);
// Wait PostgreSQL to start and be ready to receive requests:
await dockerContainer.waitReady();
// Print the current STDOUT of the container:
var output = dockerContainer.stdout.asString;
print(output);
// Stops PostgreSQL, with a timeout of 20s:
await dockerContainer.stop(timeout: Duration(seconds: 20));
// Wait PostgreSQL to exit and get exit code:
var exitCode = await dockerContainer.waitExit();
// ...
}
See Also #
See package docker_commander_test, for unit test framework with Docker containers.
Features and bugs #
Please file feature requests and bugs at the issue tracker.
Author #
Graciliano M. Passos: gmpassos@GitHub.
License #
Dart free & open-source license.