Dokcer
Docker is all about speed.
- 試跑 Nginx 容器
docker container run --public 80:80 --detach --name webhost nginx:1.11
- --datach 會給 Container ID
- --name 可以指定 Container 的 Name
- 80:80 改變 host listening port
- 1.11 可以改變 image Nginx 的版本
- 試跑 Mongo 容器
docker run --name mongo -d mongo
docker top mongo
ps aux | grep mongo
- top 可以顯示正在執行的容器
- ps aux 可以顯示所有正在執行的容器
docker container top
- process list in one containerdocker container inspect
- details of one container configdocker container stats
- performances stats for all container
- 試跑 Mysql 容器
- docker container run -d -p 3306:3306 --name db -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql
- -d 背景執行、-p listen to port、命名為db、-e環境變數
- docker container run -it - start new container iteractively
- docker container exec -it - run additional command in existing container
- 指令複習
- -p (--publish) : 記得publishing的格式總是host:container
- Port觀念:
- 當你有多個container,不可以有超過一個以上的容器同時去listen同樣的對外port,Example. 8080:80, 8080:8080 這樣就不行
- Docker Container Network:CLI Commands
- Show networks:docker network ls
- Inspect a network:docker network inspect
- Create a network:docker network create --driver
- Attach a network to container:docker network connect a b
- Disconnect a network from container:docker network disconnect a b
- Docker networks: Default Security
- Create your apps so frontend/backend sit on same Docker network
- Their inter-communication never leaves host
- All externally exposed ports closed by default
- You must manually expose via -p, which is better default security
- This gets even better later with Swarm and Overlay networks
- Docker networks: DNS
- Understand how DNS is the key to easy inner-container comms
- See how it works by default with custom netwokrs
- Learn how to use --link to enable DNS on default bridge network
- 練習DNS內網連結:
- docker network create dude 新增一個network
- docker container run -d --net dude --net-alias search elasticsearch:2 新增一個容器指定net為dude設定net的DNS別名為search
- 重複執行一次,產生兩個 elasticsearch:2 的容器
- docker container run --rm --net dude alpine nslookup search 創建容器alpine並且搜尋nslookup
- 會顯示兩個容器在同一個DNS下的Address,172.30.0.2 search.dude和 172.30.0.3 search.dude
- docker container run --rm --net dude centos curl -s search:9200
- 會隨機出現兩個address中的其中一個
- Image Layer:
- 底層是Ubuntu
- 中層是你可以放置各種檔案
- 上層是做一些環境設定
- 這底中上層包起來,就是一個 image
- Image:
- 就是一些檔案操作和metadata
- 每一層都是獨特的,並且只儲存一次
- 容器只是在single read/write layer on top of image
- docker image history / inspect 可以看到細節
- Image tag:
- latest: latest stable version
- Dockerfile:
- FROM 某個image
- EXPOSE 3000: listen 3000 port
- CMD: 只有在launch時執行一次,應該只要有一個CMD,如果有多個CMD,以最後一個為主
- 清理的指令:
docker image prune
to clean up just "dangling" imagesdocker system prune
will clean up everything- The big one is usually
docker image prune -a
which will remove all images you're not using. Usedocker system df
to see space usage.
- Persistent Data: