佐藤のメモ帳

Rust, Python, Java, AWS, etc...

gRCPサンプルの実行環境をDockerに移行

はじめに

前回の記事Next Actionだった、gRCPサンプルの実行環境をDockerに変更を行なった。

ソース

github.com

実施

前回の記事ではgRCPサンプルgreeter_server.py, greeter_client.pyをローカル環境で実行していた。 今回はdocker-compose upでそれぞれのDocker環境を用意するようにした。

MacBook-Pro:grpc-test satoukensuke$ docker-compose up
Building server
Step 1/8 : FROM python:3.9.6
3.9.6: Pulling from library/python
4c25b3090c26: Pull complete
1acf565088aa: Pull complete
b95c0dd0dc0d: Pull complete
5cf06daf6561: Pull complete
942374d5c114: Pull complete
64c0f10e4cfa: Pull complete
76571888410b: Pull complete
5e88ca15437b: Pull complete
0ab5ec771994: Pull complete
Digest: sha256:8f642902ba368481c9aca0a100f08daf93793c6fa14d3002253ea3cd210383a7
Status: Downloaded newer image for python:3.9.6
 ---> 1e76b28bfd4e
Step 2/8 : RUN mkdir /service
 ---> Running in 6180a22573cb
Removing intermediate container 6180a22573cb
 ---> 6d58e02c9b7f
Step 3/8 : COPY protobufs/ /service/protobufs/
 ---> 71feba714fdb
Step 4/8 : COPY server/ /service/server/
 ---> 2e6df3c0c61a
Step 5/8 : EXPOSE 50051
 ---> Running in 0bf536a955c8
Removing intermediate container 0bf536a955c8
 ---> b479f119e6c6
Step 6/8 : WORKDIR /service/server/
 ---> Running in a3cbfded4a25
Removing intermediate container a3cbfded4a25
 ---> 2218b025f77b
Step 7/8 : RUN pip install -r requirements.txt
 ---> Running in 7a25d264e921
Collecting grpcio==1.39.0
  Downloading grpcio-1.39.0-cp39-cp39-manylinux2014_x86_64.whl (4.3 MB)
Collecting grpcio-tools==1.39.0
  Downloading grpcio_tools-1.39.0-cp39-cp39-manylinux2014_x86_64.whl (2.5 MB)
Collecting protobuf==3.17.3
  Downloading protobuf-3.17.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.0 MB)
Collecting six>=1.5.2
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Requirement already satisfied: setuptools in /usr/local/lib/python3.9/site-packages (from grpcio-tools==1.39.0->-r requirements.txt (line 2)) (57.4.0)
Installing collected packages: six, protobuf, grpcio, grpcio-tools
Successfully installed grpcio-1.39.0 grpcio-tools-1.39.0 protobuf-3.17.3 six-1.16.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Removing intermediate container 7a25d264e921
 ---> acd83dc0df0c
Step 8/8 : RUN python -m grpc_tools.protoc -I ../protobufs --python_out=.            --grpc_python_out=. ../protobufs/helloworld.proto
 ---> Running in 3578fd3820ac
Removing intermediate container 3578fd3820ac
 ---> 8139333794d2

Successfully built 8139333794d2
Successfully tagged grpc-test_server:latest
WARNING: Image for service server was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building client
Step 1/8 : FROM python:3.9.6
 ---> 1e76b28bfd4e
Step 2/8 : RUN mkdir /service
 ---> Using cache
 ---> 6d58e02c9b7f
Step 3/8 : COPY protobufs/ /service/protobufs/
 ---> Using cache
 ---> 71feba714fdb
Step 4/8 : COPY client/ /service/client
 ---> 0d9754f91b51
Step 5/8 : EXPOSE 50050
 ---> Running in 8dc638c154be
Removing intermediate container 8dc638c154be
 ---> a9ac09980164
Step 6/8 : WORKDIR /service/client
 ---> Running in fe0b3167571f
Removing intermediate container fe0b3167571f
 ---> db37ff46721b
Step 7/8 : RUN pip install -r requirements.txt
 ---> Running in fbf3083c440d
Collecting grpcio==1.39.0
  Downloading grpcio-1.39.0-cp39-cp39-manylinux2014_x86_64.whl (4.3 MB)
Collecting grpcio-tools==1.39.0
  Downloading grpcio_tools-1.39.0-cp39-cp39-manylinux2014_x86_64.whl (2.5 MB)
Collecting protobuf==3.17.3
  Downloading protobuf-3.17.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.0 MB)
Collecting six>=1.5.2
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Requirement already satisfied: setuptools in /usr/local/lib/python3.9/site-packages (from grpcio-tools==1.39.0->-r requirements.txt (line 2)) (57.4.0)
Installing collected packages: six, protobuf, grpcio, grpcio-tools
Successfully installed grpcio-1.39.0 grpcio-tools-1.39.0 protobuf-3.17.3 six-1.16.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Removing intermediate container fbf3083c440d
 ---> 47c38bd90bc0
Step 8/8 : RUN python -m grpc_tools.protoc -I ../protobufs --python_out=.            --grpc_python_out=. ../protobufs/helloworld.proto
 ---> Running in 41b34a563bde
Removing intermediate container 41b34a563bde
 ---> 598956e4f281

Successfully built 598956e4f281
Successfully tagged grpc-test_client:latest
WARNING: Image for service client was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating grpc-test_server_1 ... done
Creating grpc-test_client_1 ... done
Attaching to grpc-test_server_1, grpc-test_client_1
server_1  | Python 3.9.6 (default, Aug 17 2021, 15:37:33) 
server_1  | [GCC 10.2.1 20210110] on linux
server_1  | Type "help", "copyright", "credits" or "license" for more information.
client_1  | Python 3.9.6 (default, Aug 17 2021, 15:37:33) 
client_1  | [GCC 10.2.1 20210110] on linux
client_1  | Type "help", "copyright", "credits" or "license" for more information.

サーバ用コンテナに乗り込み、greeter_server.pyを実行。

MacBook-Pro:~ satoukensuke$ docker ps
CONTAINER ID   IMAGE              COMMAND     CREATED          STATUS          PORTS                      NAMES
0e70af7f1bb9   grpc-test_server   "python3"   16 minutes ago   Up 16 minutes   0.0.0.0:50051->50051/tcp   grpc-test_server_1
c9484035278e   grpc-test_client   "python3"   16 minutes ago   Up 16 minutes   0.0.0.0:50050->50050/tcp   grpc-test_client_1
MacBook-Pro:~ satoukensuke$ docker exec -it grpc-test_server_1 /bin/bash
root@SERVER:/service/server# python greeter_server.py 

クライアント用コンテナに乗り込み、greeter_client.pyを実行。 サンプルと同じ結果が返却され、gRPCで通信できていることを確認できた。

MacBook-Pro:~ satoukensuke$ docker exec -it grpc-test_client_1 /bin/bash
root@CLIENT:/service/client# python greeter_client.py 
Greeter client received: Hello, you!