Command, environment and resources

edit

Command and environment

var definition = new ContainerDefinition(ImageSource.FromRegistry("alpine"));
definition.Entrypoint.Add("/bin/sh"); // replaces the entrypoint of the image
definition.Command.AddRange(["-c", "echo started && sleep 3600"]);
definition.Environment.Add("APP_MODE", "test"); // adds or replaces a variable
definition.User = "1000:1000";
definition.WorkingDirectory = "/tmp";
definition.Hostname = "test-server";
definition.Labels.Add("com.example.suite", "integration");
SettingEffect
EntrypointReplaces the entrypoint of the image. Each item is one argument.
CommandThe command passed to the entrypoint, one argument per item.
EnvironmentThe environment variables. Add replaces a variable that is already set; GetValue, Contains and Remove read and remove them.
UserThe user the container runs as (name, uid or uid:gid).
WorkingDirectoryThe working directory inside the container.
HostnameThe hostname of the container.
LabelsLabels added to the container, for your own tools.

Each argument is passed as is, without a shell: to use shell syntax (&&, variables), run a shell as in the example.

The values of the environment variables are passed to the command lines through a file only the current user can read, not on their command line, which every user of the machine can read. wslc is the exception, and so is a value that spans several lines, which stays on the command line.

Resource limits

definition.Resources.MemoryLimit = 512L * 1024 * 1024; // in bytes
definition.Resources.CpuLimit = 1.5; // number of CPUs
definition.Resources.ReadOnlyRootFilesystem = true;

Network

By default, the container uses the default network of the runtime, and you reach it through its published ports. To have containers reach each other by name, connect them to the same network:

definition.Network.Network = "integration-tests"; // a network that already exists
definition.Network.Alias = "database"; // the name the other containers of the network use

To have the library create the network, and to connect a container to several networks, see Networks. Capabilities, shared memory, ulimits and the other less common options are described in Advanced options.

Runtime differences

Apple's container cannot set Hostname. See Container runtimes for the other differences.