千家信息网

Docker service启动的方法是什么

发表于:2025-02-06 作者:千家信息网编辑
千家信息网最后更新 2025年02月06日,这篇文章主要介绍"Docker service启动的方法是什么",在日常操作中,相信很多人在Docker service启动的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希
千家信息网最后更新 2025年02月06日Docker service启动的方法是什么

这篇文章主要介绍"Docker service启动的方法是什么",在日常操作中,相信很多人在Docker service启动的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Docker service启动的方法是什么"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Containers

#########################  Dockerfile  ########################## Dockerfile文件定义了image环境,工作空间,网络端口,执行命令## Use an official Python runtime as a parent imageFROM python:2.7-slim# Set the working directory to /appWORKDIR /app# Copy the current directory contents into the container at /appADD . /app# Install any needed packages specified in requirements.txtRUN pip install -r requirements.txt# Make port 80 available to the world outside this containerEXPOSE 80# Define environment variableENV NAME World# Run app.py when the container launchesCMD ["python", "app.py"]
#########################  requirements.txt  ########################## 应用依赖#FlaskRedis
#########################  app.py  ##########################应用from flask import Flaskfrom redis import Redis, RedisErrorimport osimport socket# Connect to Redisredis = 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 = "cannot connect to Redis, counter disabled"    html = "

Hello {name}!

" \ "Hostname: {hostname}
" \ "Visits: {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)
# 宿主机新建image目录$ lsDockerfile              app.py                  requirements.txt# docker命令会把指令发送给指定的machine,默认是本机,可通过docker-machine env 虚拟机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 80docker run -d -p 4000:80 friendlyname         # Same thing, but in detached mode# 运行image,以container形式docker container ls                                # List all running containersdocker container ls -a             # List all containers, even those not runningdocker container stop            # Gracefully stop the specified containerdocker container kill          # Force shutdown of the specified containerdocker container rm         # Remove specified container from this machinedocker container rm $(docker container ls -a -q)         # Remove all containersdocker image ls -a                             # List all images on this machinedocker image rm             # Remove specified image from this machinedocker image rm $(docker image ls -a -q)   # Remove all images from this machine# 删除image需要停止相关的containerdocker login             # Log in this CLI session using your Docker credentialsdocker tag  username/repository:tag  # Tag  for upload to registry# 标记imagedocker push username/repository:tag            # Upload tagged image to registry# 将标记的image发布到Docker账号/存储库docker run -p 4000:80 username/repository:tag                   # Run image from a registry服务器端口:服务端口

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:      - webnetnetworks:  webnet:# 从注册表中拉出上传的图像。# 运行该映像的5个实例作为调用的服务web,限制每个实例使用,最多使用10%的CPU(跨所有内核)和50MB的RAM。# 如果发生故障,立即重新启动容器。# 将主机上的端口80映射到web80端口。# 指示web容器通过称为负载平衡网络共享端口80 webnet。(在内部,集装箱本身将web在短暂的港口发布到 80号港口。)# webnet使用默认设置(这是一个负载平衡的覆盖网络)来定义网络。

以service启动

docker-compose up$ docker-compose psWARNING: 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启动

docker stack ls                                            # List stacks or appsdocker swarm initdocker stack deploy -c docker-compose.yml getstartedlab  # Run the specified Compose filedocker service ls                          # List running services associated with an appdocker service ps getstartedlab_web     # List tasks associated with an appcurl 192.168.2.249docker stack rm getstartedlab      # Tear down an applicationdocker swarm leave --force      # Take down the swarm            docker inspect                    # Inspect task or containerdocker 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 # Win10docker-machine create --engine-insecure-registry 192.168.1.112:5000 x-node1dicker-machine lsNAME       ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORSx-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-cedocker-machine env x-master                # View basic information about your nodedocker-machine ssh x-master "docker swarm init --advertise-addr "docker-machine ssh x-master "docker node ls"         # List the nodes in your swarmdocker-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 swarmdocker stack deploy -c docker-compose.yml getstartedlab # swarms开启servicedocker stack ps getstartedlabcurl 192.168.2.249curl 192.168.99.131curl 192.168.99.132docker 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:      - webnetnetworks:  webnet:# 增加stack的service

到此,关于"Docker service启动的方法是什么"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0