Skip to content

Instantly share code, notes, and snippets.

@yuuan
Last active December 2, 2020 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuuan/1953e2487981253490fd812b8ea5b331 to your computer and use it in GitHub Desktop.
Save yuuan/1953e2487981253490fd812b8ea5b331 to your computer and use it in GitHub Desktop.
$ docker run --rm -it -v $PWD/:/mnt -w /mnt ruby:2.7.2-alpine3.12 irb
irb(main):001:0> source = Tempfile.new('source')
=> #<Tempfile:/tmp/source20201202-1-1xbulh>
irb(main):002:0> FileUtils.copy_file('/etc/group', source.path)
=> nil
irb(main):003:0> source.size
=> 682
irb(main):004:0> dist = Tempfile.new('dist')
=> #<Tempfile:/tmp/dist20201202-1-11g4dcm>
irb(main):005:0> IO.copy_stream(source, dist)
=> 682

コンテナの中のファイルをコピーするとちゃんとコピーできるが

$ echo HOGE > hoge.txt
$ docker run --rm -it -v $PWD/:/mnt -w /mnt ruby:2.7.2-alpine3.12 irb
irb(main):001:0> source = Tempfile.new('source')
=> #<Tempfile:/tmp/source20201202-1-1snxz99>
irb(main):002:0> FileUtils.copy_file('hoge.txt', source.path)
=> nil
irb(main):003:0> source.size
=> 5
irb(main):004:0> dist = Tempfile.new('dist')
=> #<Tempfile:/tmp/dist20201202-1-5npy3i>
irb(main):005:0> IO.copy_stream(source, dist)
=> 0
irb(main):006:0> dist.size
=> 0

コンテナの外のファイルだとコピーできない。(サイズが 0 になる)

irb(main):007:0> FileUtils.touch(source.path)
=> ["/tmp/source20201202-1-zu0miw"]
irb(main):008:0> dist = Tempfile.new('dist')
=> #<Tempfile:/tmp/dist20201202-1-15elbxt>
irb(main):009:0> IO.copy_stream(source, dist)
=> 5

だがなぜか、touch とか chmod u+r とかすると、コピーできるようになる。 上の例では Ruby から touch している。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment