ksaitoの日記

日々試したことの覚え書き

docker runすると/bin/bash: /bin/bash: cannot execute binary fileが出る場合の対処

コンテナのデバッグで下記のコマンドをよく使います。

$ docker run -it --rm <イメージ> /bin/sh

zabbixのオフシャルイメージの幾つかで下記のようなエラーがでてしばらく悩みました。

$ docker run -it --rm zabbix/zabbix-agent:alpine-3.2-latest /bin/sh
/bin/sh: /bin/sh: cannot execute binary file
$

次のように実行することでエラーを回避出来ます。

$ docker run -it --rm zabbix/zabbix-agent:alpine-3.2-latest -i
bash-4.3#

原因と再現手順

オフシャルイメージのDockerfileを見ると、ENTRYPOINTCMDは、下記のように書かれています。

$ tail -3 Dockerfile
ENTRYPOINT ["/bin/bash"]

CMD ["/run_zabbix_component.sh", "agentd", "none"]
$

5. CMD and ENTRYPOINT better togetherENTRYPOINTCMDを併用することでコンテナ化したバイナリのアプリケーションをENTRYPOINTに指定して、CMDにデフォルトの振る舞いに必要なオプションを指定するというベストプラクティスが紹介されています。

今回のケースでは、ENTRYPOINT/bin/bashが指定されていることで引数に指定した/bin/bashシェルスクリプトと想定して実行しようとしたためにエラーとなりました。

再現してみると下記のようなことが発生していることになります。-iオプションは、bashの対話的な起動オプションでCMDに指定されたシェルを無効にするためのワークアラウンドです。

$ docker run -it --rm zabbix/zabbix-agent:alpine-3.2-latest /bin/bash
/bin/bash: /bin/bash: cannot execute binary file
$ docker run -it --rm zabbix/zabbix-agent:alpine-3.2-latest -i
bash-4.3# /bin/bash /bin/bash
/bin/bash: /bin/bash: cannot execute binary file
bash-4.3#

ENTRYPOINTにシェル以外のバイナリが指定されているコンテナの場合は、下記のような別解で対応します。

$ docker run --entrypoint /bin/sh -it --rm zabbix/zabbix-agent:alpine-3.2-latest
/var/lib/zabbix #

これで、Dockerfileの設定にかかわらず内容を確認できるようになりました。

Dockerコンテナでdebconfのdialogを使う

Dockerコンテナでdialogを使ってインタラクティブに設定をするための設定です。

コンテナからdebconf実行

特別何もしないとtextで入力を求められます。

sudo docker run -it --rm -e TZ=Asia/Tokyo -e LANG=ja_JP.UTF-8 ubuntu:16.10 bash
root@b1af411dee00:/mnt# dpkg-reconfigure debconf
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
Configuring debconf
-------------------

設定に debconf
を用いるパッケージは、共通のルック&フィールを用います。どの種類のユーザインターフェイスを用いるかを選んでください。

「ダイアログ」は全画面の文字ベースのインターフェイスです。「readline」はより伝統的なプレーンテキストのインターフェイスです�
��「gnome」と「kde」は近代的な X のインターフェイスで、それぞれのデスクトップに適しています (ほかの X
環境で利用することもできます)。「エディタ」を用いるとあなたの好きなテキストエディタを用いることができます。「非対話的」�
�選ぶとまったく質問をしなくなります。

  1. ダイアログ  2. Readline  3. Gnome  4. Kde  5. エディタ  6. 非対話的
利用するインターフェイス: 

whiptailをインストールして再実行

whiptailは、newtというテキストベースのウィンドウツールです。

これがないので、利用できるtextが選択されたのですね。

再実行するとおなじみのダイアログが表示されました。

