SlideShare una empresa de Scribd logo
1 de 75
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Docker introduction
Docker introductionDocker introduction
Docker introduction
 
從軟體開發角度
談 Docker 的應用
從軟體開發角度
談 Docker 的應用從軟體開發角度
談 Docker 的應用
從軟體開發角度
談 Docker 的應用
 
Docker Introduction
Docker IntroductionDocker Introduction
Docker Introduction
 
Dockerfile
Dockerfile Dockerfile
Dockerfile
 
Docker Container Security
Docker Container SecurityDocker Container Security
Docker Container Security
 
Docker introduction for the beginners
Docker introduction for the beginnersDocker introduction for the beginners
Docker introduction for the beginners
 
Docker Introduction
Docker IntroductionDocker Introduction
Docker Introduction
 
Containers 101
Containers 101Containers 101
Containers 101
 
Docker應用
Docker應用Docker應用
Docker應用
 
From Zero to Docker
From Zero to DockerFrom Zero to Docker
From Zero to Docker
 
Docker 101 : Introduction to Docker and Containers
Docker 101 : Introduction to Docker and ContainersDocker 101 : Introduction to Docker and Containers
Docker 101 : Introduction to Docker and Containers
 
Docker Networking Overview
Docker Networking OverviewDocker Networking Overview
Docker Networking Overview
 
What is Docker | Docker Tutorial for Beginners | Docker Container | DevOps To...
What is Docker | Docker Tutorial for Beginners | Docker Container | DevOps To...What is Docker | Docker Tutorial for Beginners | Docker Container | DevOps To...
What is Docker | Docker Tutorial for Beginners | Docker Container | DevOps To...
 
왕초보를 위한 도커 사용법
왕초보를 위한 도커 사용법왕초보를 위한 도커 사용법
왕초보를 위한 도커 사용법
 
Docker networking Tutorial 101
Docker networking Tutorial 101Docker networking Tutorial 101
Docker networking Tutorial 101
 
Introduction to docker
Introduction to dockerIntroduction to docker
Introduction to docker
 
Getting started with Docker
Getting started with DockerGetting started with Docker
Getting started with Docker
 
Docker Container Introduction
Docker Container IntroductionDocker Container Introduction
Docker Container Introduction
 
Docker Introduction
Docker IntroductionDocker Introduction
Docker Introduction
 
Installing and running Postfix within a docker container from the command line
Installing and running Postfix within a docker container from the command lineInstalling and running Postfix within a docker container from the command line
Installing and running Postfix within a docker container from the command line
 

Destacado

Destacado (6)

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

Similar a Docker 基礎介紹與實戰

docker intro
docker introdocker intro
docker intro
koji lin
 
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
Wen-Tien Chang
 

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

Docker tutorial
Docker tutorialDocker tutorial
Docker tutorial
 
docker intro
docker introdocker intro
docker intro
 
Linking error
Linking errorLinking error
Linking error
 
Docker 101
Docker 101Docker 101
Docker 101
 
運用 Docker 整合 Laravel 提升團隊開發效率
運用 Docker 整合 Laravel 提升團隊開發效率運用 Docker 整合 Laravel 提升團隊開發效率
運用 Docker 整合 Laravel 提升團隊開發效率
 
讓軟體開發與應用更自由 - 使用 Docker 技術
讓軟體開發與應用更自由 - 使用 Docker 技術讓軟體開發與應用更自由 - 使用 Docker 技術
讓軟體開發與應用更自由 - 使用 Docker 技術
 
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
 
用 Docker 改善團隊合作模式
用 Docker 改善團隊合作模式用 Docker 改善團隊合作模式
用 Docker 改善團隊合作模式
 
桃園市教育局Docker技術入門與實作
桃園市教育局Docker技術入門與實作桃園市教育局Docker技術入門與實作
桃園市教育局Docker技術入門與實作
 
