Arry's cnBlog

Barcelona win

博客园 首页 新随笔 联系 订阅 管理

Docker入门


Docker中文社区:http://www.docker.org.cn/,这里可以找到Docker的中文api的介绍,非常简单,但很适合初学者观看,毕竟英文的不是很好理解,看官方文档之前先看一下这个中文的api,便于理解;


Docker网站:https://docs.docker.com/,官网;


Docker指南,官网上可以找到的:https://docs.docker.com/get-started/


Get Docker:https://docs.docker.com/engine/installation/,Docker有服务端和客户端,服务端一般来说就是我们在虚拟机上装的,yum -y install docker,像这种的,学习的时候可以先装这一个,客户端在用的时候再装就是了,就和redis的服务端和客户端一样,客户端就是用来方便管理的;


Docker云:创建自己的docker yun账户:https://hub.docker.com/,可以在上面搜索镜像容器 ;


咱按照api流程走,先看一看Docker是个啥:



第一部分:Orientation(定位),就是说Docker是干什么的,


这么说有点笼统,直接上2张图对比一下传统vm和Docker有什么区别:


vm:



Docker环境:



从这里就可以看出区别,传统vm需要多个os,当你部署你的app时,你需要多个os,同时也要配多份环境,每多一个os就要多操作一份;而Docker是共用一个os的,基本环境都是一样的,你只需要集成某个容器特有的环境就好了,而且这个image(镜像)还是可移植的;



第二部分:Containers(容器),如上图所示,这是我们实际要用到的东西,

Containers可以说是运行的image,Docker中运行着许多隔离的Container,而这些Containers都是从它们的本体image执行出来的,就好像是windows操作系统本来是iso文件(image),安装了之后就变成我们使用的桌面了(Container);

Docker云上有许多已经创建好了的image,可以直接下载运行,而我们想要创建自己的,就可以使用Dockerfile,想必大家用过ant的build.xml来变编译,构建程序代码,那么这个Dockerfile就和它类似;

在这一章节里,Docker向我们展示了一个创建image及container的例子,看一个这个Dockerfile的代码:

# Use an official Python runtime as a parent 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 environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]
在这个Dockerfile中,它为我们安装了python及一系列的环境,并在container启动时为我们准备了一些命令执行,最终这个文件以及它的附属文件为我们创造了一个image,运行之后就成了一个container;

基本命令如下:

docker build -t friendlyname .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname         # Same thing, but in detached mode
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container
docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls -a -q)         # Remove all containers
docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls -a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry



第三部分:Services(服务),这一部分主要说的是一个service下的多个运行的container实例,之前我们都是一个image运行出一个container,而在这里,我们在一个service下可以同时运行一个image的多个container,这和进程与线程的关系有点相似;

这里主要用到docker-compose.yml这个构建文件,名字无所谓,可以自己起,看一个yml文件,

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/repo:tag
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:
yml在一个services中定义了一个web,这是许多service中的一个,web中定义了image,部署个数,cpu限制,内存限制等等,用这个yml文件可以一个拥有5个进程的服务(暂时这么说),那我们再请求时就会从这5个中间找一个来响应了;

一些基本命令:

docker stack ls                                            # List stacks or apps
docker stack deploy -c <composefile> <appname>  # Run the specified Compose file
docker service ls                 # List running services associated with an app
docker service ps <service>                  # List tasks associated with an app
docker inspect <task or container>                   # Inspect task or container
docker container ls -q                                      # List container IDs
docker stack rm <appname>                             # Tear down an application

第四部分: Swarms(应该是集群的意思),这部分呢和services比较相似,只不过这里是集群和节点的概念,不再是在一台机器上,而是模拟在许多台机器上面,和数据库集群是不是很相似呢?

api里就模拟了2台机器,但是这里需要注意的是,我们操作是只能在一台机器上面(主机器),按照step 3相似的操作后,你会发现现在的服务是在不同的机器上面,称之为node,


相关命令:

docker-machine create --driver virtualbox myvm1 # Create a VM (Mac, Win7, Linux)
docker-machine create -d hyperv --hyperv-virtual-switch "myswitch" myvm1 # Win10
docker-machine env myvm1                # View basic information about your node
docker-machine ssh myvm1 "docker node ls"         # List the nodes in your swarm
docker-machine ssh myvm1 "docker node inspect <node ID>"        # Inspect a node
docker-machine ssh myvm1 "docker swarm join-token -q worker"   # View join token
docker-machine ssh myvm1   # Open an SSH session with the VM; type "exit" to end
docker-machine ssh myvm2 "docker swarm leave"  # Make the worker leave the swarm
docker-machine ssh myvm1 "docker swarm leave -f" # Make master leave, kill swarm
docker-machine start myvm1            # Start a VM that is currently not running
docker-machine stop $(docker-machine ls -q)               # Stop all running VMs
docker-machine rm $(docker-machine ls -q) # Delete all VMs and their disk images
docker-machine scp docker-compose.yml myvm1:~     # Copy file to node's home dir
docker-machine ssh myvm1 "docker stack deploy -c <file> <app>"   # Deploy an app


第五部分:Stacks(栈s?这里我也无法给出明确的释义),但我们可以结合Services来看,

在这里,官方api好像实在对Services进行扩展,通过在Services中加内容构造出了Stacks的概念,

visualizer:

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/repo:tag
    deploy:
      replicas: 5
      restart_policy:
        condition: on-failure
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
    ports:
      - "80:80"
    networks:
      - webnet
  visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]
    networks:
      - webnet
networks:
  webnet:



redis:

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/repo:tag
    deploy:
      replicas: 5
      restart_policy:
        condition: on-failure
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
    ports:
      - "80:80"
    networks:
      - webnet
  visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]
    networks:
      - webnet
  redis:
    image: redis
    ports:
      - "6379:6379"
    volumes:
      - /home/docker/data:/data
    deploy:
      placement:
        constraints: [node.role == manager]
    command: redis-server --appendonly yes
    networks:
      - webnet
networks:
  webnet:




第六部分:Deploy your app(部署应用),

如果可以的话,用Docker Cloud账户就能搞定,像git hub一样,当然啦,还有另一种方法是通过Docker desktop app(也就是客户端啦~);其实啦,命令行更能......你懂得~


Docker对我来说也是新知识,还需要多多学习,如果大家想看的话,建议先从网上找点中文的资料看看Docker是什么,然后再去看官方的api,说的很详细,英文的,要慢慢看,基本能看到所有的内容,然后再自己琢磨,英文好很重要啊~~~,刚刚看完,再琢磨琢磨~

posted on 2017-10-03 12:11  Arry  阅读(184)  评论(0编辑  收藏  举报