LoginSignup
0
1

More than 5 years have passed since last update.

【Docker】公式チュートリアルをやってみた 【Part3】

Last updated at Posted at 2017-06-09

はじめに

環境

  • VirtualBox 5.1.22 r115126
    • ポートフォワーディング設定でアクセスできるようにしています
  • Ubuntu16.04.2 LTS
  • Docker version 17.03.1-ce, build c6d412e
    • Dockerのインストールについては公式手順に沿って実施しております

チュートリアル

Part 3: Services

docker-compose.ymlの作成

docker-compose.yml
version: "3"
services:
  web:
    # Part2で作成したdocker cloudのイメージを使用
    image: tocyuki/repository:tag
    deploy:
      # 5台作成する
      replicas: 5
      resources:
        limits:
          # 最大10%までのCPU利用
          cpus: "0.1"
          # 最大50MBまでのメモリ利用
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      # ホストの80番ポートをコンテナの80番ポートへマッピング
      - "80:80"
    networks:
      - webnet
networks:
  webnet:

docker swarm initの実施

  • このコマンドを実行することで、実行したホストをdocker swarmのマネージャーホストとすることができる
tocyuki@vm01:~/docker_tutorial/part3$ docker swarm init
Swarm initialized: current node (mfb7mslrjr6qbkkzi42x9525p) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join \
    --token SWMTKN-1-3562frmookuker51re8lelyaewh1c37g690yodforc13gxhby6-3gntbpftwe8br3h225i5ew7ul \
    10.0.2.15:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

アプリの実行

  • getstartedlabという名前でアプリをswarmモードで実行
tocyuki@vm01:~/docker_tutorial/part3$ docker stack deploy -c docker-compose.yml getstartedlab
Creating network getstartedlab_webnet
Creating service getstartedlab_web

コンテナの起動を確認

コマンドラインでの確認

tocyuki@vm01:~/docker_tutorial/part3$ docker stack ps getstartedlab
ID            NAME                 IMAGE                   NODE  DESIRED STATE  CURRENT STATE          ERROR  PORTS
msazmhxdrs9z  getstartedlab_web.1  tocyuki/repository:tag  vm01  Running        Running 4 seconds ago
4bz3usxe0fpa  getstartedlab_web.2  tocyuki/repository:tag  vm01  Running        Running 4 seconds ago
js4j0zhxaydu  getstartedlab_web.3  tocyuki/repository:tag  vm01  Running        Running 5 seconds ago
5rdhkuc5xkns  getstartedlab_web.4  tocyuki/repository:tag  vm01  Running        Running 4 seconds ago
p3tqe5lhu9zg  getstartedlab_web.5  tocyuki/repository:tag  vm01  Running        Running 4 seconds ago
tocyuki@vm01:~/docker_tutorial/part3$ docker ps
CONTAINER ID        IMAGE                                                                                        COMMAND             CREATED             STATUS              PORTS               NAMES
49ffec8732f5        tocyuki/repository@sha256:1ae82dcb5528f752c9c1fc57a55ec0dde097ee2b119011ed2d99b3b9e81228d8   "python app.py"     3 minutes ago       Up 3 minutes        80/tcp              getstartedlab_web.4.5rdhkuc5xknsyeru01160pang
7f105ebe27ed        tocyuki/repository@sha256:1ae82dcb5528f752c9c1fc57a55ec0dde097ee2b119011ed2d99b3b9e81228d8   "python app.py"     3 minutes ago       Up 3 minutes        80/tcp              getstartedlab_web.5.p3tqe5lhu9zgh1atrwe7svk59
485d12957ac6        tocyuki/repository@sha256:1ae82dcb5528f752c9c1fc57a55ec0dde097ee2b119011ed2d99b3b9e81228d8   "python app.py"     3 minutes ago       Up 3 minutes        80/tcp              getstartedlab_web.1.msazmhxdrs9z75379ojwkgmgj
a55408bffb42        tocyuki/repository@sha256:1ae82dcb5528f752c9c1fc57a55ec0dde097ee2b119011ed2d99b3b9e81228d8   "python app.py"     3 minutes ago       Up 3 minutes        80/tcp              getstartedlab_web.3.js4j0zhxaydun9wwv9xqtew5c
c2d0e23af466        tocyuki/repository@sha256:1ae82dcb5528f752c9c1fc57a55ec0dde097ee2b119011ed2d99b3b9e81228d8   "python app.py"     3 minutes ago       Up 3 minutes        80/tcp              getstartedlab_web.2.4bz3usxe0fpaxsy0q9qi3ai53

WEBブラウザからの確認

  • 更新していくと、各コンテナに負荷分散されているのがわかる

WS000517.png
WS000518.png
WS000519.png
WS000520.png
WS000521.png

アプリの終了

tocyuki@vm01:~/docker_tutorial/part3$ docker stack rm getstartedlab
Removing service getstartedlab_web
Removing network getstartedlab_webnet

Part3で学習したコマンドのチートシート

# List all running applications on this Docker host
docker stack ls      

# Run the specified Compose file        
docker stack deploy -c <composefile> <appname>  

# List the services associated with an app
docker stack services <appname>    

# List the running containers associated with an app   
docker stack ps <appname>   

# Tear down an application
docker stack rm <appname>                             
0
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
0
1