Using Docker container to develop Flask application
Azharuddin Mohammad
Indian national working as a Senior Data Engineer at FWD Insurance Technology and Innovation in Kuala Lumpur, Malaysia
Kubernetes
Open source CONTAINER orchestration tool. It has two main components, Control Plane and Data Plane?
Control plane
Key components of a control plane are API server, the etcd data store, the controller manager, the scheduler, and an optional cloud controller manager?
All components of control plane can be hosted on a single node i.e physical machine. Multiple nodes can host different control plane components for fault tolerance and scalability.?
Data plane
Contains worker nodes which are physical machines (or virtual).?
Nodes can have one or more pods. default maximum of 110 Pods per node.?
Pods typically contains one containers application. In rare and advanced use case, one main container is tightly coupled with multiple supporting containers.?
Horizontal Pod Auto-Scaling configuration (HPA) automatically adjusts the number of pod replicas based on CPU utilization, memory usage, or custom metrics.?
Pods are allocated CPU and memory. This can be configured to auto scaled as well.
Docker
The IT software “Docker” is containerisation technology that enables the creation and use of Linux containers.?
The open source Docker community works to improve these technologies to benefit all users.?
The company, Docker Inc., builds on the work of the Docker community, makes it more secure, and supports the improved and hardened technologies for enterprise customers.?
Docker components
Docker desktop software include client, deamon and Kubernetes (inactive by default) etc?
Docker objects
Images, containers, volumes etc?
Deployment
Simple Flask web application design include
Need WSGI to decouple actual python web application tech stack from the web server. PEP 3333 is a standard followed by all WSGI.
Flask containerised application code
Docker commands
Shows active containers.
docker ps
Shows all containers.
docker ps –a
Download image from docker hub to create a container and start it?
docker run -it --name <container name> <image name>?
领英推荐
docker run -it --name ubuntu_container01 ubuntu?
docker pause <container name>?
docker unpause <container name>?
docker stop <container name>?
docker start <container name>?
docker start ubuntu_container01?
Syntax to remove or delete container.
docker rm <container name>
Note we cannot remove running container without stoping it first.??
Syntax to execute command inside a container.
docker exec <container name> <command>
docker exec ubuntu_container01 ls??
docker exec –it <container_name> bash?
-it is for Interactive TTY (TeleTYpe)?
docker exec -it ubuntu_container01 bash
?List images available on local machine.
docker images
Port mapping between container and host machine.
Docker run –it –p <local_port>:<container_port> <container_name>?
docker run –it –p 1025:1025 ubuntu-container01?
To build an image create Dockerfile inside project folder and run below commands.
docker build –t <image_name> <path/to/Dockerfile>?
docker build -t test_image .
Dockerfile
FROM
RUN
COPY
WORKDIR
EXEC
CMD
ENTRYPOINT
Docker compose
To create multiple containers at once create a docker-compose.yaml file in project folder and run below commands in the folder.
Compose yaml file contains images to be downloaded from docker hub, port mapping etc.
docker compose up?-d
–d is detached mode runs containers in background and lets us use terminal.
docker compose down