编写镜像脚本Dockerfile文件
# 基础镜像
FROM python:3-alpine
# 维护者信息
MAINTAINER yunxingluoyun
#作者
LABEL author="yunxingloyun"
# 项目描述
LABEL description="Dockerfile for Python script which prints Hello, Name"
# 复制文件
COPY hello_world.py /root/dockerfile/
# 设置变量
ENV NAME=Sathya
# 设置环境变量
ENV MYPATH /root/dockerfile/
# 设置工作路径
WORKDIR $MYPATH
# 运行文件中的命令
CMD python3 /root/dockerfile/hello_world.py
hello_world.py文件
from os import getenv
if getenv('NAME') is None:
name = 'World!'
else:
name = getenv('NAME')
print("Hello {}".format(name))
构建镜像
root@yunxingluoyun:~/dockerfile# docker build -t py:h .
Sending build context to Docker daemon 4.096kB
Step 1/9 : FROM python:3-alpine
---> fbfb63e3c6bb
Step 2/9 : MAINTAINER yunxingluoyun<672319707@qq.com>
---> Running in 503508fc9557
Removing intermediate container 503508fc9557
---> f16ab6908eb0
Step 3/9 : LABEL author="yunxingloyun"
---> Running in 40a02ccc51aa
Removing intermediate container 40a02ccc51aa
---> d7fa14e5478d
Step 4/9 : LABEL description="Dockerfile for Python script which prints Hello, Name"
---> Running in b8678b26d975
Removing intermediate container b8678b26d975
---> 8eb46d841556
Step 5/9 : COPY hello_world.py /root/dockerfile/
---> dda8add216c1
Step 6/9 : ENV NAME=Sathya
---> Running in 524203c1ef8d
Removing intermediate container 524203c1ef8d
---> c7f820ca768c
Step 7/9 : ENV MYPATH /root/dockerfile/
---> Running in 9d7a502b4a2b
Removing intermediate container 9d7a502b4a2b
---> afbcbf13f17e
Step 8/9 : WORKDIR $MYPATH
---> Running in c3247158149c
Removing intermediate container c3247158149c
---> 24ec46fda06d
Step 9/9 : CMD python3 /root/dockerfile/hello_world.py
---> Running in bfe24fd53a00
Removing intermediate container bfe24fd53a00
---> 4e33fbe14312
Successfully built 4e33fbe14312
Successfully tagged py:h
运行镜像:
root@yunxingluoyun:~/dockerfile# docker run py:h
Hello Sathya
root@yunxingluoyun:~/dockerfile# docker run -e NAME=xingyun py:h
Hello xingyun