Quick & Dirty: Docker Anaconda Flask Hello World

Submitted by Xilodyne on Thu, 01/11/2024 - 15:26

Dockerfile "Hello World" (using Anaconda & Flask)

Using docker build to run a Flask application as described in the Docker Build documentation, https://docs.docker.com/build .

This example uses the Anaconda (Miniconda) Python environment, https://repo.anaconda.com/miniconda/Miniconda3-py310_23.11.0-2-Linux-x86_64.sh

Directory structure
~/hello_world
     - flask_conda.buildfile
      /src
          - hello.py
          - Miniconda3-py310_23.11.0-2-Linux-x86_64.sh
      /scripts
          - docker_build.sh
          - docker_run.sh

 

wget <link>
$ wget https://repo.anaconda.com/miniconda/Miniconda3-py310_23.11.0-2-Linux-x86_64.sh

 

 

hello.py
$ cat src/hello.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!\n"

 

 

docker_build.sh
$ cat scripts/docker_build.sh
docker build --progress=plain --no-cache -f ../flask_conda.buildfile -t flask_test:final_v1 ../

 

 

flask_conda.buildfile
$ cat flask_conda.buildfile
# https://docs.docker.com/build/building/packaging/ # syntax=docker/dockerfile:1
FROM ubuntu:22.04
SHELL ["/bin/bash", "-c"]
ENV PATH="${PATH}:/root/miniconda3/bin"

COPY src/hello.py /
COPY src/Miniconda3-py310_23.11.0-2-Linux-x86_64.sh /

RUN chmod 755 Miniconda3-py310_23.11.0-2-Linux-x86_64.sh ; \
    /Miniconda3-py310_23.11.0-2-Linux-x86_64.sh -b

# "conda activate" does not work in non-interactive session; use "conda run"
RUN conda create --yes --name dock_flask python==3.10; \
    conda init bash; \
    conda run -n dock_flask; \
    conda install -y anaconda::flask

# final configuration
ENV FLASK_APP=hello
EXPOSE 8000
CMD flask run --host 0.0.0.0 --port 8000

 

 

Run docker build results
scripts$ ./docker_build.sh
#0 building with "default" instance using docker driver

 

#1 [internal] load .dockerignore
#1 transferring context: 2B done
#1 DONE 0.0s

#2 [internal] load build definition from flask_conda.buildfile
#2 transferring dockerfile: 757B done
#2 DONE 0.0s

#3 [internal] load metadata for docker.io/library/ubuntu:22.04
#3 DONE 0.0s

#4 [1/5] FROM docker.io/library/ubuntu:22.04
#4 CACHED

#5 [internal] load build context
#5 transferring context: 238B 0.0s done
#5 DONE 0.0s

#6 [2/5] COPY src/hello.py /
#6 DONE 0.1s

#7 [3/5] COPY src/Miniconda3-py310_23.11.0-2-Linux-x86_64.sh /
#7 DONE 1.2s

#8 [4/5] RUN chmod 755 Miniconda3-py310_23.11.0-2-Linux-x86_64.sh ;     /Miniconda3-py310_23.11.0-2-Linux-x86_64.sh -b
#8 1.152 PREFIX=/root/miniconda3
#8 2.414 Unpacking payload ...

#8 16.86 Installing base environment...
#8 16.86
#8 20.26
#8 20.26 Downloading and Extracting Packages: ...working... done
#8 20.26
#8 20.26 Downloading and Extracting Packages: ...working... done
#8 20.26 Preparing transaction: ...working... done
#8 22.66 Executing transaction: ...working... done
#8 34.91 installation finished.
#8 DONE 35.4s

