docker實(shí)際操作指令記錄------For Mac

[For Linux: add 'sudo' before all the commands]

1.image:

a lightweight, stand-alone, executable package that includes everything needed to run a piece of software , including the code, a runtime, libraries, environnement variables, and config files.

2. container:

- a runtime instance of image

- what the image becomes in memory when actually executed

- runs completely isolated from the host environnement by default

- only access host files and ports

3. begin building an app the Docker way:

Stack:

at the top level, define the interactions of all the services

Service:

defines how containers behave in production

Container:

at the bottom of the hierarchy of such an app executed images

3.1Dockerfile:

Define a container with a Dockerfile :

#use an official Python runtime as a base image

FROM ? python:2.7-slim

#Set the working directory to /app

WORKDIR ? /app

#Copy the current directory contents into the container at /app

ADD ? . ? ?/app

#Install any needed packages specified in requirements.txt

RUN ? pip ? install ? -r ? ?requirements.txt

#Make port 80 available to the world outside this container

EXPOSE ? 80

#Define environnement variable

ENV ? NAME ? World

#Run app.py when the container launches

CMD ? ["python", "app.py"]

3.2 Build the app

- ls

Dockerfile ? ? app.py ? ?requirements.txt

docker build -t friendlyhello .

* the build command

* this creates a Docker image called "friendlyhello" from the current directory

* tag this created image by "-t"

docker images

Where's your built images? in your machine's?local Docker image registry

3.3 Run the app

- run the app, map your machine's port 4000 to the container's exposed port 80 using "-p" :

docker run -p 4000:80 friendlyhello

- http://0.0.0.0:80 : coming from inside the container

- http:// localhost:4000 : the correct URL

- run in detached mode :

docker run -d -p 4000:80 friendlyhello

- see the abbreviatedcontainer IDwith

docker ps

- to end the process, using theCONTAINER ID:

docker stop 1fa4ab2cf395

3.4 Share your image:

- Object :

* upload our build

* run it somewhere else

* learn how to push to registries to make deployment of containers actually happen

- Registry :

* a collection of?repositories

- Repository :

* a collection of?images

* like a Github repository

* except the code is already built

- An account on a repository :?cloud.docker.com

- STEPS :

* docker login

* docker tag friendlyhello username/repository:tag

* docker push username/repository:tag

* docker run -p 4000:80 username/repository:tag

4. Services

- learn how to scale your application by?running this container in a service?& enable?load-balancing

- Prerequisites :

* docker run -p 80:80 username/repo:tag

* ensure your image is working by running this and visiting http://localhost

- Service :

In a distributed application, different pieces of app are called "Services"

For example, a video sharing site:

* a service for storing application data in db

* a service for video transcoding in the background

* a service for the front-end

"Services : containers in production"

A service :

* only runs an image

* codifies the way that images run :

-> what?ports?it should use

-> how many?replicas of the container?should run so the service has the capability it needs

*Scaling a service changes the number of containers instances?running that piece of software

4.1 define, run and scale services with the Docker platform with file :

docker-compose.yml

a YAML file that?defines how docker containers should behave in production

Docker-compose.yml

version: "3"

services:

? ? web:

? ? ? ? images: username/repository:tag

? ? ? ? deploy:

? ? ? ? ? ? replicas: 5

? ? ? ? ? ? resources:

? ? ? ? ? ? ? ? ? limits:

? ? ? ? ? ? ? ? ? ? ? ? ? ? cpus: "0.1"

? ? ? ? ? ? ? ? ? ? ? ? ? ? memory: 50M

? ? ? ? ? ? ? ? ? ?restart_policy:

? ? ? ? ? ? ? ? ? ? ? ? ? ? condition: on-failure

? ? ? ? ports:

? ? ? ? ? ? ? ? ?- "80:80"

? ? ? ? networks:

? ? ? ? ? ? ? ? ?- webnet

networks:

? ? ? ? ?webnet

* replicas: 5

Run 5 instances of the images we uploaded as a service called web

* condition: on-failure

immediately restart containers if one fails

* "80:80"

the 1st 80 means the port on the host

* the 1st "networks: - webnet "

instruct web's containers to share port 80via a load-balanced network called webnet

* the 2nd "networks: -webnet"

