Docker compose tutorial

  1. install docker-compose
    apt install docker-compose
  2. create two dir: Product and website and files as below:
    root@ubunu2004:~/tmp# tree
    .
    ├── docker-compose.yml
    ├── Product
    │   ├── api.py
    │   ├── Dockerfile
    │   └── requirements.txt
    └── website
    └── index.php

    root@ubunu2004:~/Product# cat api.py
    from flask import Flask
    from flask_restful import Resource, Api
    app = Flask(__name__)
    api = Api(app)
    class Product(Resource):
    def get(self):
        return {
            'products': ['Ice cream',
                        'Chocolate',
                        'Eggs',
                        'Fruit']
        }
    api.add_resource(Product, '/')
    if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)
    root@ubunu2004:~/Product# cat requirements.txt
    Flask==0.12
    flask-restful==0.3.5
    root@ubunu2004:~/Product# cat Dockerfile
    FROM python:3-onbuild
    COPY . /usr/src/app
    CMD ["python", "api.py"]
    root@ubunu2004:~/tmp/website# cat index.php
    <html>
    <head>
        <title>My Shop</title>
    </head>
    
    <body>
        <h1>Welcome to my shop</h1>
        <ul>
            <?php
                $json = file_get_contents('http://product-service');
                $obj = json_decode($json);
                $products = $obj->products;
                foreach ($products as $product) {
                    echo "<li>$product</li>";
                }
            ?>
        </ul>
    </body>
    </html>

    create docker-compose.yml:

    version: '3'
    services:
    product-service:
    build: ./Product
    volumes:
      - ./Product:/usr/src/app
    ports:
      - 5001:80
    website:
    image: php:apache
    volumes:
      - ./website:/var/www/html
    ports:
      - 5000:80
    depends_on:
      - product-service

then we can start it:
docker-compose up
or start/stop as daemon:
root@ubunu2004:~# docker-compose up -d
Starting root_product-service_1 … done
Starting root_website_1 … done
root@ubunu2004:~# docker-compose stop
Stopping root_website_1 … done
Stopping root_product-service_1 … done

verify the app on http://192.168.0.43:5000/

One Reply to “Docker compose tutorial”

Leave a Reply

Your email address will not be published. Required fields are marked *