美文网首页
Docker 学习之构建及运行镜像

Docker 学习之构建及运行镜像

作者: seventeencm | 来源:发表于2019-06-18 22:16 被阅读0次

    前言

    接上篇,我们已经把docker的环境搭建完毕。下面将按官网的教程进行相关的入门操手。官网的教程地址:Get Started, Part 2: Containers

    Dockerfilew配置容器

    1. 在宿主机上建立一个目录 如demo-docker
    2. cd demo-docker
    3. vi Dockerfile
    4. 在文件中录入如下内容:
    # Use an official Python runtime as a parent image
    # 我们使用官方提供的Python运行库作为父镜像
    FROM python:2.7-slim
    
    # Set the working directory to /app
    # 设置工程目录路径 /app
    WORKDIR /app
    
    # Copy the current directory contents into the container at /app
    # 复制当前文件夹下的文件到工程路径 /app下
    ADD . /app
    
    # Install any needed packages specified in requirements.txt
    # 安装在requirements.txt中列出的依赖包
    RUN pip install --trusted-host pypi.python.org -r requirements.txt
    
    # Make port 80 available to the world outside this container
    # 对外暴露本容器80端口
    EXPOSE 80
    
    # Define environment variable
    # 定义环境变量
    ENV NAME World
    
    # Run app.py when the container launches
    # 当容器运行时,执行的命令
    CMD ["python", "app.py"]
    
    1. vi requirements.txt,并录入如下内容
    Flask
    Redis
    
    1. vi 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)
    
    1. 最后在demo-docker中将有如下文件
    app.py  Dockerfile  requirements.txt
    
    1. 创建docker镜像
    docker build -t demo-docker .
    
    1. 运行完毕后,我们查看一下系统存在的docker镜像
    docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    demo-docker         latest              321a0064dbe2        About an hour ago   148MB
    python              2.7-slim            b0259cf63993        7 days ago          138MB
    hello-world         latest              725dcfab7d63        8 days ago          1.84kB
    
    1. 运行我们的镜像
    # 将本机的4000端口映射到容器的80端口
    docker run -p 4000:80 demo-docker
    
    1. 在后台运行
    docker run -d -p 4000:80 demo-docker
    
    1. 启动后我们将得到容器的ID信息,停止的时候,我们将会使用到此ID.另外我们还可以通过命令来查看相关的ID
    docker container ls
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
    83dd2473dc19        demo-docker         "python app.py"     7 seconds ago       Up 6 seconds        0.0.0.0:4000->80/tcp   pensive_yalow
    
    1. 停止在后台运行的容器
    docker container stop 83dd2473dc19
    
    1. docker的一些常用命令
    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 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
    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
    

    一些常见问题

    # 当因网络问题,安装一些依赖失败的时候,会发现Docker的镜像并没有构建完成,而且在
    docker image ls
    中可看到相关的信息。
    此时,我们先运行
    docker ps -a
    如果存在容器,则先把容器给删除掉
    docker rm <cotainerID>
    再执行
    docker rmi <imageID>
    
    

    相关文章

      网友评论

          本文标题:Docker 学习之构建及运行镜像

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