Multiple Containers
Running a single container is easy. Real engineers control entire fleets of containers. Remember, Docker containers are very lightweight, so it’s normal to run many containers on a single host machine.
Each container is its own isolated environment and process. Even if we start 10 containers from the same image, the image is just the definition of the container (the code and starting environment). Each container is a separate instance of that image.
Example: Running Multiple Instances
You can start multiple different instances of the same container, each on a new host port:
docker run -d -p 8965:80 docker/getting-started
docker run -d -p 8966:80 docker/getting-started
docker run -d -p 8967:80 docker/getting-started
docker run -d -p 8968:80 docker/getting-started
docker run -d -p 8969:80 docker/getting-started
We use port 80 within each container because that’s the port that the application is binding to inside the container. It’s being forwarded to the different ports we specify on the host machine. We have to use different host ports because two processes can’t bind to the same port on the same operating system.
If you load each webpage in your browser, they will all work independently:
http://localhost:8965http://localhost:8966http://localhost:8967http://localhost:8968http://localhost:8969
Even though they all serve the same page, each is served from its own isolated container! You can run docker ps to see all the running containers.