root@b1af411dee00:/mnt# apt-get install whiptail
root@b1af411dee00:/mnt# dpkg-reconfigure debconf
  ┌────────────────────────────────────────────────────────────────────────────┤ Configuring debconf ├────────────────────────────────────────────────────────────────────────────┐  
  │ 設定に debconf                                                                                                                                                                │  
  │ を用いるパッケージは、共通のルック&フィールを用います。どの種類のユーザインターフェイスを用いるかを選んでください<E3>                                                        │  
  │ <80><82>                                                                                                                                                                      │  
  │                                                                                                                                                                               │  
  │ 「ダイアログ」は全画面の文字ベースのインターフェイスです。「readline」はより伝統的なプレーンテキストのインターフェイ<E3><82>                                                  │  
  │ <B9>です。「gnome」と「kde」は近代的な X のインターフェイスで、それぞれのデスクトップに適しています (ほかの X                                                                 │  
  │ 環境で利用することもできます)。「エディタ」を用いるとあなたの好きなテキストエディタを用いることができます。「非対話                                                           │  
  │ 的」を選ぶとまったく質問をしなくなります。                                                                                                                                    │  
  │                                                                                                                                                                               │  
  │ 利用するインターフェイス:                                                                                                                                                     │  
  │                                                                                                                                                                               │  
  │                                                                                  ダイアログ                                                                                   │  
  │                                                                                  Readline                                                                                     │  
  │                                                                                  Gnome                                                                                        │  
  │                                                                                  Kde                                                                                          │  
  │                                                                                  エディタ                                                                                     │  
  │                                                                                  非対話的                                                                                     │  
  │                                                                                                                                                                               │  
  │                                                                                                                                                                               │  
  │                                                     <了解>                                                       <取消>                                                       │  
  │                                                                                                                                                                               │  
  └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘  

後続のダイアログです。debconfは、ソフトウェアをパッケージして柔軟に設定/再設定をするための非常に優れた仕組みなので使わない手はないですね。

                   ┌──────────────────────────────────────────────────────────┤ Configuring debconf ├───────────────────────────────────────────────────────────┐                    
                   │ あなたが答えたい質問のうち、最低の優先度のものを選択してください。                                                                         │                    
                   │  - 「重要」は、ユーザが介在しないとシステムを破壊しかねないような項目用です。                                                              │                    
                   │    あなたが初心者か、あるいは急いでいるのであればこれを選んでください。                                                                    │                    
                   │  - 「高」は、適切なデフォルトの回答がないような項目用です。                                                                                │                    
                   │  - 「中」は、適切なデフォルトの回答があるような普通の項目用です。                                                                          │                    
                   │  - 「低」は、ほとんどの場合にデフォルトの回答でかまわないような、ささいな項目用です。                                                      │                    
                   │                                                                                                                                            │                    
                   │                                                                                                                                            │                    
                   │ 注意: ここで何を選択しても、以前に行った質問は dpkg-reconfigure プログラムを使用して表示できます。                                         │                    
                   │                                                                                                                                            │                    
                   │ より低い優先度の質問を無視:                                                                                                                │                    
                   │                                                                                                                                            │                    
                   │                                                                    重要                                                                    │                    
                   │                                                                    高                                                                      │                    
                   │                                                                    中                                                                      │                    
                   │                                                                    低                                                                      │                    
                   │                                                                                                                                            │                    
                   │                                                                                                                                            │                    
                   │                                         <了解>                                           <取消>                                            │                    
                   │                                                                                                                                            │                    
                   └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘      

GNU screenの日本語環境でncursesを使うと表示が乱れる

GNU screenの日本語環境でncursesを使うと表示が乱れます。

環境

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:        16.04
Codename:       xenial
$ screen -v
Screen version 4.03.01 (GNU) 28-Jun-15
$ echo $LANG
ja_JP.UTF-8
$ 

表示が乱れる

screenを起動してncursesのサンプルを動かすと表示が乱れます。

サンプルは、こちらのコードを使わせていただきました。

$ screen
$ ./curwin1
                            ┌─────────────────────┐
     │   Move the window   │
   │ with the arrow keys │
 │     or HOME/END     │