#9 [5/5] RUN conda create --yes --name dock_flask python==3.10;     conda init bash;     conda run -n dock_flask;     conda install -y anaconda::flask
#9 2.912 Channels:
#9 2.912  - defaults
#9 2.912 Platform: linux-64
#9 2.912 Collecting package metadata (repodata.json): ...working... done
#9 23.57 Solving environment: ...working... done
#9 39.57
#9 39.57 ## Package Plan ##
#9 39.57
#9 39.57   environment location: /root/miniconda3/envs/dock_flask
#9 39.57
#9 39.57   added / updated specs:
#9 39.57     - python==3.10
#9 39.57
#9 39.57
#9 39.57 The following packages will be downloaded:
#9 39.57
#9 39.57     package                    |            build
#9 39.57     ---------------------------|-----------------
#9 39.57     libffi-3.3                 |       he6710b0_2          50 KB
#9 39.57     openssl-1.1.1w             |       h7f8727e_0         3.7 MB
#9 39.57     python-3.10.0              |       h12debd9_5        23.5 MB
#9 39.57     tzdata-2023d               |       h04d1e81_0         117 KB
#9 39.57     ------------------------------------------------------------
#9 39.57                                            Total:        27.4 MB
#9 39.57
#9 39.57 The following NEW packages will be INSTALLED:
#9 39.57
#9 39.57   _libgcc_mutex      pkgs/main/linux-64::_libgcc_mutex-0.1-main
#9 39.57   _openmp_mutex      pkgs/main/linux-64::_openmp_mutex-5.1-1_gnu
#9 39.57   bzip2              pkgs/main/linux-64::bzip2-1.0.8-h7b6447c_0
#9 39.57   ca-certificates    pkgs/main/linux-64::ca-certificates-2023.12.12-h06a4308_0
#9 39.57   ld_impl_linux-64   pkgs/main/linux-64::ld_impl_linux-64-2.38-h1181459_1
#9 39.57   libffi             pkgs/main/linux-64::libffi-3.3-he6710b0_2
#9 39.57   libgcc-ng          pkgs/main/linux-64::libgcc-ng-11.2.0-h1234567_1
#9 39.57   libgomp            pkgs/main/linux-64::libgomp-11.2.0-h1234567_1
#9 39.57   libstdcxx-ng       pkgs/main/linux-64::libstdcxx-ng-11.2.0-h1234567_1
#9 39.57   libuuid            pkgs/main/linux-64::libuuid-1.41.5-h5eee18b_0
#9 39.57   ncurses            pkgs/main/linux-64::ncurses-6.4-h6a678d5_0
#9 39.57   openssl            pkgs/main/linux-64::openssl-1.1.1w-h7f8727e_0
#9 39.57   pip                pkgs/main/linux-64::pip-23.3.1-py310h06a4308_0
#9 39.57   python             pkgs/main/linux-64::python-3.10.0-h12debd9_5
#9 39.57   readline           pkgs/main/linux-64::readline-8.2-h5eee18b_0
#9 39.57   setuptools         pkgs/main/linux-64::setuptools-68.2.2-py310h06a4308_0
#9 39.57   sqlite             pkgs/main/linux-64::sqlite-3.41.2-h5eee18b_0
#9 39.57   tk                 pkgs/main/linux-64::tk-8.6.12-h1ccaba5_0
#9 39.57   tzdata             pkgs/main/noarch::tzdata-2023d-h04d1e81_0
#9 39.57   wheel              pkgs/main/linux-64::wheel-0.41.2-py310h06a4308_0
#9 39.57   xz                 pkgs/main/linux-64::xz-5.4.5-h5eee18b_0
#9 39.57   zlib               pkgs/main/linux-64::zlib-1.2.13-h5eee18b_0
#9 39.57
#9 39.57
#9 39.57
#9 39.57 Downloading and Extracting Packages: ...working... done
#9 39.57 Preparing transaction: ...working... done
#9 41.24 Verifying transaction: ...working... done
#9 47.50 Executing transaction: ...working... done
#9 69.99 #
#9 69.99 # To activate this environment, use
#9 69.99 #
#9 69.99 #     $ conda activate dock_flask
#9 69.99 #
#9 69.99 # To deactivate an active environment, use
#9 69.99 #
#9 69.99 #     $ conda deactivate
#9 69.99
#9 71.41 no change     /root/miniconda3/condabin/conda
#9 71.41 no change     /root/miniconda3/bin/conda
#9 71.41 no change     /root/miniconda3/bin/conda-env
#9 71.41 no change     /root/miniconda3/bin/activate
#9 71.41 no change     /root/miniconda3/bin/deactivate
#9 71.41 no change     /root/miniconda3/etc/profile.d/conda.sh
#9 71.41 no change     /root/miniconda3/etc/fish/conf.d/conda.fish
#9 71.41 no change     /root/miniconda3/shell/condabin/Conda.psm1
#9 71.41 no change     /root/miniconda3/shell/condabin/conda-hook.ps1
#9 71.41 no change     /root/miniconda3/lib/python3.10/site-packages/xontrib/conda.xsh
#9 71.41 no change     /root/miniconda3/etc/profile.d/conda.csh
#9 71.41 modified      /root/.bashrc
#9 71.41
#9 71.41 ==> For changes to take effect, close and re-open your current shell. <==
#9 71.41
#9 77.06 Channels:
#9 77.06  - defaults
#9 77.06  - anaconda
#9 77.06 Platform: linux-64
#9 77.06 Collecting package metadata (repodata.json): ...working... done
#9 89.58 Solving environment: ...working... done
#9 92.95
#9 92.95 ## Package Plan ##
#9 92.95
#9 92.95   environment location: /root/miniconda3
#9 92.95
#9 92.95   added / updated specs:
#9 92.95     - anaconda::flask
#9 92.95
#9 92.95
#9 92.95 The following packages will be downloaded:
#9 92.95
#9 92.95     package                    |            build
#9 92.95     ---------------------------|-----------------
#9 92.95     click-8.1.7                |  py310h06a4308_0         166 KB
#9 92.95     flask-2.2.2                |  py310h06a4308_0         137 KB  anaconda
#9 92.95     itsdangerous-2.0.1         |     pyhd3eb1b0_0          18 KB
#9 92.95     jinja2-3.1.2               |  py310h06a4308_0         215 KB
#9 92.95     markupsafe-2.1.3           |  py310h5eee18b_0          22 KB
#9 92.95     werkzeug-2.2.3             |  py310h06a4308_0         346 KB
#9 92.95     ------------------------------------------------------------
#9 92.95                                            Total:         903 KB
#9 92.95
#9 92.95 The following NEW packages will be INSTALLED:
#9 92.95
#9 92.95   click              pkgs/main/linux-64::click-8.1.7-py310h06a4308_0
#9 92.95   flask              anaconda/linux-64::flask-2.2.2-py310h06a4308_0
#9 92.95   itsdangerous       pkgs/main/noarch::itsdangerous-2.0.1-pyhd3eb1b0_0
#9 92.95   jinja2             pkgs/main/linux-64::jinja2-3.1.2-py310h06a4308_0
#9 92.95   markupsafe         pkgs/main/linux-64::markupsafe-2.1.3-py310h5eee18b_0
#9 92.95   werkzeug           pkgs/main/linux-64::werkzeug-2.2.3-py310h06a4308_0
#9 92.95
#9 92.95
#9 92.95
#9 92.95 Downloading and Extracting Packages: ...working... done
#9 92.95 Preparing transaction: ...working... done
#9 93.08 Verifying transaction: ...working... done
#9 93.22 Executing transaction: ...working... done
#9 DONE 95.4s

#10 exporting to image
#10 exporting layers
#10 exporting layers 43.0s done
#10 writing image sha256:60e0c977c2e803ccbd8042c07d0a330c1543b8a87fb708d606f3e73f4e4f481a done
#10 naming to docker.io/library/flask_test:final_v1 0.0s done
#10 DONE 43.0s

 

 

docker_run_flask.sh
cat scripts/docker_run_flask.sh
docker run -p 127.0.0.1:8000:8000 flask_test:final_v1

 

 

run flask
script$ ./docker_run_flask.sh
 * Serving Flask app 'hello'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:8000
 * Running on http://172.17.0.3:8000
Press CTRL+C to quit

 

 

 

docker ps
$ docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                      NAMES
03de9a5bc5e4   flask_test:final_v1   "/bin/bash -c 'flask…"   3 minutes ago   Up 3 minutes   127.0.0.1:8000->8000/tcp   strange_ellis

 

 

Flask Results
$ curl http://127.0.0.1:8000
Hello World!