3
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.

PythonからDockerコンテナを起動しAlexaをしゃべらせる

Last updated at Posted at 2023-05-05

概要

過去作成した、AlexaをCLIからしゃべらせるDockerコンテナを、Pythonから実行する場合の手順です。

実装

ライブラリのインストール

下記のコマンドでDocker SDK for Pythonをインストールする

pip install docker

また記事執筆時点(2023/05/05現在)だと、pip install dockerを実行するとrequestsライブラリのバージョンが2.30だが、その場合Dockerホストとの接続に失敗し、下記のようなエラーが出てしまう.

Traceback (most recent call last):
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/docker/api/client.py", line 214, in _retrieve_server_version
    return self.version(api_version=False)["ApiVersion"]
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/docker/api/daemon.py", line 181, in version
    return self._result(self._get(url), json=True)
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/docker/utils/decorators.py", line 46, in inner
    return f(self, *args, **kwargs)
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/docker/api/client.py", line 237, in _get
    return self.get(url, **self._set_request_timeout(kwargs))
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/requests/sessions.py", line 600, in get
    return self.request("GET", url, **kwargs)
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/requests/adapters.py", line 486, in send
    resp = conn.urlopen(
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/urllib3/connectionpool.py", line 790, in urlopen
    response = self._make_request(
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/urllib3/connectionpool.py", line 496, in _make_request
    conn.request(
TypeError: HTTPConnection.request() got an unexpected keyword argument 'chunked'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/sey323/Workspace/programs/tmp/python-alexa-speech/python_alexa_speech/main.py", line 3, in <module>
    client = docker.DockerClient("unix:///var/run/docker.sock", version='auto',)
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/docker/client.py", line 45, in __init__
    self.api = APIClient(*args, **kwargs)
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/docker/api/client.py", line 197, in __init__
    self._version = self._retrieve_server_version()
  File "/Users/sey323/.anyenv/envs/pyenv/versions/python-alexa-speech/lib/python3.10/site-packages/docker/api/client.py", line 221, in _retrieve_server_version
    raise DockerException(
docker.errors.DockerException: Error while fetching server API version: HTTPConnection.request() got an unexpected keyword argument 'chunked'

そこで下記のIssueに従って、requestsのバージョンをrequests<2.2.29にダウングレードする。

pip install requests==2.29.0 

AlexaをしゃべらせるDockerイメージの作成

再掲にはなるが記事を参考に、AlexaをしゃべらせるDockerイメージを作成する。

上記の記事に従って、下記のコマンドで対象のAlexaが発言できれば前準備は完了

docker run --rm -t arc-cmd -d "リビング の Echo Plus" -e "speak: テスト"

実行

上記のコマンドをPythonから実行する。

main.py
import docker

client = docker.from_env()
client.containers.run("arc-cmd", '-d "リビング の Echo Plus" -e "speak: テスト"')

docker runコマンドで実行した場合と同様に対処のAlexaが発言すれば動作OK

終わりに

docker sdkを利用することで、コマンド化したAlexa SpeechをPythonから簡単に実行できるようになったので、API化などがしやすくなった。

しかしGoogle Homeと違って、Alexaを自発的に喋らせる方法が回りくどすぎるので、早く公式がライブラリ作って欲しい...

3
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
3
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?