---
# System prepended metadata

title: 第9章 建立與使用 Docker Volume
tags: [volume, docker, '2021']

---

# 第9章 建立與使用 Docker Volume

https://i.imgur.com/SNzlSGI.png
![](https://i.imgur.com/SNzlSGI.png)


# 觀念

- mac/window host disk  => 主機硬碟
- linux vm disk => 砍掉 continaer 資料不會消失
- container disk => 暫時的，砍掉continaer 資料也會消失

# 實作

```
docker build -t xxx/apache01
docker run -d -p 8080:80 xxx/apache01
docker container ls
echo $(docker machine ip)
docker exec -it   container_id /bin/sh

# ls 
# cat index.html
# echo "I made this change in 1997." >> index.html
# cat index.html

docekr stop container_id
docker rm container_id
docker ls
docker run -d -p 8080:80 xxx/apache01
docker container ls

// 用 docker volume 功能

docekr volume ls
docker volume create mainpage--vol
docker volume ls
docker volume inspect mainpage--vol
docker run -d -p 8081:80 -v(用volume) mainpage--vol(VM linux disk空間):/var/www/localhost/htdics/(container disk 空間) xxx/apache001

docker container ls
docker exec -it container_id /bin/sh

# ls 
# cat index.html
# echo "I made this change in 1997." >> index.html
# cat index.html
# exit

docekr container ls
docker container stop container_id
docker rm container_id


docker run -d -p 8081:80 -v(用volume) mainpage--vol(VM linux disk空間):/var/www/localhost/htdics/(container disk 空間) xxx/apache001
```

```dockerfile=
FROM alpine
ENV myworkdir /var/www/localhost/htdocs
ARG whoami=Jake
WORKDIR ${myworkdir}
RUN apk --update add apache2
RUN rm -rf /var/cache/apk/*
ENTRYPOINT ["httpd", "-D", "FOREGROUND"]
```

###### tags: `2021`  `docker` `volume`


{%hackmd BJrTq20hE %}
