Class ContainerDefinition

java.lang.Object
software.amazon.jsii.JsiiObject
software.constructs.Construct
software.amazon.awscdk.services.ecs.ContainerDefinition
All Implemented Interfaces:
software.amazon.jsii.JsiiSerializable, software.constructs.IConstruct, software.constructs.IDependable
Direct Known Subclasses:
FirelensLogRouter

@Generated(value="jsii-pacmak/1.104.0 (build e79254c)", date="2024-11-22T02:24:01.053Z") @Stability(Stable) public class ContainerDefinition extends software.constructs.Construct
A container definition is used in a task definition to describe the containers that are launched as part of a task.

Example:

 TaskDefinition taskDefinition;
 Cluster cluster;
 // Add a container to the task definition
 ContainerDefinition specificContainer = taskDefinition.addContainer("Container", ContainerDefinitionOptions.builder()
         .image(ContainerImage.fromRegistry("/aws/aws-example-app"))
         .memoryLimitMiB(2048)
         .build());
 // Add a port mapping
 specificContainer.addPortMappings(PortMapping.builder()
         .containerPort(7600)
         .protocol(Protocol.TCP)
         .build());
 Ec2Service.Builder.create(this, "Service")
         .cluster(cluster)
         .taskDefinition(taskDefinition)
         .cloudMapOptions(CloudMapOptions.builder()
                 // Create SRV records - useful for bridge networking
                 .dnsRecordType(DnsRecordType.SRV)
                 // Targets port TCP port 7600 `specificContainer`
                 .container(specificContainer)
                 .containerPort(7600)
                 .build())
         .build();
 
  • Field Details

    • CONTAINER_PORT_USE_RANGE

      @Stability(Stable) public static final Number CONTAINER_PORT_USE_RANGE
  • Constructor Details

    • ContainerDefinition

      protected ContainerDefinition(software.amazon.jsii.JsiiObjectRef objRef)
    • ContainerDefinition

      protected ContainerDefinition(software.amazon.jsii.JsiiObject.InitializationMode initializationMode)
    • ContainerDefinition

      @Stability(Stable) public ContainerDefinition(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull ContainerDefinitionProps props)
      Constructs a new instance of the ContainerDefinition class.

      Parameters:
      scope - This parameter is required.
      id - This parameter is required.
      props - This parameter is required.
  • Method Details

    • addContainerDependencies

      @Stability(Stable) public void addContainerDependencies(@NotNull @NotNull ContainerDependency... containerDependencies)
      This method adds one or more container dependencies to the container.

      Parameters:
      containerDependencies - This parameter is required.
    • addDockerLabel

      @Stability(Stable) public void addDockerLabel(@NotNull String name, @NotNull String value)
      This method adds a Docker label to the container.

      Parameters:
      name - This parameter is required.
      value - This parameter is required.
    • addEnvironment

      @Stability(Stable) public void addEnvironment(@NotNull String name, @NotNull String value)
      This method adds an environment variable to the container.

      Parameters:
      name - This parameter is required.
      value - This parameter is required.
    • addInferenceAcceleratorResource

      @Stability(Stable) public void addInferenceAcceleratorResource(@NotNull @NotNull String... inferenceAcceleratorResources)
      This method adds one or more resources to the container.

      Parameters:
      inferenceAcceleratorResources - This parameter is required.
    • addLink

      @Stability(Stable) public void addLink(@NotNull ContainerDefinition container, @Nullable String alias)
      This method adds a link which allows containers to communicate with each other without the need for port mappings.

      This parameter is only supported if the task definition is using the bridge network mode. Warning: The --link flag is a legacy feature of Docker. It may eventually be removed.

      Parameters:
      container - This parameter is required.
      alias -
    • addLink

      @Stability(Stable) public void addLink(@NotNull ContainerDefinition container)
      This method adds a link which allows containers to communicate with each other without the need for port mappings.

      This parameter is only supported if the task definition is using the bridge network mode. Warning: The --link flag is a legacy feature of Docker. It may eventually be removed.

      Parameters:
      container - This parameter is required.
    • addMountPoints

      @Stability(Stable) public void addMountPoints(@NotNull @NotNull MountPoint... mountPoints)
      This method adds one or more mount points for data volumes to the container.

      Parameters:
      mountPoints - This parameter is required.
    • addPortMappings

      @Stability(Stable) public void addPortMappings(@NotNull @NotNull PortMapping... portMappings)
      This method adds one or more port mappings to the container.

      Parameters:
      portMappings - This parameter is required.
    • addScratch

      @Stability(Stable) public void addScratch(@NotNull ScratchSpace scratch)
      This method mounts temporary disk space to the container.

      This adds the correct container mountPoint and task definition volume.

      Parameters:
      scratch - This parameter is required.
    • addSecret

      @Stability(Stable) public void addSecret(@NotNull String name, @NotNull Secret secret)
      This method adds a secret as environment variable to the container.

      Parameters:
      name - This parameter is required.
      secret - This parameter is required.
    • addToExecutionPolicy

      @Stability(Stable) public void addToExecutionPolicy(@NotNull PolicyStatement statement)
      This method adds the specified statement to the IAM task execution policy in the task definition.

      Parameters:
      statement - This parameter is required.
    • addUlimits

      @Stability(Stable) public void addUlimits(@NotNull @NotNull Ulimit... ulimits)
      This method adds one or more ulimits to the container.

      Parameters:
      ulimits - This parameter is required.
    • addVolumesFrom

      @Stability(Stable) public void addVolumesFrom(@NotNull @NotNull VolumeFrom... volumesFrom)
      This method adds one or more volumes to the container.

      Parameters:
      volumesFrom - This parameter is required.
    • findPortMapping

      @Stability(Stable) @Nullable public PortMapping findPortMapping(@NotNull Number containerPort, @NotNull Protocol protocol)
      Returns the host port for the requested container port if it exists.

      Parameters:
      containerPort - This parameter is required.
      protocol - This parameter is required.
    • findPortMappingByName

      @Stability(Stable) @Nullable public PortMapping findPortMappingByName(@NotNull String name)
      Returns the port mapping with the given name, if it exists.

      Parameters:
      name - This parameter is required.
    • renderContainerDefinition

      @Stability(Stable) @NotNull public CfnTaskDefinition.ContainerDefinitionProperty renderContainerDefinition(@Nullable TaskDefinition _taskDefinition)
      Render this container definition to a CloudFormation object.

      Parameters:
      _taskDefinition - [disable-awslint:ref-via-interface] (unused but kept to avoid breaking change).
    • renderContainerDefinition

      @Stability(Stable) @NotNull public CfnTaskDefinition.ContainerDefinitionProperty renderContainerDefinition()
      Render this container definition to a CloudFormation object.
    • getContainerDependencies

      @Stability(Stable) @NotNull public List<ContainerDependency> getContainerDependencies()
      An array dependencies defined for container startup and shutdown.
    • getContainerName

      @Stability(Stable) @NotNull public String getContainerName()
      The name of this container.
    • getContainerPort

      @Stability(Stable) @NotNull public Number getContainerPort()
      The port the container will listen on.
    • getEssential

      @Stability(Stable) @NotNull public Boolean getEssential()
      Specifies whether the container will be marked essential.

      If the essential parameter of a container is marked as true, and that container fails or stops for any reason, all other containers that are part of the task are stopped. If the essential parameter of a container is marked as false, then its failure does not affect the rest of the containers in a task.

      If this parameter is omitted, a container is assumed to be essential.

    • getImageName

      @Stability(Stable) @NotNull public String getImageName()
      The name of the image referenced by this container.
    • getIngressPort

      @Stability(Stable) @NotNull public Number getIngressPort()
      The inbound rules associated with the security group the task or service will use.

      This property is only used for tasks that use the awsvpc network mode.

    • getMemoryLimitSpecified

      @Stability(Stable) @NotNull public Boolean getMemoryLimitSpecified()
      Whether there was at least one memory limit specified in this definition.
    • getMountPoints

      @Stability(Stable) @NotNull public List<MountPoint> getMountPoints()
      The mount points for data volumes in your container.
    • getPortMappings

      @Stability(Stable) @NotNull public List<PortMapping> getPortMappings()
      The list of port mappings for the container.

      Port mappings allow containers to access ports on the host container instance to send or receive traffic.

    • getTaskDefinition

      @Stability(Stable) @NotNull public TaskDefinition getTaskDefinition()
      The name of the task definition that includes this container definition.
    • getUlimits

      @Stability(Stable) @NotNull public List<Ulimit> getUlimits()
      An array of ulimits to set in the container.
    • getVolumesFrom

      @Stability(Stable) @NotNull public List<VolumeFrom> getVolumesFrom()
      The data volumes to mount from another container in the same task definition.
    • getCpu

      @Stability(Stable) @Nullable public Number getCpu()
      The number of cpu units reserved for the container.
    • getCredentialSpecs

      @Stability(Stable) @Nullable public List<CredentialSpecConfig> getCredentialSpecs()
      The crdential specifications for this container.
    • getEnvironmentFiles

      @Stability(Stable) @Nullable public List<EnvironmentFileConfig> getEnvironmentFiles()
      The environment files for this container.
    • getLinuxParameters

      @Stability(Stable) @Nullable public LinuxParameters getLinuxParameters()
      The Linux-specific modifications that are applied to the container, such as Linux kernel capabilities.
    • getLogDriverConfig

      @Stability(Stable) @Nullable public LogDriverConfig getLogDriverConfig()
      The log configuration specification for the container.
    • getPseudoTerminal

      @Stability(Stable) @Nullable public Boolean getPseudoTerminal()
      Specifies whether a TTY must be allocated for this container.
    • getReferencesSecretJsonField

      @Stability(Stable) @Nullable public Boolean getReferencesSecretJsonField()
      Whether this container definition references a specific JSON field of a secret stored in Secrets Manager.