Dockerservice啟動(dòng)的方法是什么

這篇文章主要介紹“Docker service啟動(dòng)的方法是什么”,在日常操作中,相信很多人在Docker service啟動(dòng)的方法是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Docker service啟動(dòng)的方法是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

成都創(chuàng)新互聯(lián)公司主營長安網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),長安h5小程序制作搭建,長安網(wǎng)站營銷推廣歡迎長安等地區(qū)企業(yè)咨詢

 Containers

#########################  Dockerfile  #########################
# Dockerfile文件定義了image環(huán)境,工作空間,網(wǎng)絡(luò)端口,執(zhí)行命令
#
# 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"]
#########################  requirements.txt  #########################
# 應(yīng)用依賴
#
Flask
redis
#########################  app.py  #########################
#應(yīng)用
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 = "<h4>Hello {name}!</h4>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<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)
# 宿主機(jī)新建image目錄
$ ls
Dockerfile		app.py			requirements.txt
# docker命令會(huì)把指令發(fā)送給指定的machine,默認(rèn)是本機(jī),可通過docker-machine env 虛擬機(jī)
docker build -t friendlyname .  # Create image using this directory's Dockerfile
# 注冊image到machine的image注冊表
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
# 運(yùn)行image,以container形式
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
# 刪除image需要停止相關(guān)的container
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
# 標(biāo)記image
docker push username/repository:tag            # Upload tagged image to registry
# 將標(biāo)記的image發(fā)布到Docker賬號/存儲庫
docker run -p 4000:80 username/repository:tag                   # Run image from a registry
服務(wù)器端口:服務(wù)端口

Service

#########################  docker-compose.yml  #########################
version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: 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:
# 從注冊表中拉出上傳的圖像。
# 運(yùn)行該映像的5個(gè)實(shí)例作為調(diào)用的服務(wù)web,限制每個(gè)實(shí)例使用,最多使用10%的CPU(跨所有內(nèi)核)和50MB的RAM。
# 如果發(fā)生故障,立即重新啟動(dòng)容器。
# 將主機(jī)上的端口80映射到web80端口。
# 指示web容器通過稱為負(fù)載平衡網(wǎng)絡(luò)共享端口80 webnet。(在內(nèi)部,集裝箱本身將web在短暫的港口發(fā)布到 80號港口。)
# webnet使用默認(rèn)設(shè)置(這是一個(gè)負(fù)載平衡的覆蓋網(wǎng)絡(luò))來定義網(wǎng)絡(luò)。

以service啟動(dòng)

docker-compose up
$ docker-compose ps
WARNING: Some services (visualizer, web) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
      Name             Command      State           Ports          
------------------------------------------------------------------
new2_visualizer_1   npm start       Up      0.0.0.0:8083->8080/tcp 
new2_web_1          python app.py   Up      0.0.0.0:4003->80/tcp

 以stack啟動(dòng)

docker stack ls                                            # List stacks or apps
docker swarm init
docker stack deploy -c docker-compose.yml getstartedlab  # Run the specified Compose file
docker service ls                          # List running services associated with an app
docker service ps getstartedlab_web     # List tasks associated with an app
curl 192.168.2.249
docker stack rm getstartedlab      # Tear down an application
docker swarm leave --force      # Take down the swarm
            
docker inspect <task or container>                   # Inspect task or container
docker container ls -q                                      # List container IDs

Swarms

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 create --engine-insecure-registry 192.168.1.112:5000 x-node1
dicker-machine ls
NAME       ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
x-master   *        virtualbox   Running   tcp://192.168.99.131:2376           v17.09.0-ce   
x-node1    -        virtualbox   Running   tcp://192.168.99.132:2376           v17.09.0-ce
docker-machine env x-master                # View basic information about your node
docker-machine ssh x-master "docker swarm init --advertise-addr <x-master ip>"
docker-machine ssh x-master "docker node ls"         # List the nodes in your swarm
docker-machine ssh x-master "docker swarm join-token manager"
docker-machine ssh x-node1 "docker swarm join --token SWMTKN-1-1mw72ip89hsz351lbbhvuj97nj4x5q5vs6zk1zidtcs093isvq-7y6kwg6emzyhokesi7zn7d1qt 192.168.99.131:2377"
docker-machine ssh x-master "docker node ls"         # List the nodes in your swarm
docker stack deploy -c docker-compose.yml getstartedlab # swarms開啟service
docker stack ps getstartedlab
curl 192.168.2.249
curl 192.168.99.131
curl 192.168.99.132
docker stack rm getstartedlab

Stacks

#########################  docker-compose.yml  #########################
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:
# 增加stack的service

到此,關(guān)于“Docker service啟動(dòng)的方法是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

文章標(biāo)題:Dockerservice啟動(dòng)的方法是什么
網(wǎng)頁鏈接:http://www.muchs.cn/article6/ishgig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、虛擬主機(jī)、網(wǎng)站改版、品牌網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)