浏览代码

docker multi stage build update

kevin 2 年之前
父节点
当前提交
0970620b81
共有 11 个文件被更改,包括 60 次插入58 次删除
  1. 3 1
      .dockerignore
  2. 1 43
      Dockerfile
  3. 35 0
      Dockerfile.base
  4. 7 0
      Dockerfile.req
  5. 1 5
      build_docker_base.sh
  6. 3 0
      build_docker_req.sh
  7. 2 2
      gunicorn.conf.py
  8. 1 2
      requirements.txt
  9. 0 5
      run.sh
  10. 4 0
      start_docker.sh
  11. 3 0
      stop_docker.sh

+ 3 - 1
.dockerignore

@@ -7,7 +7,9 @@ mysql
 Dockerfile
 configure.sh
 build_docker_base.sh
-run_docker.sh
+build_docker_req.sh
+start_docker.sh
+stop_docker.sh
 face_align.py
 hello*
 main.py

+ 1 - 43
Dockerfile

@@ -1,49 +1,7 @@
-FROM python:3.10-bullseye AS fr_base
+FROM fr_req AS fr
 
 WORKDIR /usr/src/app
 
-COPY requirements.txt .
-
-RUN apt-get -y update && \
-    apt-get -y upgrade && apt-get install --no-install-recommends -y --fix-missing \
-    build-essential \
-    cmake \
-    gfortran \
-    git \
-    wget \
-    curl \
-    graphicsmagick \
-    libgraphicsmagick1-dev \
-    libatlas-base-dev \
-    libavcodec-dev \
-    libavformat-dev \
-    libgtk2.0-dev \
-    libjpeg-dev \
-    liblapack-dev \
-    libswscale-dev \
-    pkg-config \
-    python3-dev \
-    software-properties-common \
-    zip \
-    nmap \
-    net-tools \
-    vim-tiny \
-    nano \
-    && apt-get clean && rm -rf /tmp/* /var/tmp/*
-
-RUN pip install numpy && \
-    mkdir -p dlib && \
-    git clone -b 'v19.9' --single-branch https://github.com/davisking/dlib.git dlib/ && \
-    cd dlib && python setup.py install --yes USE_AVX_INSTRUCTIONS && \
-    cd .. && pip install --no-cache-dir -r requirements.txt
-
-FROM fr_base AS fr
-
-COPY . .
-
-RUN chmod +x *.sh && \
-    python face_recognition_svm.py
-
 EXPOSE 8349
 
 CMD ["/usr/src/app/run.sh", "--no-venv", "--has-db"]

+ 35 - 0
Dockerfile.base

@@ -0,0 +1,35 @@
+FROM python:3.10-bullseye AS fr_base
+
+WORKDIR /usr/src/app
+
+RUN apt-get -y update && \
+    apt-get -y upgrade && apt-get install --no-install-recommends -y --fix-missing \
+    build-essential \
+    cmake \
+    gfortran \
+    git \
+    wget \
+    curl \
+    graphicsmagick \
+    libgraphicsmagick1-dev \
+    libatlas-base-dev \
+    libavcodec-dev \
+    libavformat-dev \
+    libgtk2.0-dev \
+    libjpeg-dev \
+    liblapack-dev \
+    libswscale-dev \
+    pkg-config \
+    python3-dev \
+    software-properties-common \
+    zip \
+    nmap \
+    net-tools \
+    vim-tiny \
+    nano \
+    && apt-get clean && rm -rf /tmp/* /var/tmp/*
+
+RUN pip install numpy && \
+    mkdir -p dlib && \
+    git clone -b 'v19.9' --single-branch https://github.com/davisking/dlib.git dlib/ && \
+    cd dlib && python setup.py install --yes USE_AVX_INSTRUCTIONS

+ 7 - 0
Dockerfile.req

@@ -0,0 +1,7 @@
+FROM fr_base AS fr_req
+
+COPY . .
+
+RUN pip install --no-cache-dir -r requirements.txt && \
+    chmod +x *.sh && \
+    python face_recognition_svm.py

+ 1 - 5
build_docker_base.sh

@@ -1,7 +1,3 @@
 #!/bin/bash
 
-if [ $1 == "--no-cache" ]; then
-  docker build --target fr_base --no-cache -t fr_base .
-else
-  docker build --target fr_base -t fr_base .
-fi
+docker build -f Dockerfile.base --target fr_base --no-cache -t fr_base .

+ 3 - 0
build_docker_req.sh

@@ -0,0 +1,3 @@
+#!/bin/bash
+
+docker build -f Dockerfile.req --target fr_req --no-cache -t fr_req .

+ 2 - 2
gunicorn.conf.py

@@ -8,8 +8,8 @@ preload_app = True
 def on_starting(server):
     worker_count = int(workers)
 
-    # Insert a 5-second gap between workers
-    gap_duration = 5
+    # Insert a 2-second gap between workers
+    gap_duration = 2
 
     for i in range(worker_count):
         # Add a delay between each worker

+ 1 - 2
requirements.txt

@@ -4,5 +4,4 @@ Flask
 scikit-learn
 gunicorn
 mysql-connector-python
-Pillow
-numpy
+Pillow

+ 0 - 5
run.sh

@@ -1,5 +0,0 @@
-#!/bin/bash
-
-EXT_PORT=8349
-TAG=latest
-docker run -it --rm -p "$EXT_PORT:8349" "kevhnmay94/face_recognition:$TAG"

+ 4 - 0
start_docker.sh

@@ -0,0 +1,4 @@
+#!/bin/bash
+
+docker-compose build --no-cache
+docker-compose up "$@"

+ 3 - 0
stop_docker.sh

@@ -0,0 +1,3 @@
+#!/bin/bash
+
+docker-compose down