2015-03-28 22:05:17 -04:00
---
layout: "docker"
page_title: "Docker: docker_container"
sidebar_current: "docs-docker-resource-container"
description: |-
Manages the lifecycle of a Docker container.
---
# docker\_container
Manages the lifecycle of a Docker container.
## Example Usage
2017-04-17 06:17:54 -04:00
```hcl
2015-03-28 22:05:17 -04:00
# Start a container
resource "docker_container" "ubuntu" {
2017-02-18 17:48:50 -05:00
name = "foo"
2015-03-28 22:05:17 -04:00
image = "${docker_image.ubuntu.latest}"
}
# Find the latest Ubuntu precise image.
resource "docker_image" "ubuntu" {
2015-06-28 22:14:00 -04:00
name = "ubuntu:precise"
2015-03-28 22:05:17 -04:00
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required, string) The name of the Docker container.
* `image` - (Required, string) The ID of the image to back this container.
The easiest way to get this value is to use the `docker_image` resource
as is shown in the example above.
* `command` - (Optional, list of strings) The command to use to start the
2015-10-01 17:43:08 -04:00
container. For example, to run `/usr/bin/myprogram -f baz.conf` set the
command to be `["/usr/bin/myprogram", "-f", "baz.conf"]` .
2015-10-26 17:24:48 -04:00
* `entrypoint` - (Optional, list of strings) The command to use as the
Entrypoint for the container. The Entrypoint allows you to configure a
container to run as an executable. For example, to run `/usr/bin/myprogram`
when starting a container, set the entrypoint to be
`["/usr/bin/myprogram"]` .
2016-04-04 22:43:59 -04:00
* `user` - (Optional, string) User used for run the first process. Format is
`user` or `user:group` which user and group can be passed literraly or
by name.
2015-03-28 22:05:17 -04:00
* `dns` - (Optional, set of strings) Set of DNS servers.
2016-06-29 10:48:15 -04:00
* `dns_opts` - (Optional, set of strings) Set of DNS options used by the DNS provider(s), see `resolv.conf` documentation for valid list of options.
* `dns_search` - (Optional, set of strings) Set of DNS search domains that are used when bare unqualified hostnames are used inside of the container.
2016-07-11 18:37:51 -04:00
* `env` - (Optional, set of strings) Environment variables to set.
2016-01-30 16:23:35 -05:00
* `labels` - (Optional, map of strings) Key/value pairs to set as labels on the
container.
2015-04-16 15:42:21 -04:00
* `links` - (Optional, set of strings) Set of links for link based
connectivity between containers that are running on the same host.
2018-03-10 09:50:56 -05:00
~> **Warning** The --link flag is a legacy feature of Docker. It may eventually
be removed. It exposes _all_ environment variables originating from Docker to
any linked containers. This could have serious security implications if sensitive
data is stored in them. See [the docker documentation][linkdoc] for more details.
2015-03-28 22:05:17 -04:00
* `hostname` - (Optional, string) Hostname of the container.
* `domainname` - (Optional, string) Domain name of the container.
2015-10-27 12:08:57 -04:00
* `restart` - (Optional, string) The restart policy for the container. Must be
2016-02-25 22:38:31 -05:00
one of "no", "on-failure", "always", "unless-stopped".
2015-10-27 12:08:57 -04:00
* `max_retry_count` - (Optional, int) The maximum amount of times to an attempt
a restart when `restart` is set to "on-failure"
2015-03-28 22:05:17 -04:00
* `must_run` - (Optional, bool) If true, then the Docker container will be
kept running. If false, then as long as the container exists, Terraform
assumes it is successful.
2017-03-07 11:48:20 -05:00
* `capabilities` - (Optional, block) See [Capabilities ](#capabilities ) below for details.
2016-01-30 16:23:35 -05:00
* `ports` - (Optional, block) See [Ports ](#ports ) below for details.
2016-10-27 05:54:05 -04:00
* `host` - (Optional, block) See [Extra Hosts ](#extra_hosts ) below for
2016-01-30 16:23:35 -05:00
details.
2015-06-04 06:57:38 -04:00
* `privileged` - (Optional, bool) Run container in privileged mode.
2018-04-20 05:14:44 -04:00
* `devices` - (Optional, bool) See [Devices ](#devices ) below for details.
2015-03-28 22:05:17 -04:00
* `publish_all_ports` - (Optional, bool) Publish all ports of the container.
2016-01-30 16:23:35 -05:00
* `volumes` - (Optional, block) See [Volumes ](#volumes ) below for details.
2015-10-27 19:53:49 -04:00
* `memory` - (Optional, int) The memory limit for the container in MBs.
* `memory_swap` - (Optional, int) The total memory limit (memory + swap) for the
2016-06-29 10:48:15 -04:00
container in MBs. This setting may compute to `-1` after `terraform apply` if the target host doesn't support memory swap, when that is the case docker will use a soft limitation.
2015-10-27 19:53:49 -04:00
* `cpu_shares` - (Optional, int) CPU shares (relative weight) for the container.
2015-11-04 12:42:55 -05:00
* `log_driver` - (Optional, string) The logging driver to use for the container.
Defaults to "json-file".
2016-01-30 16:23:35 -05:00
* `log_opts` - (Optional, map of strings) Key/value pairs to use as options for
the logging driver.
2017-05-22 09:20:32 -04:00
* `network_alias` - (Optional, set of strings) Network aliases of the container for user-defined networks only.
2016-01-30 16:23:35 -05:00
* `network_mode` - (Optional, string) Network mode of the container.
* `networks` - (Optional, set of strings) Id of the networks in which the
container is.
2016-07-11 11:03:02 -04:00
* `destroy_grace_seconds` - (Optional, int) If defined will attempt to stop the container before destroying. Container will be destroyed after `n` seconds or on successful stop.
2016-12-05 06:06:34 -05:00
* `upload` - (Optional, block) See [File Upload ](#upload ) below for details.
2018-04-20 05:35:49 -04:00
* `ulimit` - (Optional, block) See [Ulimits ](#ulimits ) below for
details.
2018-10-11 04:55:18 -04:00
* `pid_mode` - (Optional, string) The PID (Process) Namespace mode for the container. Either `container:<name|id>` or `host` .
* `userns_mode` - (Optional, string) Sets the usernamespace mode for the container when usernamespace remapping option is enabled.
2015-03-28 22:05:17 -04:00
2017-03-07 11:48:20 -05:00
< a id = "capabilities" > < / a >
### Capabilities
`capabilities` is a block within the configuration that allows you to add or drop linux capabilities. For more information about what capabilities you can add and drop please visit the docker run documentation.
* `add` - (Optional, set of strings) list of linux capabilities to add.
* `drop` - (Optional, set of strings) list of linux capabilities to drop.
Example:
2017-04-17 06:17:54 -04:00
```hcl
2017-03-07 11:48:20 -05:00
resource "docker_container" "ubuntu" {
name = "foo"
image = "${docker_image.ubuntu.latest}"
capabilities {
add = ["ALL"]
drop = ["SYS_ADMIN"]
}
}
```
2015-03-28 22:05:17 -04:00
< a id = "ports" > < / a >
2016-01-30 16:23:35 -05:00
### Ports
2015-03-28 22:05:17 -04:00
`ports` is a block within the configuration that can be repeated to specify
the port mappings of the container. Each `ports` block supports
the following:
* `internal` - (Required, int) Port within the container.
2018-10-09 16:32:26 -04:00
* `external` - (Optional, int) Port exposed out of the container, defaults to `32768` .
* `ip` - (Optional, string) IP address/mask that can access this port, default to `0.0.0.0`
2015-03-28 22:05:17 -04:00
* `protocol` - (Optional, string) Protocol that can be used over this port,
defaults to TCP.
2015-10-09 09:05:43 -04:00
< a id = "extra_hosts" > < / a >
2016-01-30 16:23:35 -05:00
### Extra Hosts
2015-10-09 09:05:43 -04:00
2016-10-27 05:54:05 -04:00
`host` is a block within the configuration that can be repeated to specify
the extra host mappings for the container. Each `host` block supports
2015-10-09 09:05:43 -04:00
the following:
2016-10-27 05:54:05 -04:00
* `host` - (Required, string) Hostname to add.
* `ip` - (Required, string) IP address this hostname should resolve to.
2015-10-09 09:05:43 -04:00
2016-01-14 21:59:07 -05:00
This is equivalent to using the `--add-host` option when using the `run`
command of the Docker CLI.
2015-03-28 22:05:17 -04:00
< a id = "volumes" > < / a >
2016-01-30 16:23:35 -05:00
### Volumes
2015-03-28 22:05:17 -04:00
`volumes` is a block within the configuration that can be repeated to specify
the volumes attached to a container. Each `volumes` block supports
the following:
* `from_container` - (Optional, string) The container where the volume is
coming from.
* `host_path` - (Optional, string) The path on the host where the volume
is coming from.
2016-01-15 16:59:33 -05:00
* `volume_name` - (Optional, string) The name of the docker volume which
should be mounted.
* `container_path` - (Optional, string) The path in the container where the
volume will be mounted.
2015-10-07 16:07:41 -04:00
* `read_only` - (Optional, bool) If true, this volume will be readonly.
2015-03-28 22:05:17 -04:00
Defaults to false.
2016-01-30 16:23:35 -05:00
2016-01-15 16:59:33 -05:00
One of `from_container` , `host_path` or `volume_name` must be set.
2015-04-20 13:45:58 -04:00
2016-12-05 06:06:34 -05:00
< a id = "upload" > < / a >
### File Upload
`upload` is a block within the configuration that can be repeated to specify
files to upload to the container before starting it.
Each `upload` supports the following
* `content` - (Required, string) A content of a file to upload.
* `file` - (Required, string) path to a file in the container.
2018-04-20 05:30:45 -04:00
* `executable` - (Optional, bool) If true, the file will be uploaded with user
executable permission.
Defaults to false.
2016-12-05 06:06:34 -05:00
2018-04-20 05:14:44 -04:00
< a id = "devices" > < / a >
### Devices
`devices` is a block within the configuration that can be repeated to specify
the devices exposed to a container. Each `devices` block supports
the following:
* `host_path` - (Required, string) The path on the host where the device
is located.
* `container_path` - (Optional, string) The path in the container where the
device will be binded.
* `permissions` - (Optional, string) The cgroup permissions given to the
container to access the device.
Defaults to `rwm` .
2018-04-20 05:35:49 -04:00
< a id = "ulimits" > < / a >
### Ulimits
`ulimit` is a block within the configuration that can be repeated to specify
the extra ulimits for the container. Each `ulimit` block supports
the following:
* `name` - (Required, string)
* `soft` - (Required, int)
* `hard` - (Required, int)
2015-04-20 13:45:58 -04:00
## Attributes Reference
The following attributes are exported:
* `ip_address` - The IP address of the container as read from its
NetworkSettings.
* `ip_prefix_length` - The IP prefix length of the container as read from its
NetworkSettings.
* `gateway` - The network gateway of the container as read from its
NetworkSettings.
* `bridge` - The network bridge of the container as read from its
NetworkSettings.
2018-03-10 09:50:56 -05:00
2018-10-09 16:32:26 -04:00
[linkdoc] https://docs.docker.com/network/links/