Win7搭建Docker环境+Helloworld测试容器(二)

接下来,我们按照官网继续操作

操作前需要在官网注册帐号 

1.分享我们的镜像(镜像和容器区别,转载 http://www.cnblogs.com/zhangmingcheng/p/5718042.html

在Docker终端执行以下命令,将镜像上传到您的仓库中,类似GIT命令

docker login  #我登陆的是imily帐号
docker tag friendlyhello imily/repository:hello    
docker push imily/repository:hello   #推送镜像到imily仓库

 2.运行仓库中的镜像

docker run -p 4000:80 imily/repository:hello   #运行镜像

3.利用容器建立负载均衡测试,

建立docker-compose.yml文件,可创建容器 服务

version: "3"
services:
  web:
    image: imily/repository:hello
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:

4.运行容器的5个实例,来进行负载均衡测试

docker swarm init --advertise-addr 192.168.99.100 #换成你的IP
docker stack deploy -c docker-compose.yml helloapp   #运行实例
docker stack ps helloapp  #此命令可查看容器运行栈,会显示5个实例

打开浏览器可看到结果,刷新后会显示不同的hostname

运行期间可直接修改docker-compose.yml文件并运行命令更新 docker stack deploy -c docker-compose.yml helloapp

停止运行可运行如下命令

docker stack rm helloapp

 5.集群测试

docker-machine create --driver virtualbox myvm1 #创建虚拟机1
docker-machine create --driver virtualbox myvm2 #创建虚拟机2
docker-machine ssh myvm1 "docker swarm init --advertise-addr 192.168.99.101:2377"  #利用ssh, 让myvm1执行命令docker swarm init,使之称为管理员

#小提示:可执行  docker-machine ssh myvm1  #转到myvm1虚拟机shell下,运行完后执行exit退出
导出TOKEN :
docker-machine ssh myvm1 "docker swarm join-token --quiet worker" > token.txt
docker-machine ssh myvm2  #转到myvm2下,执行
docker swarm join --token 你的token 192.168.99.101:2377
成功执行后
exit #返回到主shell控制台
docker-machine scp docker-compose.yml myvm1:~  #复制docker-compose.yml文件到myvm1用户目录
docker-machine ssh myvm1 "docker stack deploy -c docker-compose.yml helloapp"  #运行容器服务

 此时打开 192.168.99.101 会打不开,这时候需要进入VirtualBox设置myvm1和myvm2网络,打开端口

 

然后就可以在浏览器打开 http://192.168.99.101  和 http://192.168.99.102 了

 

 

显示更多

Win7搭建Docker环境+Helloworld测试容器(一)

目前Windows Docker 支持 Windows 10 Professional or Enterprise 64-bit.

win7或win8需要下载 Docker Toolbox 下载地址:https://download.docker.com/win/stable/DockerToolbox.exe

我自己操作系统为win7

1.安装DockerToolbox后桌面会出现三个图标

2.运行官方HelloWorld,直接打开Docker Terminal  输入:

docker run hello-world

运行后正常情况下会显示

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
省略...

如果不正常尝试连接VPN试试 3.创建一个简单的容器实例: 我在桌面创建了一个文件夹,有三个文件 文件内容如下 DockerFile :

# Use an official Python runtime as a base image
FROM python

# 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 "app/requirements.txt"
RUN pip install -r  "app/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/app.py"

requirements.txt

Flask Redis 

app.py

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "</code></pre>
<h3>Hello {name}!</h3>
<pre><code></code></pre>
" \ "<b>Hostname:</b> {hostname}
" \ "<b>Visits:</b> {visits}" 
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits) 
if __name__ == "__main__": 
    app.run(host='0.0.0.0', port=80)

4.生成一个实例,输入命令(注意当前目录为Docker-Test):docker build -t friendlyhello .   5.运行生成的实例:docker run -d -p 4001:80 friendlyhello 正常的话 浏览器输入容器IP:4001就可以访问了, 我的是http://192.168.99.100:4001/  

显示更多