141118 Raspberry Pi 電鈴工作坊@松山文創園區
141118 Raspberry Pi 電鈴工作坊@松山文創園區141118 Raspberry Pi 電鈴工作坊@松山文創園區
141118 Raspberry Pi 電鈴工作坊@松山文創園區
 
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
 
Weic2015 docker
Weic2015 dockerWeic2015 docker
Weic2015 docker
 
FHIR Server 安裝與使用
FHIR Server 安裝與使用FHIR Server 安裝與使用
FHIR Server 安裝與使用
 
Python 于 webgame 的应用
Python 于 webgame 的应用Python 于 webgame 的应用
Python 于 webgame 的应用
 
開發人員不可不知的 Windows Container 容器技術預覽
開發人員不可不知的 Windows Container 容器技術預覽開發人員不可不知的 Windows Container 容器技術預覽
開發人員不可不知的 Windows Container 容器技術預覽
 
Docker容器微服務 x WorkShop
Docker容器微服務 x WorkShopDocker容器微服務 x WorkShop
Docker容器微服務 x WorkShop
 
Docker Compose
Docker ComposeDocker Compose
Docker Compose
 
Docker open stack
Docker open stackDocker open stack
Docker open stack
 
快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)
 
Bitbucket pipeline CI
Bitbucket pipeline CIBitbucket pipeline CI
Bitbucket pipeline CI
 

Más de Bo-Yi Wu

Más de Bo-Yi Wu (20)

Drone CI/CD 自動化測試及部署
Drone CI/CD 自動化測試及部署Drone CI/CD 自動化測試及部署
Drone CI/CD 自動化測試及部署
 
用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in Golang
 
Golang Project Layout and Practice
Golang Project Layout and PracticeGolang Project Layout and Practice
Golang Project Layout and Practice
 
Introduction to GitHub Actions
Introduction to GitHub ActionsIntroduction to GitHub Actions
Introduction to GitHub Actions
 
Drone 1.0 Feature
Drone 1.0 FeatureDrone 1.0 Feature
Drone 1.0 Feature
 
Drone CI/CD Platform
Drone CI/CD PlatformDrone CI/CD Platform
Drone CI/CD Platform
 
GraphQL IN Golang
GraphQL IN GolangGraphQL IN Golang
GraphQL IN Golang
 
Go 語言基礎簡介
Go 語言基礎簡介Go 語言基礎簡介
Go 語言基礎簡介
 
drone continuous Integration
drone continuous Integrationdrone continuous Integration
drone continuous Integration
 
Gorush: A push notification server written in Go
Gorush: A push notification server written in GoGorush: A push notification server written in Go
Gorush: A push notification server written in Go
 
用 Drone 打造 輕量級容器持續交付平台
用 Drone 打造輕量級容器持續交付平台用 Drone 打造輕量級容器持續交付平台
用 Drone 打造 輕量級容器持續交付平台
 
Introduction to Gitea with Drone
Introduction to Gitea with DroneIntroduction to Gitea with Drone
Introduction to Gitea with Drone
 
用 Go 語言實戰 Push Notification 服務
用 Go 語言實戰 Push Notification 服務用 Go 語言實戰 Push Notification 服務
用 Go 語言實戰 Push Notification 服務
 
用 Go 語言打造 DevOps Bot
用 Go 語言打造 DevOps Bot用 Go 語言打造 DevOps Bot
用 Go 語言打造 DevOps Bot
 
A painless self-hosted Git service: Gitea
A painless self-hosted Git service: GiteaA painless self-hosted Git service: Gitea
A painless self-hosted Git service: Gitea
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golang
 
Git flow 與團隊合作
Git flow 與團隊合作Git flow 與團隊合作
Git flow 與團隊合作
 
PHP & JavaScript & CSS Coding style
PHP & JavaScript & CSS Coding stylePHP & JavaScript & CSS Coding style
PHP & JavaScript & CSS Coding style
 
Why to choose laravel framework
Why to choose laravel frameworkWhy to choose laravel framework
Why to choose laravel framework
 

Docker 基礎介紹與實戰

Notas del editor

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