define the webnet network with the default settings (which is aload-balancing overlay network)

4.2 Run your new load-balanced app

STEP 01

docker swarm init

STEP 02

docker stack deploy -c docker-compose.yml getstarted

(give your app a name getstarted)

STEP 03

docker stack ps getstarted

(see a list of five containers you just launched)

STEP 04

curl http://localhost/

4.3 Scale the app

scale the app by changing the replicas values in docker-compose.yml, saving the change, and re-running the "docker stack deploy" command :

docker stack deploy -c docker-compose.yml getstarted

4.4 Take down the app

docker stack rm getstarted

5. Swarm

* In part?Services:

- take an app you wrote?

* In part?Containers

- define?how it should run in production by turning it to a service

- scaling it up 5 x in the process

* In part?Swarm:

- deploy this application onto a cluster

- running it on multiple machines

*Multi-container, multi-machine applications?are made possible by joining multiple machines into a"dockerized" cluster?called a

swarm

* A swarm:

a group of machines that are running Docker and have been joined into a cluster

* run the Docker commands, these commands run on a cluster by a

swarm manager

* Swarm manager:

use strategy to run containers:

-"emptiest mode": which fills the least utilised machines with containers

-"global": which ensures that each machine gets exactly one instance of the specified container

instruct swarm manager to use these strategies in the?compose file

* the only machine in a swarm to:

- execute your commands

- authorise other machines to join the swarm as?workers

* Worker:

- just provide capacity

- do not have the authority to tell any other machine what it can or cannot do

* Swarm mode:

- Docker work mode:

*a single-host mode?on your local machine

* swarm mode

- swarm mode:

* Enabling swarm mode instantly makes the current machine a swarm manager

* Docker executes commands on the swarm, rather than on the current machine.

5.1 Set up your swarm:

- A swarm is made up of multiple?nodes, which can be either?physical or virtual machines.

- basic concepts:

docker swarm init

to enable swarm node and make your current machine a swarm manager

docker swarm join

(on other machines)

to have them join the swarm as a worker

5.2 Create a cluster

- create a couple of Vos using the Virtualbox driver :

docker-machine

docker-machine create --driver virtualbox myvm1

docker-machine create --driver virtualbox myvm2

(myvm1 - manager; myvm2 - worker)

- send commands to your VMs using

docker-machine ssh

docker-machine ssh myvm1 "docker swarm init"

("docker swarm init" - instruct myvm1 to become a swarm manager)

- to have myvm2 join your new swarm as a worker:

docker-machine ssh myvm2 "docker swarm join --token :"

5.3 Deploy your app on a cluster

- deploy your app on your new swarm

- only swarm manager like myvm1 can execute commands

- workers are just for capacity

- copy the docker-compose file to the swarm manager myvm1 home directory (alias: ~)

docker-machine sap docker-compose.yml myvm1:~

- Now that myvm1 use its powers as a swarm manager to deploy your app

docker-machine ssh myvm1 "docker stack deploy -c docker-compose.yml getstartedlab"

the app is deployed on a cluster

- you'll see the containers have been distributed between both myvm1 and myvm2

docker-machine ssh myvm1 "docker stack ps getstartedlab"

5.4 Accessing your cluster

- To get your VMs' IP addresses

docker-machine ls

- visit either of them on a browser

6. Stack

Swarm?Chapitre:

- learn how to set up a swarm

* a swarm: a cluster of machines running Docker

- and deploy an app on a swarm, with containers running in concert on multiple machines

Stack?Chapitre:

- A stack:

* the top of the hierarchy of distributed app

*a group of integrated services?that share dependencies, and can be orchestrated and scaled together

-A single stack is capable of defining and coordination the functionality of an entire application

- In part?Swarm: a single stack of a single service runs on a single host

- In part?Stack: a stack of multiple services related to each other and run them on multiple machines

實(shí)際操作過程總結(jié)

1. /images/php7_fpm_base: ?{php7_fpm_base}

* docker build -t bonjourausy_php7_fpm_base .

* docker tag bonjourausy_php7_fpm_base $DOCKER_ID_USER/bonjourausy_php7_fpm_base

* docker push $DOCKER_ID_USER/bonjourausy_php7_fpm_base

* docker images

- php: 7-fpm

