feat: add properties -it (tty and stdin_opn) to docker container

Closes #120
This commit is contained in:
tanis 2021-01-18 08:00:26 +00:00 committed by GitHub
parent 3036de3068
commit 8b617bd690
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 2 deletions

View file

@ -842,6 +842,18 @@ func resourceDockerContainer() *schema.Resource {
Optional: true,
Computed: true,
},
"tty": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},
"stdin_open": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},
},
}
}

View file

@ -43,6 +43,8 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err
Image: image,
Hostname: d.Get("hostname").(string),
Domainname: d.Get("domainname").(string),
Tty: d.Get("tty").(bool),
OpenStdin: d.Get("stdin_open").(bool),
}
if v, ok := d.GetOk("env"); ok {
@ -628,6 +630,7 @@ func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error
d.Set("publish_all_ports", container.HostConfig.PublishAllPorts)
d.Set("restart", container.HostConfig.RestartPolicy.Name)
d.Set("max_retry_count", container.HostConfig.RestartPolicy.MaximumRetryCount)
// From what I can tell Init being nullable is only for container creation to allow
// dockerd to default it to the daemons own default settings. So this != nil
// check is most likely not ever going to fail. In the event that it does the
@ -712,6 +715,8 @@ func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error
d.Set("sysctls", container.HostConfig.Sysctls)
d.Set("ipc_mode", container.HostConfig.IpcMode)
d.Set("group_add", container.HostConfig.GroupAdd)
d.Set("tty", container.Config.Tty)
d.Set("stdin_open", container.Config.OpenStdin)
return nil
}

View file

@ -448,6 +448,56 @@ func TestAccDockerContainer_groupadd_multiple(t *testing.T) {
})
}
func TestAccDockerContainer_tty(t *testing.T) {
var c types.ContainerJSON
testCheck := func(*terraform.State) error {
if !c.Config.Tty {
return fmt.Errorf("Tty not enabled")
}
return nil
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDockerContainerTTYConfig,
Check: resource.ComposeTestCheckFunc(
testAccContainerRunning("docker_container.foo", &c),
testCheck,
),
},
},
})
}
func TestAccDockerContainer_STDIN_Enabled(t *testing.T) {
var c types.ContainerJSON
testCheck := func(*terraform.State) error {
if !c.Config.OpenStdin {
return fmt.Errorf("STDIN not enabled")
}
return nil
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDockerContainerSTDIN_Config,
Check: resource.ComposeTestCheckFunc(
testAccContainerRunning("docker_container.foo", &c),
testCheck,
),
},
},
})
}
func TestAccDockerContainer_customized(t *testing.T) {
var c types.ContainerJSON
@ -2387,3 +2437,27 @@ resource "docker_container" "foo" {
]
}
`
const testAccDockerContainerTTYConfig = `
resource "docker_image" "foo" {
name = "nginx:latest"
}
resource "docker_container" "foo" {
name = "tf-test"
image = "${docker_image.foo.latest}"
tty = true
}
`
const testAccDockerContainerSTDIN_Config = `
resource "docker_image" "foo" {
name = "nginx:latest"
}
resource "docker_container" "foo" {
name = "tf-test"
image = "${docker_image.foo.latest}"
stdin_open = true
}
`

View file

@ -21,7 +21,7 @@ call:mkdirp %~dp0auth
call docker run ^
--rm ^
--entrypoint htpasswd ^
registry:2 ^
registry:2.7.0 ^
-Bbn testuser testpwd ^
> %~dp0auth\htpasswd
if %ErrorLevel% neq 0 (
@ -43,7 +43,7 @@ call docker run ^
-v %~dp0certs:/certs ^
-e "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/registry_auth.crt" ^
-e "REGISTRY_HTTP_TLS_KEY=/certs/registry_auth.key" ^
registry:2
registry:2.7.0
if %ErrorLevel% neq 0 (
call:print "Failed to create ephemeral Docker registry."
exit /b %ErrorLevel%

View file

@ -75,6 +75,8 @@ data is stored in them. See [the docker documentation](https://docs.docker.com/n
* `must_run` - (Optional, boolean) If true, then the Docker container will be
kept running. If false, then as long as the container exists, Terraform
assumes it is successful.
* `tty` - (Optional, boolean) if true, allocate a pseudo-tty (docker run -t)
* `stdin_open` - (Optional, boolean) if true, keep STDIN open even if not attached (docker run -i)
* `capabilities` - (Optional, block) See [Capabilities](#capabilities-1) below for details.
* `security_opts` - (Optional, set of strings) Set of string values to customize labels for MLS systems, such as SELinux. See https://docs.docker.com/engine/reference/run/#security-configuration.
* `mounts` - (Optional, set of blocks) See [Mounts](#mounts-1) below for details.