Multi-container docker-compose file which will bring UP and bring DOWN containers in a single shot & Docker Volumes Volumes to share files and directories between multiple containers.
Docker volumes and why it used?
Understanding Docker Volumes: Docker Volumes provide a way to store and share data between containers and the host machine. They act as a special storage location that can be accessed by one or more containers simultaneously. Think of Docker Volumes as folders or directories that are independent of containers, allowing data to persist even if containers are deleted or recreated.
Why Use Docker Volumes:
Persistent Data: Docker Volumes ensure that your data remains intact even if containers come and go. This is particularly important for applications that require long-term storage of important information or user-generated content.
Flexibility: By using Docker Volumes, you can update or replace containers without worrying about losing data. The data stored in the volume is separate from the containers, allowing for easier maintenance and updates.
Collaboration Made Easy: Docker Volumes provide a convenient way for teams to collaborate. Team members can work on different containers independently while using the same shared data storage. This promotes efficient collaboration and avoids data duplication.
Improved Performance: Docker Volumes are designed to optimize input/output (I/O) operations for better performance. Compared to alternative approaches like bind mounts, Docker Volumes offer faster and more efficient data access within containers.
Task 1 - Create a multi-container docker-compose file that will bring UP and bring DOWN containers in a single shot
create a docker-compose file-
version: '3.9'
services:
web:
container_name: "node-todo-app"
build: .
ports:
- "8000:8000"
volumes:
- nodevolume:/app
volumes:
nodevolume:
Now in this compose file, we attached a volume in it and now run the container, by docker-compose up
now in port 8000 run the output
Use the docker-compose scale
command to increase or decrease the number of replicas for a specific service. For example, to scale up the "web" service to three replicas, use the following command:
docker-compose scale web=3
to stop and remove all containers, networks, and volumes associated with your Docker Compose application, use the docker-compose down
command.
TASK 2: use Docker Volumes to share files and directories between multiple containers.
create a docker volume
docker volume create --name nodevolume --opt device=home/ubuntu/volumes/volume_1 --opt o=bind
Now check volume by
docker volume inspect <volumeid>
Now mount the volume into the container -
docker run -d -p 8000:8000 --mount source=nodevolume,target=/app node_cicd
execute the container and make a new file under there after that cross check in to the node volume - all newly created files will be there.
docker exec -it <container id > bash
#> ls
#you will see all the files, make a new file here. then
exit
now go to the volume directory and do ls to view the all container files with new file.
Thanks for reading!!!!