Write a note + small example Dockerfile file

First write a simple program flask

vim app.py

from flask import Flask
import redis

app = Flask(__name__)
# 链接redis做容器间关联验证效果用
rd = redis.Redis(host='redis',port=6379)

@app.route('/')
def hello():
    # Redis Incr 命令将 key 中储存的数字值增一。
    # 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。
    # 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
    # 本操作的值限制在 64 位(bit)有符号数字表示之内。
    count = rd.incr("hits")
    return "hello world".format(count)

if __name__ == "__main__":
    app.run(host="0.0.0.0",port=5000)


Write Dockerfile

vim Dockerfile

Dockerfile generally divided into four parts: the base image information, perform a maintainer information, instructions and image containers start command.

FROM python:alpine
COPY . /code
RUN pip install flask redis
WORKDIR /code
EXPOSE 5000
CMD ["python","app.py"]

Common command interpreter:

FROM : Specify a base image (parent image), the first row must, when creating a plurality Dockerfile same mirror may be used a plurality FROM instruction.

FROM python:alpine
     镜像名:版本

COPY : The contents of a directory of the host to the mirror copy in the current directory

COPY host-Dir image-dir
     [主机目录] [镜像目录] 

The RUN : Run mirrored container, there are two ways command:

# 1、shell执行
RUN <command>
# 2、exec执行
RUN ["executable", "param1", "param2"]

WORKDIR : Configuring working directory. For subsequent RUN, CMD, ENTRYPOINT instructions to configure the working directory. WORKDIR plurality of instructions may be used, if the subsequent command parameter is a relative path, before the command will be specified based on the path.

EXPOSE : port number of the container exposed. When you start Docker containers need to assign a host via a port forwarding -P parameter to the specified port. The -p parameter you can specify which port is mapped over the host.

CMD : executing instructions when a container starts, every Dockerfile only a CMD command

The VOLUME : Create a can mount from a local mount point or other containers, generally used to store databases and data needs to be retained.

VOLUME ["/data"]
# 指令添加多个数据卷
VOLUME ["/data1", "/data2"]

Construction of container

docker build -t myweb .

-t: Specifies the image name 

The directory is created when specified, the directory can post dockerfile file COPY is at the same level

Boot image

docker run -d --rm --link redis -p 8010: 5000 myweb # --link binding redis container

发布了73 篇原创文章 · 获赞 188 · 访问量 117万+

Guess you like

Origin blog.csdn.net/GodDavide/article/details/104112805