Skip to content

Docker to run Python Application

jm1021 edited this page Jun 12, 2022 · 40 revisions

Docker Procedures

The concept is to build Dockerfile (container) and docker-compose.yml (driver or make file). In the yaml file, there is a volume setup to manage persistent data.

Read our Docker wiki before doing this, you will understand stuff better

https://github.com/nighthawkcoders/nighthawk_csp/wiki/Docker-Basics

Official Docker wiki:

https://docs.docker.com/

Make a Docker Run File: Dockerfile, customize Git location and Port binding to match your requirements.

If you have ever made a Makefile, this is similar to that.

NOTE: You should use the Alpine image (alpine:latest) instead of the Python image, because Alpine is super small (5 MB!!)

WARNING: this Dockerfile template is still WIP, you will not be able to regularly update your git repo yet.

FROM docker.io/python:3.9

WORKDIR /app

# --- [Install python and pip] ---
RUN apt-get update && apt-get upgrade -y && \
    apt-get install -y python3 python3-pip git
RUN git clone https://github.com/nighthawkcoders/pages_python /app

RUN pip install --no-cache-dir -r requirements.txt
RUN pip3 install gunicorn

ENV GUNICORN_CMD_ARGS="--workers=3 --bind=0.0.0.0:8080"

EXPOSE 8080

CMD [ "gunicorn", "main:app" ]
  • Tip: Putting multiple commands into one line can save space! You can put multiple commands in one line with && or ; between the commands. If the one line becomes messy or long, use an escape character like \ to put the other command on a "new" line. But the shell will just ignore the new line and run the command as if it was just a one liner.

Make a Docker Compose File: docker-compose.yml

The Docker image runs on port 8082 and has access to a disk volume where preserve database, uploads, etc.

version: '3'
services:
  web:
    image: python_appv1
    build: .
    ports:
      - "8082:8080"
    volumes:
      - persistent_volume:/app/volumes
volumes:
  persistent_volume:
    driver: local
    driver_opts:
       o: bind
       type: none
       device: /home/ubuntu/dock.py/volumes

To run docker compose and build the above...

docker-compose up -d

Test to see if Home Page html is fetched from server

curl -v http://0.0.0.0:8082

Review images created

docker images

Image list sample output

REPOSITORY     TAG       IMAGE ID       CREATED        SIZE
python_appv1   latest    c91a5a8e0c9d   40 hours ago   998MB
python         3.9       8bb7b6ec5b8e   6 days ago     914MB

Review "docker" processes

docker ps

Docker process list sample output

CONTAINER ID   IMAGE          COMMAND               CREATED        STATUS        PORTS                                       NAMES
1889f38fc1f1   python_appv1   "gunicorn main:app"   40 hours ago   Up 40 hours   0.0.0.0:8082->8080/tcp, :::8082->8080/tcp   dockpy_web_1

Review "docker-compose" process (you must be in the directory of yaml file)

docker-compose ps

Docker Compose process list sample output

    Name            Command        State                    Ports                  
-----------------------------------------------------------------------------------
dockpy_web_1   gunicorn main:app   Up      0.0.0.0:8082->8080/tcp,:::8082->8080/tcp

---------- Need to focus rest of this document on updates, managing data, etc. -----------------

Stop the running Container, you will need to acquire "CONTAINER ID" from ps command. Note, find the container in command you run. The container in the image and below will not match your machine.

docker stop 263115e0d692

Docker Container/Image Management

When testing different docker images, storage can fill up quickly. Michael S logged into a test machine that the Teacher was working on and it had ~7 docker images each at 1 GB, which made the disk usage reach 90%! Be sure you clean up and inspect your disk usage.

Checking disk usage:

Running df will show you the storage usage on all your mounted disks.

df
  • tip: if you want a cooler looking df output, install duf.

Cleaning up images:

NOTE: Quick reminder, you can see all your images by running:

docker images

You can remove images with docker rmi. Note, you can delete multiple images at once. (eg: docker rmi image1_foo image2_bar). If docker complains that they can't be deleted, add the -f to end of command. This option will force it to delete. (eg: docker rmi -f image1_foo)

docker rmi image1_foo image2_bar

(Replace "image1_foo" for the image you want to remove. These will not match your system.)

Cleaning up stopped containers:

If you have stopped containers (but haven't been deleted), you can delete all stopped containers with:

docker container prune

WARNING: This will delete ALL stopped containers, so be careful.

Cleaning up unused volumes:

WIP :)

Clone this wiki locally