LoginSignup
2
1

More than 3 years have passed since last update.

FROM scratchで作ったような、ログインコマンドの無いdocker containerにログインする

Posted at

はじめに

開発向けでなく本番運用のDocker containerの場合、 FROM scratch のようにshやbashコマンドの無いシンプルなcontainerを利用すると思います。
そんなcontainerに対してログインして中を覗きたい!となることが個人的によくあるので、やり方を調べました。

力技: コマンドが無いならコピーすればいい!

対処は単純で、コマンド実行が可能となるコマンドをコピーして利用すればいい! 今回はホスト: Ubuntu Linux環境で試しました。

  1. コピー対象のコマンドを用意する。 今回は busybox を使用しました。 /bin/busybox にあるとします。
  2. docker container ps でログインしたいdocker containerのIDを確認。 ここでは 0123456789ab とします。
  3. docker cp /bin/busybox 0123456789ab:/ でbusyboxをコピー
  4. docker exec -it 0123456789ab /busybox sh でコピーした busybox を使ってでログイン
$ which busybox
/bin/busybox
$ docker container ps | grep some_keyword
0123456789ab        xxxxxxxxxx "run command"     2 days ago          Up 2 days                                                                                                        
$ docker cp /bin/busybox 0123456789ab:/
$ docker exec -it 0123456789ab /busybox sh


BusyBox v1.22.1 (Ubuntu 1:1.22.0-15ubuntu1.4) built-in shell (ash)
Enter 'help' for a list of built-in commands.

/ # 

これでログインできたので、あとは好きにデバッグしましょう。

終わったら rm で消しましょう

docker exec -it 0123456789ab /busybox rm /busybox

参考

busyboxのコピーを参考にしました: Debugging “FROM scratch” on Kubernetes
docker cp でファイルをホストからコンテナへコピーする

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1