Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
fail-fast: false
matrix:
os: [ ubuntu-latest, macos-latest ]
ruby: [ '2.7', '3.0', '3.1' ]
ruby: [ '3.2', '3.3', '3.4' ]

runs-on: ${{ matrix.os }}

Expand Down
3 changes: 2 additions & 1 deletion berkeley_library-docker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = ">= #{ruby_version}"

spec.add_development_dependency 'rails', '~> 6'
spec.add_development_dependency 'fakefs'
spec.add_development_dependency 'rails', '~> 8'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'rspec_junit_formatter'
Expand Down
10 changes: 9 additions & 1 deletion lib/berkeley_library/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module BerkeleyLibrary
module Docker
class << self
def running_in_container?
File.exist?('/.dockerenv') || init_cgroup_is_dockerish?
File.exist?('/.dockerenv') || init_cgroup_is_dockerish? || containerenv_exists? || container_in_env?
end

private
Expand All @@ -19,6 +19,14 @@ def init_cgroup_is_dockerish?
false
end
end

def containerenv_exists?
File.exist?('/run/.containerenv') || File.exist?('/var/run/.containerenv')
end

def container_in_env?
ENV.fetch('container', '') != ''
end
end
end
end
18 changes: 10 additions & 8 deletions lib/berkeley_library/docker/module_info.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# frozen_string_literal: true

module BerkeleyLibrary
module Docker
class ModuleInfo
NAME = 'berkeley_library-docker'.freeze
AUTHOR = 'Dan Schmidt'.freeze
AUTHOR_EMAIL = 'danschmidt5189@berkeley.edu'.freeze
SUMMARY = 'Utility functions for Dockerizing Ruby apps'.freeze
DESCRIPTION = 'Utility functions for making Ruby apps "just work" in Docker containers.'.freeze
LICENSE = 'MIT'.freeze
VERSION = '0.2.0'.freeze
HOMEPAGE = 'https://github.com/BerkeleyLibrary/docker'.freeze
NAME = 'berkeley_library-docker'
AUTHOR = 'Dan Schmidt'
AUTHOR_EMAIL = 'danschmidt5189@berkeley.edu'
SUMMARY = 'Utility functions for Dockerizing Ruby apps'
DESCRIPTION = 'Utility functions for making Ruby apps "just work" in Docker containers.'
LICENSE = 'MIT'
VERSION = '0.3.0.a1'
HOMEPAGE = 'https://github.com/BerkeleyLibrary/ruby-docker'

private_class_method :new
end
Expand Down
52 changes: 29 additions & 23 deletions spec/berkeley_library/docker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
require 'spec_helper'
require 'rails' # require Rails first to mimic load order
require 'fakefs/spec_helpers'
require 'berkeley_library/docker'

module BerkeleyLibrary
describe Docker do
describe '#running_in_container?' do
include FakeFS::SpecHelpers

DOCKERISH_CGROUP = <<~EOL
11:cpuacct,cpu:/docker/12345
10:devices:/docker/12345
Expand Down Expand Up @@ -33,44 +36,47 @@ module BerkeleyLibrary
1:name=systemd:/
EOL

around(:example) do |example|
current_env = ENV.to_hash
begin
ENV.clear
example.run
ensure
ENV.replace(current_env)
end
end

it 'is true when /.dockerenv exists' do
mock_dockerenv
FileUtils.touch('/.dockerenv')
expect(BerkeleyLibrary::Docker.running_in_container?).to be true
end

it 'is true when /proc/1/cgroup is docker-like' do
mock_dockerenv(false)
mock_init_cgroup
FileUtils.mkdir_p('/proc/1')
File.write('/proc/1/cgroup', DOCKERISH_CGROUP)
expect(BerkeleyLibrary::Docker.running_in_container?).to be true
end

it 'is false when /proc/1/cgroup is traditional' do
mock_dockerenv(false)
mock_init_cgroup(false)
expect(BerkeleyLibrary::Docker.running_in_container?).to be false
it 'is true when `container` is present in environment' do
ENV['container'] = 'lxc-libvirt'
expect(BerkeleyLibrary::Docker.running_in_container?).to be true
end

it 'is false when /proc/1/cgroup does not exist' do
mock_dockerenv(false)
expect(File)
.to receive(:open).with('/proc/1/cgroup')
.and_raise(Errno::ENOENT)
expect(BerkeleyLibrary::Docker.running_in_container?).to be false
it 'is true when /run/.containerenv exists' do
FileUtils.mkdir('/run')
FileUtils.touch('/run/.containerenv')
expect(BerkeleyLibrary::Docker.running_in_container?).to be true
end

private
it 'is false when /proc/1/cgroup is traditional' do
FileUtils.mkdir_p('/proc/1')
File.write('/proc/1/cgroup', TRADITIONAL_CGROUP)

def mock_dockerenv(exists = true)
expect(File)
.to receive(:exist?).with('/.dockerenv')
.and_return(exists)
expect(BerkeleyLibrary::Docker.running_in_container?).to be false
end

def mock_init_cgroup(dockerish = true)
expect(File)
.to receive(:open).with('/proc/1/cgroup')
.and_return(
StringIO.new(dockerish ? DOCKERISH_CGROUP : TRADITIONAL_CGROUP))
it 'is false when none of the conditions are true' do
expect(BerkeleyLibrary::Docker.running_in_container?).to be false
end
end
end
Expand Down
Loading