SlideShare a Scribd company logo
Docker 基礎介紹與實戰
Bo-Yi Wu
2016.04.21
1
關於我
• https://blog.wu-boy.com
• https://github.com/appleboy
• https://www.facebook.com/appleboy46
2
為什麼需要使用 Doecker
3
Why
• 新人環境建置 (蜜月期?)
• 多種環境建置及版本測試
– Node 4.x, 5.x ..
– PHP 5.6, 5.7 ..
– Wordpress, Discourse, Gogs, Gitlab ….
4
多人共用一台Build Server
5
問題是 …
• 宅宅 A: 那個誰誰,可以先停掉你的程序嗎?
• 宅宅 B: CPU 跟 Ram 都爆了啦 ….
• 宅宅 C: 編譯個 Router Code 要半小時啊 ….
• 宅宅 D: 趁半夜沒人的時候再來用 (加班狂?)
6
7
軟體工程師
• 做事效率低落
• 每天產能有限
• 浪費很多時間在 Build Code 上
• 如果 Server 壞了,是全 Team 放假嗎?
• 週末或平日晚上頻加班 …
8
聊聊Web前後端開發環境
9
前端 vs 後端
API Server
前端 Team 後端 Team
Deploy
Deploy
10
如果 API Server 掛掉
前端團隊全部都在等
後端工程師修復
11
這時候就需要 Docker 了
12
解決
• 工程師不再抱怨 Build Server 慢
– 不會再找我麻煩了 (誤)
• 前後端各自獨立作業
– 前端各自有獨立開發環境
• 要測試 Service (Wordpress, Jenkins, Gogs)
– 不需要安裝任何 redis, mysql, php 等環境
13
What’s Docker?
14
Docker vs. Virtual Machine
15
基本觀念
• Docker 映像檔 (Images)
• Docker 容器 (Container)
• Docker 倉庫 (Repository)
16
Docker Images
17
Docker container
• 從 images 建立新的 container
• 每個容器互相隔離,保證安全
• 可寫可讀 (Read, Write)
18
Docker Repository
Docker 倉庫概念跟 Git 類似
你可以想成類似 Github 託管服務
19
Image Image
Container Container
Docker Registry
Pull
Run Commit
Push
20
Image
Docker Registry
Pull
docker pull ubuntu
21
Image
Container
Docker Registry
Pull
Run docker run –t –i ubuntu /bin/bash
22
Image
Container Container
Docker Registry
Pull
Run
apt-get update 23
Image Image
Container Container
Docker Registry
Pull
Run Commitdocker commit
24
Image Image
Container Container
Docker Registry
Pull
Run Commit
Push
docker push
25
Docker 安裝
Mac OS X: https://goo.gl/05XMnB
Linux: https://goo.gl/wRpzlT
26
Docker images
https://hub.docker.com/
請先申請帳號密碼
27
Docker images
• docker pull ubuntu:14.04
– ubuntu: image name
– 14.04: tag name, default is “latest”
– host: registry.hub.docker.com
28
列出本機端 images
docker images
29
30
進入容器內 (秒入)
docker run –ti ubuntu:14.04 /bin/bash
root@9cadb3b3e718:/#
31
可以做什麼?
做你想做的任何事情
Install nginx, php, mysql ….
32
儲存目前的工作狀態
docker commit –m ‘test’ –a ‘Bo-Yi Wu’
9cadb3b3e718 appleboy/test:1.0
33
從上次 commit 進入 bash
docker run –t –i appleboy/test:1.0 /bin/bash
34
玩壞了沒關係
docker run –ti ubuntu:14.04 /bin/bash
35
Demo
36
Ubuntu images
apt-get update
Nginx Apache
PHP 5.3 PHP 5.4 PHP 5.6 PHP 5.7
37
Ubuntu images
apt-get update
docker pull ubuntu:14.04
docker run –ti ubuntu:14.04 /bin/bash
$ apt-get update && apt-get –y upgrade
$ exit
docker commit –m “test” xxxxx test/base:1.0
38
Ubuntu images
apt-get update
Nginx
docker run –ti test/base:1.0 /bin/bash
$ install nginx ……
$ exit
docker commit –m “test” xxxxx test/nginx:1.0
39
Ubuntu images
apt-get update
docker run –ti test/base:1.0 /bin/bash
$ install apache……
$ exit
docker commit –m “test” xxxxx test/apache:1.0
Apache
40
Ubuntu images
apt-get update
Nginx
docker run –ti test/nginx:1.0 /bin/bash
$ install php5.3 ……
docker commit –m “php” xx test/php:5.3
PHP 5.3
41
練習
前端建立 node 4 及 node 5 環境
後端建立 php6 及 php7 環境
驗證 images 是否有該執行檔
42
有沒有覺得打指令很累
有沒有一個指令就把 images 建立好?
43
這時候你就需要
Dockerfile
$ touch Dockerfile
44
45
Dockerfile
好理解,易於管理,還可以版控
46
透過 Dockerfile 建立 local images
docker build –t myimage -f Dockerfile .
47
啟動自製 images
48
Ubuntu
images
eth0 8000 port
Host
Docker bridge
eth0 5467 port
Docker run –d –p 8000 hello
49
Ubuntu
images
eth0 8000 port
Host
eth0 80 port
Docker run –d –p 80:8000 hello
Docker bridge
50
Hello
eth0 8000 port
Host
Docker bridge
eth0 80 port
Docker run –d ––name hello –p 80:8000 hello
51
該如何把目錄 mount
到 Container 內呢?
52
Hello
eth0 8000 port
Host
Docker bridge
eth0 80 port
Docker run –d –v /opt/test:/home/test ––name
hello –p 80:8000 hello
/opt/test
/home/test
53
如何看 Docker log
docker ps
docker logs name
54
如何進入容器
docker ps
docker exec –ti name /bin/bash
55
停止,啟動容器
docker ps
docker stop name
docker start name
docker restart name
56
連接多個服務
MySQL, Redis ….
57
Ubuntu
eth0 8000 port
Host
Docker bridge
eth0 80 port
Docker run –d ––link redis:redis –p 80:8000 hello
58
啟動 Mysql, Redis
• docker run –d ––name my-db –e
MYSQL_ROOT_PASSWORD=1234 mysql
• docker run –d ––name my-redis redis
59
連接 Mysql 及 Redis
Docker run –ti ––link my-db:db 
––link my-redis:redis 
ubuntu /bin/bash
60
建立 Wordpress
• docker run –d ––name my-db 
–e MYSQL_ROOT_PASSWORD=1234 
mysql
• docker run ––name my-wp 
--link my-db:mysql -d 
-p 8080:80
wordpress
61
如果有5個以上服務需要連接呢
這時候你就需要 docker-compose.yml
62
wordpress:
image: wordpress
links:
- db:mysql
ports:
- 8080:80
db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: test63
docker-compose 啟動
docker-compose up –d
create and start containers
64
docker-compose 列表
docker-compose ps
65
用 docker ps 也可以
66
docker-compose
• Docker-compose stop (停止服務)
• Docker-compose start (啟動服務)
• Docker-compose rm (移除全部 container)
67
平行擴展 DB 架構
docker-compose scale db=5
68
用 Docker 來測試
69
事前準備
• 準備相關環境
– Node 4
– Node 5
– PHP5
– PHP6
– PHP7
Images
Dockerfile Yoyo/node:4
Yoyo/node:5
Yoyo/php:6
Yoyo/php:7
70
Testing node4
Testing node5
Testing php7
Testing php6
71
Docker run –rm 
–v folder1:folder2 
--link mysql:mysql 
--workdir=/app 
-e DEV=Testing
yoyo:node5 
/bin/bash –c “npm test”
72
Best Practices Cheat Sheet
https://goo.gl/4CgNkd
73
74
Any Question?
75

