# IoT Lab 5 Docker Container ## Set up a Docker environment. ### Print all local docker images ![截屏2023-11-23 下午8.13.48](https://hackmd.io/_uploads/BkC7E6bST.png) ### Run a ‘hello-world’ container in the docker ![截屏2023-11-23 下午8.12.12](https://hackmd.io/_uploads/HJjz4p-H6.png) ### Print all running docker process status ![截屏2023-11-27 下午3.28.07](https://hackmd.io/_uploads/HJAmSp-Sa.png) ### Print all docker process status ![截屏2023-11-23 下午8.15.43](https://hackmd.io/_uploads/HyOBSTbSp.png) ## Run a Webapp with Docker. Run the `docker run` command to publish ports in detached mode ![截屏2023-11-23 下午8.24.36](https://hackmd.io/_uploads/ByE6STWB6.png) See the ports ![截屏2023-11-23 下午8.25.32](https://hackmd.io/_uploads/HyAaSpWBp.png) Open http://localhost:32769 in browser ![截屏2023-11-23 下午8.27.29](https://hackmd.io/_uploads/B1cABpbS6.png) ## Make your first Docker image ### Trouble [I just saw the hints in lab handout after resolving the error:(] Encountered the `werkzeug not found` error, which is caused by Flask not specifying the dependency correctly and installed Werkzeug 3.0.0 which Flask 2.0.2 not support. ![截屏2023-11-28 上午1.36.10](https://hackmd.io/_uploads/HyHj78MB6.png) ### Troubleshooting Specify `Werkzeug==2.2.2` in the `requirements.txt` ![截屏2023-11-28 上午1.47.50](https://hackmd.io/_uploads/S1XgwUMS6.png) Delete the old image. ![截屏2023-11-28 上午1.41.17](https://hackmd.io/_uploads/Bk7CN8zSp.png) Rebuild it. ![截屏2023-11-28 上午1.53.02](https://hackmd.io/_uploads/B1C5D8Gr6.png) Image successfully running. ![截屏2023-11-28 上午1.54.41](https://hackmd.io/_uploads/HkF-_IfHT.png) Cat gif display on server. ![截屏2023-11-28 上午1.45.01](https://hackmd.io/_uploads/r11NDIMBp.png) ![截屏2023-11-28 上午1.57.03](https://hackmd.io/_uploads/HJHKuIzHT.png) ## Multi-container environment ### Run FoodTrucks Able to send a request to the Elasticsearch container ![截屏2023-11-28 上午2.13.22](https://hackmd.io/_uploads/BJsU2IfST.png) Show that we talk to ES on `172.17.0.2:9200` ![截屏2023-11-28 上午2.22.37](https://hackmd.io/_uploads/H1dcRIGSp.png) ![截屏2023-11-28 上午2.29.08](https://hackmd.io/_uploads/Hk97bwGHa.png) ![截屏2023-11-28 上午2.31.17](https://hackmd.io/_uploads/r1GEbwMBa.png) ![截屏2023-11-28 上午2.37.44](https://hackmd.io/_uploads/BkPbMvGB6.png) ### How a microservice architecture is implemented #### How the Docker Network work Docker Network allows custom networks, and multiple containers can be attached to the custom netwotk by utilizing the `--net` flag in the `docker run` command. It also allows containers to communicate with each other and establish connections to the Internet. In FoodTrucks app, two containers - one running the Flask process and another running the Elasticsearch (ES) process are running in the same user-defined network so that they are able to communicate each other. #### How the Docker Compose work Compose is a tool for defining and running multi-container Docker applications. With `docker-compose.yaml` that configures application's services, it enables the easy launch of an application with several containers through command `docker-compose up`. `docker-compose.yaml` of FoodTrucks app ``` version: "3" services: es: image: docker.elastic.co/elasticsearch/elasticsearch:6.3.2 container_name: es environment: - discovery.type=single-node ports: - 9200:9200 volumes: - esdata1:/usr/share/elasticsearch/data web: image: wzr1123/foodtrucks-web command: python3 app.py depends_on: - es ports: - 5000:5000 volumes: - ./flask-app:/opt/flask-app volumes: esdata1: driver: local ``` Then by running `docker-compose up` under `FoodTrucks` directory, the app will be hosted on `http://0.0.0.0:5000/`.