美文网首页
Docker——初级学习

Docker——初级学习

作者: 草样逍遥 | 来源:发表于2019-09-30 11:23 被阅读0次

    Docker——初级学习

    标签:docker


    学习内容

    1. 学习镜像、容器和虚拟机的区别
    2. 学习使用Dockerfile来定义一个容器
    3. 学习将本地容器push,再从远程pull

    镜像、容器和虚拟机

    镜像与容器

    A container is launched by running an image. An image is an executable package that includes everything needed to run an application--the code, a runtime, libraries, environment variables, and configuration files.

    通过运行一个镜像来发起一个容器。镜像是一个可执行的包,包含了需要运行一个应用的所有东西,如代码、运行时间、库、环境变量以及配置文件等。

    A container is a runtime instance of an image--what the image becomes in memory when executed (that is, an image with state, or a user process). You can see a list of your running containers with the command, docker ps, just as you would in Linux.

    容器是镜像的运行时实例。镜像被执行时,是在内存中的。

    容器与虚拟机

    A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.

    容器在Linux上本地运行,并与其他容器共享主机的内核。它运行一个离散进程,不占用任何其他可执行文件更多内存,从而使其轻巧。

    By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.

    相反,虚拟机运行成熟的“guest”操作系统,并通过虚拟机管理程序来对主机资源进行访问。通常,VM为环境提供的资源比大多数应用程序所需要的资源更多。

    准备Docker环境

    测试Docker版本

        docker --version #确定docker版本
        docker info (or docker version) #查看更多细节
    

    测试Docker安装

    • 通过运行一个简单的Docker镜像(hello-world)来测试安装可工作
        docker run hello-world
        Unable to find image 'hello-world:latest' locally
        latest: Pulling from library/hello-world
        ca4f61b1923c: Pull complete
        Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
        Status: Downloaded newer image for hello-world:latest
        Hello from Docker!
        This message shows that your installation appears to be working correctly.
        ...
    
    • 列出下载到本地的hello-world镜像
        docker image ls
    
    • 列出hello-world容器,如果容器还在运行,则不需要--all
        docker container ls --all
    

    用Dockerfile来定义容器

    • 创建一个新的文件夹,在次文件夹下新建名为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
    COPY . /app
    
    # Install any needed packages specified in requirements.txt
    RUN pip install --trusted-host pypi.python.org -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和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)
    
    • 构建app
      进入到新建的文件夹后,可以看到这里有三个文件
    $ ls
    Dockerfile      app.py          requirements.txt
    

    然后运行构建命令,创建一个docker镜像,使用--tag来命名

    docker build --tag=friendlyhello .
    
    • 运行app
    docker run -p 4000:80 friendlyhello                   
     * Serving Flask app "app" (lazy loading)
     * Environment: production
       WARNING: This is a development server. Do not use it in a production deployment.
       Use a production WSGI server instead.
     * Debug mode: off
     * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
    

    在浏览器中输入http://localhost:4000可以看到结果,也可以使用curl

     curl http://localhost:4000
    
    <h3>Hello World!</h3><b>Hostname:</b> 8fc990912a14<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>
    

    也可以在后台运行:

    docker run -d -p 4000:80 friendlyhello
    

    使用docker container stop来停止进程:

    共享镜像

    登陆Docker

    $ docker login
    

    标记镜像

    docker tag image username/repository:tag
    

    例如:

    docker tag friendlyhello xiaoyaomakevin/get-started:part2
    

    Publish镜像

    docker push username/repository:tag
    

    例如

    docker push xiaoyaomakevin/get-started:part2
    

    从远程端拉取并运行镜像

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

    例如:

    docker run -p 4000:80 xiaoyaomakevin/get-started:part2
     * Serving Flask app "app" (lazy loading)
     * Environment: production
       WARNING: This is a development server. Do not use it in a production deployment.
       Use a production WSGI server instead.
     * Debug mode: off
     * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
    

    相关文章

      网友评论

          本文标题:Docker——初级学习

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