More Related Content

PDF
Virtual Machines and Docker
PDF
寫給大家的 Git 教學
PPTX
Kubernetes (K8s) 簡介 | GDSC NYCU
PDF
ArgoCD 的雷 碰過的人就知道 @TSMC IT Community Meetup #4
PDF
Kubernetes Summit 2024 - How GenAI Help you in K8s Ops
PDF
Learn O11y from Grafana ecosystem.
PDF
Faster Container Image Distribution on a Variety of Tools with Lazy Pulling
PDF
How to write a Dockerfile
Virtual Machines and Docker
寫給大家的 Git 教學
Kubernetes (K8s) 簡介 | GDSC NYCU
ArgoCD 的雷 碰過的人就知道 @TSMC IT Community Meetup #4
Kubernetes Summit 2024 - How GenAI Help you in K8s Ops
Learn O11y from Grafana ecosystem.
Faster Container Image Distribution on a Variety of Tools with Lazy Pulling
How to write a Dockerfile

What's hot (20)

PDF
從軟體開發角度
談 Docker 的應用
PDF
Docker入門-基礎編 いまから始めるDocker管理【2nd Edition】
PPTX
コンテナネットワーキング(CNI)最前線
PPT
presentation on Docker
PPTX
Dockerからcontainerdへの移行
PDF
今だからこそ知りたい Docker Compose/Swarm 入門
PDF
Dockerの期待と現実~Docker都市伝説はなぜ生まれるのか~
PPTX
Introduction to Docker
PDF
SpotBugs(FindBugs)による 大規模ERPのコード品質改善
PDF
A/B Linux updates with RAUC and meta-rauc-community: now & in the future
PDF
Docker and the Linux Kernel
PDF
Docker Introduction
PDF
Kubernetes Architecture | Understanding Kubernetes Components | Kubernetes Tu...
PDF
Docker란 무엇인가? : Docker 기본 사용법
PDF
왕초보를 위한 도커 사용법
PDF
A Hands-on Introduction on Terraform Best Concepts and Best Practices
PDF
Getting Started with Kubernetes
PDF
Web開発者が始める .NET MAUI Blazor App
PDF
Introduction to Docker storage, volume and image
PPTX
Dockers and containers basics
從軟體開發角度
談 Docker 的應用
Docker入門-基礎編 いまから始めるDocker管理【2nd Edition】
コンテナネットワーキング(CNI)最前線
presentation on Docker
Dockerからcontainerdへの移行
今だからこそ知りたい Docker Compose/Swarm 入門
Dockerの期待と現実~Docker都市伝説はなぜ生まれるのか~
Introduction to Docker
SpotBugs(FindBugs)による 大規模ERPのコード品質改善
A/B Linux updates with RAUC and meta-rauc-community: now & in the future
Docker and the Linux Kernel
Docker Introduction
Kubernetes Architecture | Understanding Kubernetes Components | Kubernetes Tu...
Docker란 무엇인가? : Docker 기본 사용법
왕초보를 위한 도커 사용법
A Hands-on Introduction on Terraform Best Concepts and Best Practices
Getting Started with Kubernetes
Web開発者が始める .NET MAUI Blazor App
Introduction to Docker storage, volume and image
Dockers and containers basics
Ad

