Docker-compose
k8s-learning-project/
├── [README.md](/home/nineleaps/k8s-learning-project/README.md)
├── [docker-compose.yml](/home/nineleaps/k8s-learning-project/docker-compose.yml) # services and mongodb-data volume
├── frontend/
│ ├── [Dockerfile](/home/nineleaps/k8s-learning-project/frontend/Dockerfile)
│ ├── [package.json](/home/nineleaps/k8s-learning-project/frontend/package.json)
│ ├── [index.html](/home/nineleaps/k8s-learning-project/frontend/index.html)
│ ├── [vite.config.js](/home/nineleaps/k8s-learning-project/frontend/vite.config.js)
│ └── src/
│ ├── [App.jsx](/home/nineleaps/k8s-learning-project/frontend/src/App.jsx)
│ ├── [main.jsx](/home/nineleaps/k8s-learning-project/frontend/src/main.jsx)
│ └── [styles.css](/home/nineleaps/k8s-learning-project/frontend/src/styles.css)
├── gateway/
│ ├── [Dockerfile](/home/nineleaps/k8s-learning-project/gateway/Dockerfile)
│ └── [nginx.conf](/home/nineleaps/k8s-learning-project/gateway/nginx.conf)
├── health-service/
│ ├── [Dockerfile](/home/nineleaps/k8s-learning-project/health-service/Dockerfile)
│ ├── [package.json](/home/nineleaps/k8s-learning-project/health-service/package.json)
│ └── [server.js](/home/nineleaps/k8s-learning-project/health-service/server.js)
├── login-service/
│ ├── [Dockerfile](/home/nineleaps/k8s-learning-project/login-service/Dockerfile)
│ ├── [package.json](/home/nineleaps/k8s-learning-project/login-service/package.json)
│ └── [server.js](/home/nineleaps/k8s-learning-project/login-service/server.js)
└── user-service/
├── [Dockerfile](/home/nineleaps/k8s-learning-project/user-service/Dockerfile)
├── [package.json](/home/nineleaps/k8s-learning-project/user-service/package.json)
└── [server.js](/home/nineleaps/k8s-learning-project/user-service/server.js)This is my project tree for ur reference
k8s-learning-project/
├── README.md
├── docker-compose.yml
├── frontend/
│ ├── Dockerfile
│ ├── index.html
│ ├── package.json
│ ├── vite.config.js
│ └── src/
│ ├── App.jsx
│ ├── main.jsx
│ └── styles.css
├── gateway/
│ ├── Dockerfile
│ ├── nginx.conf
├── health-service/
│ ├── Dockerfile
│ ├── package.json
│ └── server.js
├── login-service/
│ ├── Dockerfile
│ ├── package.json
│ └── server.js
└── user-service/
├── Dockerfile
├── package.json
└── server.jsThe first line while u are writing the docker compose u will come across this terms or u will have to write services and this means u list all the services that mean in this context each of the container that u want to controller at once, all the container that u want to run are called services
indentation is critical in a Docker Compose file because it’s YAML. YAML uses spaces to show structure. Like, nest a service under services. Tabs will break it. You want consistent two spaces per level, not four, not tabs, just two spaces.
In simple terms, services: is where you define all the components that Docker Compose needs to run.
Volumes
A volume is basically like a separate storage box managed by Docker, not by the MongoDB container itself. The container runs the database program, and the volume holds the data files, so when the container goes away, your data stays
in order to check all the volumes created in the containers or in the docker u can use the command i.e
docker volume ls
u will get the volumes listed in the docker
next to inspect the perticular volume u can use the command called as
docker volume inspect volume-name mongodb and volumes attached
So I’ll tell you the exact problem that I faced and I wanted to resolve. So when I checked for something called as MongoDB container,
docker ps --filter name=mongodbI had configured mongodb server in the docker compose file that is
services:
mongodb:
image: mongo:8.0
restart: unless-stopped
volumes:
- mongodb-data:/data/dbI had not written any other build or run command in the terminal at all ,
it was there running, but I had not configured its name and I didn’t build it at all. But still when I had run docker-compose.yml, I had mentioned the service name called as MongoDB, that’s it. And it didn’t have anything such as build or anything. So I wanted to know in the containers, this name was being given that is k8s-learning-project-mongodb-1, which I had not run at all. So I wanted to see who gave the container that name. So yeah, let’s go see it. So this problem I faced while I was studying about Docker and then the volume being attached to the Docker MongoDB container.
Then i got to know that the docker automatically attachs the project name to it and at the end it adds the number also to it .
9bd6fb599b89 mongo:8.0 "docker-entrypoint.s…" 2 hours ago Up 2 hours 27017/tcp k8s-learning-project-mongodb-1My project name was k8s-learning-project so this got added by the docker automatically
volume connection
mongodb-data:/data/dbservices:
mongodb:
image: mongo:8.0
restart: unless-stopped
volumes:
- mongodb-data:/data/dbhere the volume that i gave “/data/db”
is telling the mongodb container inside the docker to consider it as the database directory and then the actual saying happens in the
"Mountpoint": "/var/lib/docker/volumes/k8s-learning-project_mongodb-data/_data",this is where the data actually gets saved as my mongodb is saved in the local so the volumes get saved in the local system itself .
local MongoDB running on localhost port 27017 with leapmentor_profile. Separately, your Docker Compose project starts another MongoDB inside Docker. The purpose of the mongodb-data volume is persistence. So if you stop and remove the Docker MongoDB container, the data inside that volume will remain. When you start a new container and attach the same volume, MongoDB can still access the old data. So, you don’t need to connect your existing local database at all unless that’s the goal.
Now lets go for the service code that is
login-service:
build: ./login-service
environment:
MONGODB_URI: mongodb://mongodb:27017/kubernetes-practise
ports:
- "3001:3001"
depends_on:
- mongodbso what it does is
login-service:
build: ./login-serviceHere's what happens:
- Compose sees
build: ./login-service. - It goes into the
login-servicefolder. - It looks for a file named
Dockerfileby default. - It uses that Dockerfile and the folder's contents to build an image.
- It gives the image an automatically generated name, such as
k8s-learning-project-login-service:latest. - So, the Dockerfile is still being used. Compose is simply automating the build process.
What if you already built the image manually?
For example, suppose you previously ran:
docker build -t my-login-image:latest ./login-serviceThat creates an image called my-login-image:latest.
Get Anush Lobo’s stories in your inbox
Join Medium for free to get updates from this writer.
But your Compose configuration doesn’t mention that name. It says only:
build: ./login-serviceTherefore, Compose doesn’t automatically use my-login-image:latest. It builds or uses its own project/service-named image.
If you want Compose to use a particular image name, you can specify both:
login-service:
build: ./login-service
image: my-login-image:latestThe key distinction:
Dockerfile= instructions for building an image.docker build= manually asks Docker to build an image.docker compose build= asks Compose to build images for configured services.docker compose up= creates and starts the services, building images when needed.
So, Compose is not bypassing your Dockerfiles. It is using them automatically.
Docker Compose does two main things:
- Builds images using the Dockerfiles specified in the Compose file.
- Creates and runs containers from those images.
For example:
services:
login-service:
build: ./login-service health-service:
build: ./health-serviceHere:
login-serviceis the Compose service name../login-serviceis the folder containing its Dockerfile and application files.health-serviceis the Compose service name../health-serviceis its build folder.
For example, when you execute:
docker compose upCompose coordinates the process, and Docker Engine does the actual container creation and execution.
Remember this distinction:
- Image = blueprint/template.
- Container = running instance created from an image.
- Docker Engine = runs the containers.
- Docker Compose = coordinates multiple containers.
In your project, Docker Compose coordinates MongoDB, login-service, health-service, user-service, frontend, and gateway.
to run the docker-compose.yml u have to give the command that is
docker compose up --build or else just
docker compose up
in order to stop the compose u just have to run the command
docker compose down






