美文网首页
入门-container-最底层

入门-container-最底层

作者: ifeelok0319 | 来源:发表于2017-06-15 23:50 被阅读60次

    最底层是container,第二层是service,最高层是stack

    前提

    • dockers version:1.13或者更高(17.03.1-ce)

    Dockerfile定制container

    dockerfile定义了container的环境里面的内容。在container里面的资源(如网络接口和磁盘驱动)都是虚拟的,和物理机分离的,所以必须映射端口、指定要copy的文件。

    创建一个空目录,创建一个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 environment variable
    ENV NAME World
    
    # Run app.py when the container launches
    CMD ["python", "app.py"]
    

    需要在container中运行的App

    Dockerfile的同级目录中添加两个文件:requirements.txtapp.py

    Dockerfile中的ADD命令将这两个文件存放在image中,EXPOSE命令可以让我们通过HTTP访问。

    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 = "<h3>Hello {name}!</h3>" \
               "<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)
    

    在一个container中获取hostname得到的是container ID

    构建这个app

    docker build -t friendlyhello .
    

    运行app

    docker run -p 4000:80 friendlyhello
    
    docker run -d -p 4000:80 friendlyhello
    
    docker ps
    
    docker stop 1fa4ab2cf395
    

    分享image

    A registry is a collection of repositories, and a repository is a collection of images.

    登陆docker id

    docker login

    image打标签

    本地image和一个registry中的一个repository建立联系的标记为username/repository:tag

    docker tag friendlyhello john/get-started:part1

    发布image

    docker push username/repository:tag

    从远程仓库获取image

    docker run -p 4000:80 username/repository:tag

    在构建和运行image的时候,如果没有指定:tag,将会使用:latest。运行的时候如果没有tag则运行:latest的版本(不见得是最新的版本)。

    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 ps                                 # See a list of all running containers
    docker stop <hash>                     # Gracefully stop the specified container
    docker ps -a           # See a list of all containers, even the ones not running
    docker kill <hash>                   # Force shutdown of the specified container
    docker rm <hash>              # Remove the specified container from this machine
    docker rm $(docker ps -a -q)           # Remove all containers from this machine
    docker images -a                               # Show all images on this machine
    docker rmi <imagename>            # Remove the specified image from this machine
    docker rmi $(docker images -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
    
    

    相关文章

      网友评论

          本文标题:入门-container-最底层

          本文链接:https://www.haomeiwen.com/subject/ercpqxtx.html