Viewers also liked (7)

PDF
Docker 初探,實驗室中的運貨鯨
PDF
認識那條鯨魚 Docker 初探
PDF
PDF
What's New in Docker 1.12 by Nishant Totla for Docker SF Meetup 08.03.16
PDF
Docker home ted
PDF
Docker初识
PPTX
用 Go 語言 打造微服務架構
Docker 初探,實驗室中的運貨鯨
認識那條鯨魚 Docker 初探
What's New in Docker 1.12 by Nishant Totla for Docker SF Meetup 08.03.16
Docker home ted
Docker初识
用 Go 語言 打造微服務架構
Ad

Similar to Docker 基礎介紹與實戰 (20)

PDF
Docker Build
PDF
Docker
PPTX
Docker tutorial
PDF
docker intro
PDF
20150604 docker 新手入門
PDF
Docker容器微服務 x WorkShop
PPTX
Introduction to Docker
PDF
讓軟體開發與應用更自由 - 使用 Docker 技術
PPTX
Docker 101
PPTX
Docker
PPTX
Docker Compose
PDF
桃園市教育局Docker技術入門與實作
PPTX
Docker - 30秒生出100台伺服器
PDF
Docker In-Depth
PPTX
Docker基礎
PDF
前端工程師一定要知道的 Docker 虛擬化容器技巧
PPTX
Docker基礎
PPTX
Docker一期培训
PDF
Linking error
PPTX
Docker進階探討
Docker Build
Docker
Docker tutorial
docker intro
20150604 docker 新手入門
Docker容器微服務 x WorkShop
Introduction to Docker
讓軟體開發與應用更自由 - 使用 Docker 技術
Docker 101
Docker
Docker Compose
桃園市教育局Docker技術入門與實作
Docker - 30秒生出100台伺服器
Docker In-Depth
Docker基礎
前端工程師一定要知道的 Docker 虛擬化容器技巧
Docker基礎
Docker一期培训
Linking error
Docker進階探討

