1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Docker】Alpine でデフォルト・シェルを Bash に【ユーザー追加とシェルの指定】

Last updated at Posted at 2022-11-06

Alpine ベースの Docker イメージは、デフォルトのシェルが ash なので、apk add --no-cache bashbash をインストールしたものの、デフォルトを bash にしたい

インストールしたライブラリが bash を前提にしているため、Alpine にユーザーを追加し、デフォルトのシェルを bash にしたいのですが、毎回ググっているので、自分のググラビリティとして。

TL; DR (今北産業)

Dockerfile
FROM alpine

# Install bash
RUN apk add --no-cache bash

# For non-interactive execution, use the system-wide bash profile.
ENV BASH_ENV="/etc/profile"

# Create a user and group for the application to avoid: Dockle CIS-DI-0001
RUN \
    addgroup -S mygroup && \
    adduser -g "myuser" -s /bin/bash -D -S myuser -G mygroup

# change default shell from ash to bash for other users
#RUN sed -i -e "s/bin\/ash/bin\/bash/" /etc/passwd

USER myuser

# Use bash by default
ENTRYPOINT ["/bin/bash"]

なお、公式には /etc/login.defs/etc/default/useradd の作成が記載されていますが、これはユーザー切り替えや Shadow パスワードを使う場合などに必要なものです。しかし、それらは通常の OS 利用、つまり複数ユーザーがログインして作業したりといった場合に必要なものです。

そもそも Docker は「コンテナを起動したらユーザーの切り替えはしない」ものなので、ここでは作成しません。それらが必要な場合は、素直に仮想マシンで Alpine Linux を動かした方が良いと思います。

また、RUN ディレクティブ指示子bash を明示的に使いたい場合は、スクリプト実行前に /bin/bash を指定する。特に、bash を前提としているのに、shebang#!/bin/sh になっているスクリプトで有効です。

COPY myScript.sh /tmp/myScript.sh

RUN /bin/bash /tmp/myScript.sh

所感

「なーんか、動かないな」と、しばらく葛藤したもののスクリプトを覗いてみたら、#!/bin/sh になっているのに function を使って関数定義しているなど、bash を前提としたスクリプトなのでした。とほほほ。

参考文献

1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?