---
# System prepended metadata

title: 第10章 Docker 資源管理
tags: [network, volume, docker, service, docker-compose, '2021']

---

# 第10章 Docker 資源管理


# 建立與使用 Docker compose 觀念
![](https://i.imgur.com/71qY0YT.png)


將資源統整起來 
- Services - Container、Image、Dockerfile
- Network - None、Bridge、Container、Host
- Volume - Container空間、VM linux 空間、Host 空間


# 建立與使用 Docker compose 實作 Services

```dockerfile=
FROM alpine:latest
ENV myworkdir /var/www/localhost/htdocs
ARG whoami=Jake
WORKDIR ${myworkdir}
RUN apk --update add apache2
RUN rm -rf /var/cache/apk/*
RUN echo "<h3>I am ${whoami}. Iam taking this great Docker Course. Round 01</h3>" >> index.html
RUN echo "<h3>I am ${whoami}. Iam taking this great Docker Course. Round 02</h3>" >> index.html
RUN echo "<h3>I am ${whoami}. Iam taking this great Docker Course. Round 03</h3>" >> index.html
ENTRYPOINT ["http", "-D", "FOREGROUND"]
```

## 建立 docker compose 設定檔

```
touch docker-compose.yml (要跟 dockerfile 同一目錄底下)
```
用編輯器打開 docker-compose.yml

```
// docker-compose.yml

version: "3.7"
services: 
  myweb:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Tony"
      image: xxx/myweb:latest
      ports:
        - "8080:80" 
```

回到 terminal

```
docker-compose build --no-cache // 每次 build 都從新跑一次
docker images
docker-compose up -d (背景執行)
docker container ls
echo $(docker-machine ip)
```

用 docker-compose 把 container 都下架
```
docker-compose down
```

```
// docker-compose.yml

version: "3.7"
services: 
  myweb:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Jake"
      image: xxx/myweb:latest
      ports:
        - "8080:80" 
  myweb2:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Tony"
      image: xxx/myweb1:latest
      ports:
        - "8081:80" 
  myweb3:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Jane"
      image: xxx/myweb2:latest
      ports:
        - "8082:80" 
```

```
docker-compose build --no-cache
docker images
docker-compose up -d
docker container ls
docker-compose down
```



```
// docker-compose.yml

version: "3.7"
services: 
  myweb:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Jake"
      image: xxx/myweb:latest
      ports:
        - "8080:80" 
  myweb2:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Tony"
      image: xxx/myweb1:latest
      ports:
        - "8081:80" 
  myweb3:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Jane"
      image: xxx/myweb2:latest
      ports:
        - "8082:80" 
  myweb4:
    image:xxx/myweb:latest
    ports:
        - "8083:80" 
```

```
docker-compose build --no-cache
docker images
docker-compose up -d
docker container ls
```

# 建立與使用 Docker compose 實作 Networks


目錄底下檔案
```
Dockerfile
docker-compose.yml
```

```
docker-compose down
docker-compose up -d
docker network ls
docker network inspect
docker-compose down
```

打開 docker-compose.yml

```
// docker-compose.yml

version: "3.7"
services: 
  myweb:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Jake"
      image: xxx/myweb:latest
      ports:
        - "8080:80" 
      networks
        - mybridge001
  myweb2:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Tony"
      image: xxx/myweb1:latest
      ports:
        - "8081:80" 
      networks
        - mybridge001
  myweb3:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Jane"
      image: xxx/myweb2:latest
      ports:
        - "8082:80" 
      networks
        - mybridge001
  myweb4:
    image:xxx/myweb:latest
    ports:
        - "8083:80" 
    networks
        - mybridge002
networks:
  mybridge001:
  mybridge002:
```

```
docker-compose build --no-cache
docker-compose up -d
docker network ls
docker network inspect mybridge001

docker network ls
docker network inspect mybridge002
```

# 建立與使用 Docker compose Volumes

```
docker-compose down
```

```dockerfile=
FROM alpine:latest
ENV myworkdir /var/www/localhost/htdocs
ARG whoami=Jake
WORKDIR ${myworkdir}
RUN apk --update add apache2
RUN rm -rf /var/cache/apk/*
RUN echo "<h3>I am ${whoami}. Iam taking this great Docker Course. Round 01</h3>" >> index.html
RUN echo "<h3>I am ${whoami}. Iam taking this great Docker Course. Round 02</h3>" >> index.html
RUN echo "<h3>I am ${whoami}. Iam taking this great Docker Course. Round 03</h3>" >> index.html
ENTRYPOINT ["http", "-D", "FOREGROUND"]
```
回到 terminal

```
docker volume ls
```

打開 docker-compose.yml

```
// docker-compose.yml

version: "3.7"
services: 
  myweb:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Jake"
      image: xxx/myweb:latest
      ports:
        - "8080:80" 
      networks
        - mybridge001
  myweb2:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Tony"
      image: xxx/myweb1:latest
      ports:
        - "8081:80" 
      networks
        - mybridge001
  myweb3:
    build:
      context: . (dockerfile 所在目錄)
      args:  (dockfile 裡的 arguments)
        whoami: "Jane"
      image: xxx/myweb2:latest
      ports:
        - "8082:80" 
      networks
        - mybridge001
  myweb4:
    image:xxx/myweb:latest
    ports:
      - "8083:80" 
    networks
      - mybridge002
  myweb5:
    image:xxx/myweb:latest
    ports:
      - "8084:80" 
    networks
      - mybridge002
    volumes:
      - mainpage-vol002:/var/www/localhost/htdocs (apache 首頁目錄位置)
networks:
  mybridge001:
  mybridge002:
volumes:
  mainpage-vol002:
```

回到 terminal 

```
docker-compose build --no-cache
docker-compose up -d
docker volume ls
docker container ls
echo $(docker-machine ip)

docker container ls
docker exec -it container_id /bin/sh
# ls
# cat index.html
# echo "I made this change in 1997." >> index.html
# exit
```

```
docker-compose down
docker container ls
docker-compose up -d
```

###### tags: `2021`  `docker` `docker-compose` `service` `network` `volume`


{%hackmd BJrTq20hE %}