More from Bo-Yi Wu (20)

PDF
Drone CI/CD 自動化測試及部署
PDF
用 Go 語言打造多台機器 Scale 架構
PDF
Job Queue in Golang
PDF
Golang Project Layout and Practice
PDF
Introduction to GitHub Actions
PDF
Drone 1.0 Feature
PDF
Drone CI/CD Platform
PDF
GraphQL IN Golang
PPTX
Go 語言基礎簡介
PPTX
drone continuous Integration
PPTX
Gorush: A push notification server written in Go
PPTX
用 Drone 打造 輕量級容器持續交付平台
PPTX
Introduction to Gitea with Drone
PDF
運用 Docker 整合 Laravel 提升團隊開發效率
PDF
用 Go 語言實戰 Push Notification 服務
PPTX
用 Go 語言打造 DevOps Bot
PPTX
A painless self-hosted Git service: Gitea
PPTX
Write microservice in golang
PPTX
用 Docker 改善團隊合作模式
PPTX
Git flow 與團隊合作
Drone CI/CD 自動化測試及部署
用 Go 語言打造多台機器 Scale 架構
Job Queue in Golang
Golang Project Layout and Practice
Introduction to GitHub Actions
Drone 1.0 Feature
Drone CI/CD Platform
GraphQL IN Golang
Go 語言基礎簡介
drone continuous Integration
Gorush: A push notification server written in Go
用 Drone 打造 輕量級容器持續交付平台
Introduction to Gitea with Drone
運用 Docker 整合 Laravel 提升團隊開發效率
用 Go 語言實戰 Push Notification 服務
用 Go 語言打造 DevOps Bot
A painless self-hosted Git service: Gitea
Write microservice in golang
用 Docker 改善團隊合作模式
Git flow 與團隊合作

Docker 基礎介紹與實戰

Editor's Notes

  • #16: 傳統 VM 是在OS外來建立虛擬環境,透過Hypervisor在Host中模擬一套完整的硬體環境資源,目標是建立一個可以用來執行整套作業系統的沙箱獨立執行環境,所以VM做出來的是一個一個可以獨立安裝 OS 的「盒子」。 而 Container 是在OS內的核心系統層來打造虛擬執行環境,透過共用Host OS的作法,取代一個一個Guest OS的功用。 Container也因此被稱為是OS層的虛擬化技術。 Container 很輕、很快,啟動速度是秒級,可以大量節約開發、測試與部署的時間。