- bonjourausy_php7_fpm_base:latest

- amelieykw/bonjourausy_php7_fpm_base:latest

2. /images/app: ?{app}

* docker build -t bonjourausy_app .

* docker tag bonjourausy_app $DOCKER_ID_USER/bonjourausy_app

* docker push $DOCKER_ID_USER/bonjourausy_app

* docker images

- bonjourausy_app : latest

- amelieykw/bonjourausy_app : latest

3. /images/nginx: ??{webnginx}

* docker build -t bonjourausy_webnginx .

* docker tag bonjourausy_webnginx $DOCKER_ID_USER/bonjourausy_webnginx

* docker push $DOCKER_ID_USER/bonjourausy_webnginx

* docker images

- nginx: latest

- bonjourausy_webnginx: latest

- amelieykw/bonjourausy_webnginx: latest

4. /images/mysql: ??{mysql}

* docker build -t bonjourausy_mysql .

* docker tag bonjourausy_mysql $DOCKER_ID_USER/bonjourausy_mysql

* docker push $DOCKER_ID_USER/bonjourausy_mysql

* docker images

- mysql: latest

- bonjourausy_mysql: latest

- amelieykw/bonjourausy_mysql: latest

Docker Compose ?VS ?Docker Cloud Stack File

1. Docker Compose

* Docker compose runs on localhost or virtual machine

* Prerequistes:

- already install ??Docker Engine ?or ?Docker Compose

* STEP 01: Set Up

- mkdir composetest

- cd composetest

- create a file of app code (like app.py) in this directory

* STEP 02: Create a Dockerfile

-Dockerfile: ?to build an image

- create a Dockerfile in the directory of the image

- Dockerfile: to tell all the dependencies that this image needs

FROM ? ?python: 3.4-alpine

# Build an image starting with the python 3.4 image

ADD ? ?. ? ?/code

# Add the current directory into the path /code in the image

WORKDIR ? ? /code

# Set the working directory to /code

RUN ? ?pip ? ?install ? ?-r ? ?requirements.txt

# install the Python dependencies

CMD ["python", "app.py"]

# Set the default command for the container to "python app.py"

requirements.txt

flask

redis

* STEP 03: Define services in a Compose file

docker-compose.yml

version: '2'

services:

? ? ? web:

? ? ? ? ? ? ?build: .

? ? ? ? ? ? ?ports:

? ? ? ? ? ? ? ? ? ? ? ? - "5000:5000"

? ? ? ? ? ? ?volumes:

? ? ? ? ? ? ? ? ? ? ? ? - . : /code

? ? ? redis:

? ? ? ? ? ? ? image: "redis : alpine"

-build: .

use an image that's build from the Dockerfile in the current directory

-ports: - "5000:5000"

the 1st port 5000 on the host machine

the 2nd port exposed port 5000 on the container

-. : /code

Mounts the project directory on the host to /code inside the container, allowing you to modify the code without having to rebuild the image

* STEP 04: Build and run your app with Compose

docker-compose up -d

- Linux host machine:?http://loaclhost:5000

Mac docker machine:?http://MACHINE_VM_IP:5000

docker-machine ip MACHINE_VM

- list local images:

docker image ls

- inspect local images:

docker inspect

* STEP 05: Update the application

- Because the application code is mounted into the container using a volume, you can make changes to its code and see the changes instantly, without having to rebuild the image.

- STEPs:

* change the app code in app.py

* refresh the browser to see the changes

* STEP 06: Expriment with some other commands

docker-compose up -d

run your services in the background in the "detached" mode

docker-compose ps

to see what is currently running

docker-compose run web env

allows to run one-off commands for your service

docker-compose stop

to stop your services once you've finished with them

docker-compose down --volumes

to bring everything down, removing the containers entirely, with the down command

--volumes

remove the data volumes used by the Redis container

docker-compose --help

2. Stack file for Docker Cloud

*A stack: ?a collection of services that make up an application

*A stack file:

- a file in YAML format that defines one or more services

- similar to docker-compose.yml file for Docker Compose

- but a few extensions

- default name:

docker-cloud.yml

* Manage service stacks:

-Stacks:

a convient way to automatically deploy multiple services that are linked to each other, without needing to define each one separately

-Stack files:

define :

