Each docker container represents a running application. It is a common use case to run multiple applications on a single machine.
If all of those applications are dockerized, then you need to run multiple docker containers on the same host machine.
In this article, we will look at the method to do this.

How to Run Multiple Docker Containers on a Single Host

Following steps are required to run multiple docker containers on the same host machine.

1. Pull images

First step is to pull the docker images of all the applications that you want to run on the same system.
To pull an image, docker pull command is used along with the name and tag of the image. Example,

docker pull appimage:feature-product-1.0.0.0

This will pull the image with given name and tag from configured docker registry to the local system.

2. Start the container

A container can be started or run from a docker image using docker run command, using the name of the image or its id. Example,

docker run 12d2323d

To find the image name or id, use docker images command.

Now, to run multiple images on the same host machine, open different command prompts or terminal windows and execute docker run command with the application images that you want to run.

This approach has a disadvantage that multiple terminals need to be launched.
Secondly, the application will run only till the terminal window is open.

A solution to both these problems is running images in background using -d flag as shown below

docker run -d 12d2323d

3. Check containers

Once you have run multiple containers, you can check their status using below command

docker ps

This will list all running docker containers.

Using docker compose

You can also run multiple images using docker compose.
Docker compose is a tool which can be used to run multiple images on the same host machine at once.

With docker compose, you can easily manage multiple containers and their dependencies, making it easier to run multiple docker containers on a single host by following below steps.

1. Define services

Configures the images or applications that you want to run on the same machine in docker-compose.yaml file as shown below

version: '3'
services:
  app:
    image: myapp
    ports:
      - "9090:8080"
  db:
    image: mysql
    environment:
      DB_PASSWORD: dbpwd

In above example, the docker-compose.yml file defines two services: app and db.
The appservice runs your own application and is built from myapp image, and exposes port 9090 on the host to port 8080 in the container.
The db service uses the mysql image and sets the DB_PASSWORD environment variable to password.
2. Running services

After configuring the images to run on the host machine, next step is running them.
This can be done by running docker compose using below command

docker-compose up

This command will run all the images and services listed in docker-compose.yml file as containers on the same host machine.
Remember that all services will run in the background, which is same as docker run -d command.

3. Checking status

To check the status of running services and containers, use below command

docker-compose ps

It is obvious that using docker compose to run multiple containers on a single host is easier as compared to the first approach.
You have to define your services in a single file and then you can start, stop, and scale your services with ease.
But if you do not want to use docker compose, then first method can also be used to run multiple containers on the same host machine.

Conclusion

In conclusion, running multiple Docker containers on a single host can greatly improve efficiency and resource utilization.
By properly managing and orchestrating your containers, you can optimize the performance of your applications and streamline your development and deployment processes.
Using tools like Docker Compose, Kubernetes, or Docker Swarm, you can easily deploy, scale, and manage multiple containers on a single host.
This allows you to isolate and modularize your applications, making them easier to manage and update.
Additionally, by utilizing containerization technology, you can ensure that your applications are secure, scalable, and portable.
This means that you can easily transfer your containers between different hosts or environments without having to worry about dependencies or compatibility issues.