docker实际操作指令记录------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

实际操作过程总结

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)

实际操作过程总结

.../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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容

  • 以下原文转载于(https://docs.docker.com/docker-for-mac/)(想找中文版的最新...
    Veekend阅读 7,494评论 0 17
  • 一天,一个博士坐船欣赏风景。 在船上,博士问渔夫:“你会生物吗?”渔夫说不会,博士就说:“那你的生命就要失去4分之...
    荣创阅读 540评论 0 0
  • 十月的天气,忽冷忽热,反复无常, 火车站的黑车司机还是那几位。 候车室里依旧是熙熙攘攘, 凉凉的座椅刚刚放得下我的...
    wzqzde阅读 329评论 0 0
  • 戈壁日出 残月 弯刀 划开博尔塔拉的地平线 伤痕渗流成 血珠 谁在低语 要有光
    荒原苍狼阅读 98评论 0 0