whalebrewを使うとDockerイメージをネイティブのコマンドのように扱うことができる。

インストール

今回はHomebrewを用いてmacOSにインストールする。

$ brew install whalebrew

whalesayを使ってみる

DockerイメージをPullし、コマンドを生成する。

$ whalebrew install mailhog/mailhog:v1.0.1

実行する。

bash-5.1$ whalesay cool
 ______
< cool >
 ------
    \
     \
      \
                    ##         .
              ## ## ##        ==
           ## ## ## ## ##    ===
       /"""""""""""""""""\___/ ===
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
       \______ o           __/
         \    \         __/
          \____\_______/

MailHogを使う

DockerイメージをPullし、コマンドを生成する。

$ whalebrew install mailhog/mailhog:v1.0.1

ポートマッピングの設定を行う。

#!/usr/bin/env whalebrew
image: mailhog/mailhog:v1.0.1
ports:
 - "1025:1025"
 - "8025:8025"
/usr/local/bin/mailhog

起動する。

bash-5.1$ mailhog
2021/02/28 04:03:44 Using in-memory storage
2021/02/28 04:03:44 [SMTP] Binding to address: 0.0.0.0:1025
[HTTP] Binding to address: 0.0.0.0:8025
2021/02/28 04:03:44 Serving under http://0.0.0.0:8025/
Creating API v1 with WebPath:
Creating API v2 with WebPath:

ローカルからhttp://0.0.0.0:8025/にアクセスできる。

localstackを使う

DockerイメージをPullし、コマンドを生成する。

bash-5.1$ whalebrew install localstack/localstack:0.12.6
Unable to find image 'localstack/localstack:0.12.6' locally
0.12.6: Pulling from localstack/localstack
de021691e653: Pull complete
Digest: sha256:7ea76feaa8538a9cbd54cc25fc9157b3c1fd6acbdce9ee6873a0d88a07585dd5
Status: Downloaded newer image for localstack/localstack:0.12.6
docker.io/localstack/localstack:0.12.6
🐳  Installed localstack/localstack:0.12.6 to /usr/local/bin/localstack

起動してみると、コンテナのユーザーIDを維持しないとPermissionErrorが発生する。

bash-5.1$ localstack
Waiting for all LocalStack services to be ready
2021-02-28 06:03:58,653 INFO supervisord started with pid 15
2021-02-28 06:03:59,663 INFO spawned: 'dashboard' with pid 21
2021-02-28 06:03:59,668 INFO spawned: 'infra' with pid 22
supervisor: couldn't setuid to 1001: Can't drop privilege as nonroot user
supervisor: child process was not spawned
2021-02-28 06:03:59,679 INFO success: dashboard entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
2021-02-28 06:03:59,684 INFO exited: dashboard (exit status 127; not expected)
(. .venv/bin/activate; exec bin/localstack start --host)
2021-02-28 06:04:00,689 INFO success: infra entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
Starting local dev environment. CTRL-C to quit.
LocalStack version: 0.12.6
Waiting for all LocalStack services to be ready
Error starting infrastructure: [Errno 13] Permission denied: '/opt/code/localstack/localstack/infra/dynamodb/log4j2.xml' Traceback (most recent call last):
  File "/opt/code/localstack/localstack/services/infra.py", line 383, in start_infra
    thread = do_start_infra(asynchronous, apis, is_in_docker)
  File "/opt/code/localstack/localstack/services/infra.py", line 455, in do_start_infra
    prepare_installation()
  File "/opt/code/localstack/localstack/utils/analytics/profiler.py", line 156, in wrapped
    return f(*args, **kwargs)
  File "/opt/code/localstack/localstack/services/infra.py", line 430, in prepare_installation
    install.install_components(apis)
  File "/opt/code/localstack/localstack/services/install.py", line 246, in install_components
    parallelize(install_component, names)
  File "/opt/code/localstack/localstack/utils/common.py", line 1437, in parallelize
    result = pool.map(func, list)
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 364, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 768, in get
    raise self._value
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 48, in mapstar
    return list(map(*args))
  File "/opt/code/localstack/localstack/services/install.py", line 242, in install_component
    installer()
  File "/opt/code/localstack/localstack/services/install.py", line 203, in install_dynamodb_local
    save_file(log4j2_file, log4j2_config)
  File "/opt/code/localstack/localstack/utils/common.py", line 930, in save_file
    with open(file, mode) as f:
PermissionError: [Errno 13] Permission denied: '/opt/code/localstack/localstack/infra/dynamodb/log4j2.xml'

コンテナのIDを引き継ぐように設定する。合わせてポートマッピングの設定を行う。

#!/usr/bin/env whalebrew
image: localstack/localstack:0.12.6
keep_container_user: true
ports:
  - "4566:4566"
  - "4571:4571"
  - "8080:8080"
/usr/local/bin/localstack

そうすると正しく起動できる。

bash-5.1$ localstack
Waiting for all LocalStack services to be ready
2021-02-28 06:17:22,481 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
2021-02-28 06:17:22,494 INFO supervisord started with pid 15
2021-02-28 06:17:23,502 INFO spawned: 'dashboard' with pid 21
2021-02-28 06:17:23,507 INFO spawned: 'infra' with pid 22
2021-02-28 06:17:23,520 INFO success: dashboard entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
2021-02-28 06:17:23,522 INFO exited: dashboard (exit status 0; expected)
(. .venv/bin/activate; exec bin/localstack start --host)
2021-02-28 06:17:24,505 INFO success: infra entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
Starting local dev environment. CTRL-C to quit.
LocalStack version: 0.12.6
Waiting for all LocalStack services to be ready
Starting edge router (https port 4566)...
Starting mock ACM service on http port 4566 ...
Starting mock API Gateway service on http port 4566 ...
Starting mock CloudFormation service on http port 4566 ...
Starting mock CloudWatch service on http port 4566 ...
Starting mock DynamoDB service on http port 4566 ...
Starting mock DynamoDB Streams service on http port 4566 ...
Starting mock EC2 service on http port 4566 ...
Starting mock ES service on http port 4566 ...
Starting mock Firehose service on http port 4566 ...
Starting mock IAM service on http port 4566 ...
Starting mock STS service on http port 4566 ...
Starting mock Kinesis service on http port 4566 ...
Starting mock KMS service on http port 4566 ...
2021-02-28T06:17:29:INFO:localstack.multiserver: Starting multi API server process on port 34307
[2021-02-28 06:17:29 +0000] [23] [INFO] Running on https://0.0.0.0:4566 (CTRL + C to quit)
2021-02-28T06:17:29:INFO:hypercorn.error: Running on https://0.0.0.0:4566 (CTRL + C to quit)
[2021-02-28 06:17:29 +0000] [23] [INFO] Running on http://0.0.0.0:34307 (CTRL + C to quit)
2021-02-28T06:17:29:INFO:hypercorn.error: Running on http://0.0.0.0:34307 (CTRL + C to quit)
Starting mock Lambda service on http port 4566 ...
Starting mock CloudWatch Logs service on http port 4566 ...
Starting mock Redshift service on http port 4566 ...
Starting mock Route53 service on http port 4566 ...
Starting mock S3 service on http port 4566 ...
Starting mock Secrets Manager service on http port 4566 ...
Starting mock SES service on http port 4566 ...
Starting mock SNS service on http port 4566 ...
Starting mock SQS service on http port 4566 ...
Starting mock SSM service on http port 4566 ...
Starting mock Cloudwatch Events service on http port 4566 ...
Starting mock StepFunctions service on http port 4566 ...
Waiting for all LocalStack services to be ready
Ready.
2021-02-28T06:17:42:INFO:localstack.utils.analytics.profiler: Execution of "start_api_services" took 13098.462104797363ms