How to Use Docker Images For Scaling Applications?
Last Updated :
21 Apr, 2025
Docker image is a lightweight piece of software that includes everything that your application needs to run, it includes code, software packages, and libraries. Docker images are built using a Docker file which includes the instruction for creating the image. The Docker image is built on top of a base image which is specified on top of the Dockerfile.
Docker images are typically stored in the docker registry such as the DockerHub, or Artifactory. Dockerfile is a source code for Docker images.
Types of Scaling
Scaling is the ability of the system to handle the increasing amount of workload. So the system should be up and running even when the load on the system increases. There are two types of scaling :
- Horizontal Scaling: In this more servers are used to distribute the workload across the servers or containers is called horizontal scaling.
- Vertical Scaling: In this more resources such as CPU, and memory are added to the same server to handle the increasing workload is called vertical scaling.
Sample Python Code For To Build Application
Create a directory named scaling and open that directory on the code editor of your choice.
First, create a Main.py file and copy-paste the below code :
Python
from flask import Flask
import socket
app = Flask(__name__)
@app.route('/')
def get_hostname():
hostname = socket.gethostname()
return f"Hostname: {hostname}\n"
if __name__ == '__main__':
app.run(debug=True, port=5000)
The above script will run a Flask server on port 5000. Now create a Docker image from the above file using a Dockerfile. Create a file named Dockerfile without any extension in the scaling directory.
Copy and paste the below code into that file:
FROM python:3.8
RUN pip install flask
COPY app.py /app.py
CMD ["python", "app.py"]
Now create a Docker image which will be stored on your machine using the below command :
docker build -t flaskapi .
Using Docker Image for Vertical Scaling
Now we can start the container from the docker image specifying the amount of resources it should use.
docker run -it -p 8000:5000 --cpus 1 --memory 1g flaskapi
The above command will start the docker container from the flask API image, we have specified the number of CPUs it can use and also the amount of memory it can use. We have started the container in the detached mode specified using the -d flag and mapped port 8000 on the local host to port 5000 inside a container where the flask API is listening.
Now considering that the number of requests coming to the API increases, we can just start another container from the image with more ram and CPU, and as soon it gets up we can stop the previous container.
docker run -it -p 8000:5000 --cpus 2--memory 2g flaskapi
Using Docker Image for Horizontal Scaling
We can horizontally scale an application using docker swarm. Docker swarm is a cluster management and orchestration feature embedded in Docker. It allows running multiple containers across a cluster of nodes. To enable docker swarm in your machine run the below command :
docker swarm init
Now to create containers and deploy them using Swarm we need to create a docker-compose file, which is the deployment file. In the YAML file, we define the service, the container that the service should use, and also the number of containers in that image. that should be running.

Create a file name docker-compose.yml in the scaling directory and copy and paste the below code in it:
version: '3.8'
services:
web:
image: flaskapi
ports:
- 8000:5000
deploy:
replicas : 3
The docker-compose file defines the following thing:
- Version: the version of the compose file.
- Services: info about the services.
- Web: name of the service.
- Image: flaskapi is the image used for the web service.
- Ports: mapping local port to container port.
- Replicas: the number of containers to run.
Now to deploy the above file you can run the below command :
docker stack deploy my-stack -c docker-compose.yml
Above command will start 3 docker containers each running the flaskapi image.

Now you can scale the flask API by increasing the number of containers running the flaskapi image using the below command :
docker service scale my-stack_web=5
After running the above command 5 containers will be running the flaskapi image.

Similar Reads
How To Use Docker For IoT Applications? Docker is a super tool that makes our lives much less complicated by providing us with standardization, productivity, performance, maintainability, and compatibility of our code. It lets us continuously and hastily install and test our code, and it is platform-impartial. Docker provides the ability
8 min read
How to Use Docker For Big Data Processing? Docker has revolutionized the way software program packages are developed, deployed, and managed. Its lightweight and transportable nature makes it a tremendous choice for various use instances and huge file processing. In this blog, we can discover how Docker may be leveraged to streamline huge rec
13 min read
How to Use Ansible for Docker Container Management Containerization has become one of the foundations for realizing scalable, reliable, and efficient application deployments in modern DevOps practice. Docker, as a leading containerization platform, enables developers to package applications and all dependencies into containers for consistency across
9 min read
How to Run GUI Based Applications inside Docker? A Docker Container is an isolated application platform that contains everything needed to run an application built from one or more images. Docker is an Open Source project that provides an open platform to run any number of applications inside a container according to your requirements and you can
7 min read
Running a Ruby Application using Docker Pre-requisite:- Docker Docker is a containerization platform that allows you to package your applications and their dependencies into a lightweight and portable containers. Using Docker, you can easily deploy and run your applications on any platform that supports Docker, without having to worry abo
4 min read