Answers: <p>In Ruby you can use this function:</p>
  
  <pre><code># return true if we are inside a docker container
  def in_container?
    return File.file?('/.dockerenv')
  end
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-03-03T15:13:57.680Z
Answers: 2020-03-03T15:13:57.680Z
Answers: Hedi Nasr
Body: <p>I'm writing an <a href="https://www.inspec.io/docs/reference/profiles/" rel="noreferrer">Inspec profile</a> which has a specific control that I want to skip <em>if the control is running inside a docker container</em>. This looks like:</p>
  
  <pre><code>control 'ssh daemon' do
    impact 'critical'
    only_if('not in docker') do
      condition_expression
    end
    describe service('sshd') do
      it { should be_installed }
      it { should be_enabled }
      it { should be_running }
    end
  end
  </code></pre>
  
  <p>I need the <code>condition_expression</code> to return false when in a container. Is there an elegant way to do this with ruby or inspec without shelling out?</p>
  
Tags: <docker><inspec>
Title: Condition to skip a test when running in a container
UserDisplayName: Bruce Becker
ThreadText: # Condition to skip a test when running in a container
  
  (Asked by: username_0 on 2020-03-03)
  
  I'm writing an [Inspec profile](https://www.inspec.io/docs/reference/profiles/) which has a specific control that I want to skip *if the control is running inside a docker container*. This looks like:
  
  ```
  control 'ssh daemon' do
    impact 'critical'
    only_if('not in docker') do
      condition_expression
    end
    describe service('sshd') do
      it { should be_installed }
      it { should be_enabled }
      it { should be_running }
    end
  end
  
  ```
  
  I need the `condition_expression` to return false when in a container. Is there an elegant way to do this with ruby or inspec without shelling out?
  
  **Tags:** docker, inspec
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-03-03. Score: 5 | Accepted answer
  
  In Ruby you can use this function:
  
  ```
  # return true if we are inside a docker container
  def in_container?
    return File.file?('/.dockerenv')
  end
  
  ```
  
  
  ---
  
  