ドッカー(B):容器コンテナ

ドッカー階層は下から3層に分けることができます:容器(コンテナ)、サービス(サービス)、コンテナサービスの動作を定義スタック(スタック)、スタックのインタラクティブサービス定義する
次の試みは、使用する方法でありますドッキングウィンドウは、コンテナ内のアプリケーションを起動します

コンテナを作成します。

  1. Dockerfileが含まれている空のフォルダを作成し、app.py、次の3つのファイル、ファイルの内容はrequirements.txt

Dockerfile

# 使用python的运行环境作为parent image
FROM python:2.7-slim

# 设置工作路径
WORKDIR /app

# 将当前目录的内容复制到相应路径下
COPY . /app

# 下载requirements.txt中要求的包
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# 暴露容器的80端口
EXPOSE 80

# 定义环境变量
ENV NAME World

#当docker开始运行时执行文件
CMD ["python", "app.py"]

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__":

アプリケーションの構築

アプリケーションを構築するには、次のコマンドを使用します

docker build --tag=friendlyhello  .
# or  docker build -t=friendlyhello  .

使用docker image lsイメージの建物を見ることができます

 $  docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
friendlyhello       latest              7ecc82fd960a        53 minutes ago      148MB
python              2.7-slim            5f759d36aead        7 hours ago         137MB
hello-world         latest              fce289e99eb9        7 months ago        1.84kB

既定されたタグlatestは、使用することができます--tag=friendlyhello:v0.0.1.タグを指定します

アプリケーションを実行します。

プログラムを実行するには、次のステートメントを使用します

docker run -p 4000:80 friendlyhello

どの-p 4000:80ホストポート4000へのポートの地図画像80を示しています。
ランは、http://localhost:4000プログラムの結果を表示することができます
使用することを-dプログラムがバックグラウンドで実行させることができます

$ docker run -d -p 4000:80 friendlyhello
c21b81020e77e9f15df5fafbfdaf2791599c6233b4169615ea9226f243ff68b8

$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                  NAMES
c21b81020e77        friendlyhello       "python app.py"     About a minute ago   Up About a minute   0.0.0.0:4000->80/tcp   elegant_raman

# 关闭程序
$ docker container stop c21b

$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

関連コマンド

docker build -t friendlyhello .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello  # Run "friendlyhello" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello         # 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

おすすめ

転載: www.cnblogs.com/yezhh/p/11271626.html
おすすめ