dockerfile测试

//this is index.html file content 
<h1>Hello Docker</h1>  
//this is build dockfile
FROM nginx:1.21.0-alpine
ADD index.html /usr/share/nginx/html/index.html
//build
docker image build -t mynginx-alpine .

dockerfile RUN测试

FROM ubuntu:20.04
RUN apt-get update && \
    apt-get install -y wget && \
    wget https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.1/ipinfo_2.0.1_linux_amd64.tar.gz && \
    tar zxf ipinfo_2.0.1_linux_amd64.tar.gz && \
    mv ipinfo_2.0.1_linux_amd64 /usr/bin/ipinfo && \
    rm -rf ipinfo_2.0.1_linux_amd64.tar.gz
#build
docker image build -f Dockerfile -t ipinfo-good .   # -f 指定文件
docker image history xxx #查看镜像分层
docker image build -f  Dockerfile-arg  -t  ipinfo-arg-2.0.0  --bulid-arg VERSION=2.0.0 .
docker image prune -a  #删除镜像
docker system prune -f  #删除未运行的容器
docker container run --rm -it ipinfo ipinfo 8.8.8.8  #退出删除

docker 启动flask程序

#flask app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello, World!'
#Dockerfile
FROM python:3.9.5-slim
COPY app.py /src/app.py
RUN pip install flask
WORKDIR /src
ENV FLASK_APP=app.py
EXPOSE 5000
CMD ["flask", "run", "-h", "0.0.0.0"]
#Run
docker image build -t flask-demo .
docker container run -d -p 80:5000 flask-demo

dockerfile构建

//记得编写.dockerignore文件 用来忽略文件
.dockerignore 
  .vscode
  env

docker多阶段构建

FROM gcc:9.4 AS builder
COPY hello.c /src/hello.c
WORKDIR /src
RUN gcc --static -o hello hello.c

FROM alpine:3.13.5
COPY --from=builder /src/hello /src/hello
ENTRYPOINT [ "/src/hello" ]
CMD []

docker使用非root用户

FROM python:3.9.5-slim
RUN pip install flask && \
    groupadd -r flask && useradd -r -g flask flask && \
    mkdir /src && \
    chown -R flask:flask /src

USER flask
COPY app.py /src/app.py
WORKDIR /src
ENV FLASK_APP=app.py
EXPOSE 5000
CMD ["flask", "run", "-h", "0.0.0.0"]