Project: Chef InSpec (Provisioning / Security & Compliance)
File: docker_container.md

+++ title = "docker_container resource" draft = false gh_repo = "inspec" platform = "linux" [menu] [menu.inspec] title = "docker_container" identifier = "inspec/resources/os/dockercontainer.md dockercontainer resource" parent = "inspec/resources/os" +++ Use the `docker_container` Chef InSpec audit resource to test a Docker container. This resource is distributed with Chef InSpec. This resource is available from the InSpec version 1.21.0. A `docker_container` resource block declares the configuration data to be tested: describe docker_container('container') do it { should exist } it { should be_running } its('id') { should_not eq '' } its('image') { should eq 'busybox:latest' } its('repo') { should eq 'busybox' } its('tag') { should eq 'latest' } its('ports') { should eq [] } its('command') { should eq 'nc -ll -p 1234 -e /bin/cat' } end The container name can be provided with the `name` resource parameter. describe docker_container(name: 'an-echo-server') do it { should exist } it { should be_running } end Alternatively, you can pass in the container id. describe docker_container(id: '71b5df59442b') do it { should exist } it { should be_running } end The following examples show how to use this Chef InSpec resource. The `id` property tests the container ID. its('id') { should eq 'sha:71b5df59...442b' } The `repo` property tests the value of the image repository. its('repo') { should eq 'REPO' } The `tag` property tests the value of the image tag. its('tag') { should eq 'LATEST' } The `ports` property tests the value of the Docker ports. its('ports') { should eq '0.0.0.0:1234->1234/tcp' } The `command` property tests the value of the container run command. its('command') { should eq 'nc -ll -p 1234 -e /bin/cat' } describe docker_container('an-echo-server') do it { should exist } it { should be_running } its('id') { should_not eq '' } its('image') { should eq 'busybox:latest' } its('repo') { should eq 'busybox' } its('tag') { should eq 'latest' } its('ports') { should eq [] } its('command') { should eq 'nc -ll -p 1234 -e /bin/cat' } end {{< readfile file="content/inspec/reusable/md/inspecmatcherslink.md" >}} The specific matchers of this resource are: `exist`, `berunning`, `havevolume?`. The `exist` matcher specifies if the container exists. it { should exist } The `be_running` matcher checks if the container is running. it { should be_running } The `have_volume?` matcher checks if the container has mounted volumes. it { should havevolume?(destinationpathincontainer, sourcepathin_source) } The following examples show how to use this Chef InSpec audit resource. The below test passes if the container `wonderful_wozniak` exists as part of the Docker instances. describe dockercontainer('wonderfulwozniak') do it { should exist } end The below test passes if the container `trusting_williams` exists as part of the Docker instances and the status is running. describe dockercontainer('trustingwilliams') do it { should be_running } end The below test passes if the container `quizzicalwilliamson` exists as part of the Docker instances, the status is running, and has mounted volume on `/app` in the container from the source path of `/var/lib/docker/volumes/myvol2/data` describe dockercontainer('quizzicalwilliamson') do it { should havevolume('/app', '/var/lib/docker/volumes/myvol2/data') } end