│                    │
│  Press 'q' to quit  │
└─────────────────────┘

GNU screenの設定

下記の設定を`.screenrcに追加します。

$ tail -1 ~/.screenrc 
cjkwidth off
$

screenを再起動すると、きれいに表示されるようになりました。

日本語表示/入力とも問題ないようです。今のところ副作用はないです。

$ screen
$ ./curwin1

                            ┌─────────────────────┐
                            │   Move the window   │
                            │ with the arrow keys │
                            │     or HOME/END     │
                            │                     │
                            │  Press 'q' to quit  │
                            └─────────────────────┘

dockerでgnu screenを使う

dockerコンテナでGNU screenを使えるようにしてみました。

環境

試した環境は、以下の通りです。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:    16.04
Codename:   denial
$ docker --version
Docker version 1.12.2, build bb80604
$ sudo docker run -it -d ubuntu:16.04 bash
$

screenのインストールと起動

コンテナに接続してscreenを起動するとttyがないので起動できません。

$ sudo docker exec -it ecstatic_williams bash
root@d82a18f67359:/# apt update && apt install -y screen
root@d82a18f67359:/# screen
Must be connected to a terminal.
root@d82a18f67359:/# tty
not a tty
root@d82a18f67359:/# 

ttyを設定

ngettyパッケージでgettyttyを設定します。

root@d82a18f67359:/# apt install -y ngetty
root@d82a18f67359:/# useradd -m test
root@d82a18f67359:/# passwd test
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@d82a18f67359:/# getty tty 

Ubuntu 16.04.1 LTS d82a18f67359 tty

d82a18f67359 login: test
Password: 
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-45-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

test@d82a18f67359:~$ tty
/dev/tty
test@d82a18f67359:~$ 

ttyが設定されたのでscreenも無事起動しました。

test@d82a18f67359:~$ screen
Screen version 4.03.01 (GNU) 28-Jun-15

Copyright (c) 2010 Juergen Weigert, Sadrul Habib Chowdhury
Copyright (c) 2008, 2009 Juergen Weigert, Michael Schroeder, Micah Cowan, Sadrul Habib Chowdhury
Copyright (c) 1993-2002, 2003, 2005, 2006, 2007 Juergen Weigert, Michael Schroeder
Copyright (c) 1987 Oliver Laumann

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either
version 3, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program (see the file COPYING); if not, see http://www.gnu.org/licenses/, or contact Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA.

Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to screen-devel@gnu.org


Capabilities:
+copy +remote-detach +power-detach +multi-attach +multi-user +font +color-256 +utf8 +rxvt +builtin-telnet

DockerのGUIをMacで使う

DockerコンテナのGUIアプリ(といってもxterm)をMacから使ってみました。

Quartz 2.7.11のインストール

QuartzからMacにダウンロードしてインストールします。

Quartzの設定

X11 - 環境設定でネットワークク・ライアントからの接続を許可をチェックしてX11を再起動する。 f:id:ksaito11:20161130211256p:plain

Quartzを起動するとxtermが起動するので下記のコマンドを実行してリモートからの接続を許可します。

$ xhost +

Dockerからの接続

コンテナにxtermをインストールして、下記のように実行します。

root@6497ba135950:/# useradd -m user
root@6497ba135950:/# su - user
user@cba17df706a0:~$ export DISPLAY=<MacのIPアドレス>:0
user@cba17df706a0:~$ xterm

これで、Dockerコンテナで実行したxtermがMac側に表示されます。

以上

既存のDebianパッケージの展開

既存のDebianパッケージを参考にするためにダウンロードして展開する方法です。

下記のコマンドで作成したディレクトリに既存パッケージの完成形が展開されます。

apt-get download mysql-server-5.7
mkdir mysql
dpkg-deb --raw-extract mysql-server-5.7_5.7.16-0ubuntu0.16.04.1_amd64.deb mysql