* environnement variables

* deployment tags

* the number of containers

* related environnement-specific config

docker-cloud.yml

lb:

? ? ? image: dockercloud/haproxy

? ? ? links:

? ? ? ? ? ? ?- web

? ? ? ports:

? ? ? ? ? ? - "80:80"

? ? ? roles:

? ? ? ? ? ?- global

web:

? ? ? ? ?image: dockercloud/quickstart-python

? ? ? ? links:

? ? ? ? ? ? ? ? ?- redis

? ? ? ? target_num_containers: 4

redis:

? ? ? ? ? image: redis

lb/web/redis:

Each key define in docker-cloud.yml creates a service with that name in Docker Cloud.

* Create a Stack:

- from the web interface

- using CLI:

docker-cloud stack create -f docker-cloud.yml

* Update an existing stack:

- specify an existing stack when you create a service

- later want to add a service to an existing stack

- from the Docker Cloud web interface

- using CLI:

docker-cloud stack update -f docker-cloud.yml (uuid or name)

實(shí)際操作過程總結(jié)

.../test/ykw_BonjourAUSY

docker-machine ls

docker-machine start BonjourAUSY

docker-machine env BonjourAUSY

eval $(docker-machine env BonjourAUSY)

docker login

docker image ls

docker-compose up -d ? ? #?to run docker-compose.yml

docker exec [OPTIONS] CONTAINERS COMMAND [ARG...]

- Run a command in a running container

- docker exec only runs a new command in a running container and not restarted if the container is restarted

docker run --name ubuntu_bash --rm -it ubuntu bash

docker exec -d ubuntu_bash touch /tmp/execWords

# create a new file (/tmp/execWords) inside the running container (ubuntu_bash), in the background

docker exec -it ubuntu_bash bash

# execute an interactive bash shell on the container

docker-compose ps

to see the running local services

docker container ps

to see the local containers

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末录豺,一起剝皮案震驚了整個濱河市闰非,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌套啤,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件扛门,死亡現(xiàn)場離奇詭異抓歼,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)兔甘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門锉矢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來孙援,“玉大人趾撵,你說我怎么就攤上這事幌衣”鹬牵” “怎么了窥突?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵蒂秘,是天一觀的道長拆融。 經(jīng)常有香客問我褒链,道長唁情,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任甫匹,我火速辦了婚禮甸鸟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘兵迅。我一直安慰自己抢韭,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布恍箭。 她就那樣靜靜地躺著刻恭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪扯夭。 梳的紋絲不亂的頭發(fā)上鳍贾,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天,我揣著相機(jī)與錄音交洗,去河邊找鬼贾漏。 笑死,一個胖子當(dāng)著我的面吹牛藕筋,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼隐圾,長吁一口氣:“原來是場噩夢啊……” “哼伍掀!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起暇藏,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤蜜笤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后盐碱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體把兔,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年瓮顽,在試婚紗的時候發(fā)現(xiàn)自己被綠了县好。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡暖混,死狀恐怖缕贡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情拣播,我是刑警寧澤晾咪,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站贮配,受9級特大地震影響谍倦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜泪勒,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一昼蛀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧酣藻,春花似錦曹洽、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至怕轿,卻和暖如春偷崩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背撞羽。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工阐斜, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人诀紊。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓谒出,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子笤喳,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 以下原文轉(zhuǎn)載于(https://docs.docker.com/docker-for-mac/)(想找中文版的最新...
    Veekend閱讀 7,558評論 0 17
  • 一天为居,一個博士坐船欣賞風(fēng)景。 在船上杀狡,博士問漁夫:“你會生物嗎蒙畴?”漁夫說不會,博士就說:“那你的生命就要失去4分之...
    榮創(chuàng)閱讀 599評論 0 0
  • 十月的天氣呜象,忽冷忽熱膳凝,反復(fù)無常, 火車站的黑車司機(jī)還是那幾位恭陡。 候車室里依舊是熙熙攘攘蹬音, 涼涼的座椅剛剛放得下我的...
    wzqzde閱讀 352評論 0 0
  • 戈壁日出 殘月 彎刀 劃開博爾塔拉的地平線 傷痕滲流成 血珠 誰在低語 要有光
    荒原蒼狼閱讀 119評論 0 0