📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Docker Compose Beginner Tutorial: Run Multiple Containers Easily - Detail Explaination

5MinDev6:19

Transcription

To 5 minute dev. We've built and run single containers, but real-world apps usually need more than one surface, like an app plus a database. That's where Docker Compose comes in.

So, Docker Compose is a tool that lets you define and run multicontainer applications. Instead of typing long `docker run` commands for each service, like we did, you describe everything in a single `docker-compose.yml` file.

Let's say we have a NodeJS app and it needs a MongoDB database. Our `docker-compose.yml` might look like this. So, we have `services` here. Each service equals one container definition. Think of it as: "What containers do I need to run my app?"

In this file, we define two services: we have `app` and `database`.

`app`: Here we have `service name` with this uh, `service name` is is this the container that runs your application. Here's the app. Okay. And `build` this tells Docker Compose to build a Docker image for this service from the current directory. And it, it looks for `Dockerfile` in that folder. Here, we just uh have that `Dockerfile` from that previous video. And the `ports` here, `30000` means it maps host port `3000` to container `5000`. This means you can visit `localhost:3000` to access the app. And it `depends on` `database`. This says `app` depends on the `database` service, ensuring the database container starts before the app container. The database must be running first. Okay. Note that it does not wait until the database is ready, only until it's started. For readiness, you use health checks.

And for the `database` service here, is another service. Yeah, this one runs on `mongo:5` image. Here, it will pull the official MongoDB image version five, version six here. If the image isn't on the machine, Docker will download it. And we have the `volumes` here. Is the `volumes` here is the uh data uh, it maps a named volume called `data` uh to MongoDB's internal data uh directory here. It maps to here, and this is where our database data persists, even if the container is stopped or removed.

And the `volumes` global here, this defines is a named uh volume called `data`. This is one used by the database service. Unlike by `mounts` uh linking to a folder on your host, named volumes are managed by Docker and stored in Docker's volume system. Okay, that's uh is a detailed explanation about that. Uh, but um, in a very, very short explanation, uh, we can uh re uh, we can just uh simply to um see that here we have `services`. We have the `app` service, builds from the `Dockerfile` and expose to expose it to port `3000`, and the `database` service here, this is the official `mongo` image and official `volumes` stores database data. Okay, so that's it.

Um, now let's uh zoom into and uh with this ready file, we just need to run `docker compose up`. Here. Yeah, here's uh this will uh the app and the database will start together and run, and we can just visit the uh `localhost:3000` and it's ready on this port. And the database here is running here. You can see the way one is running. That's it.

And to stop and remove anything, uh, any um, everything, we just `docker compose` and then we say `down`. It will remove everything. Okay. Now let's say `docker compose ps` to see the running containers. We have no thing, nothing running anymore. And to see logs of the compose, we see we use `docker compose logs`. Yeah, we have no logs here. Why?

Now, let's wrap it up. Why is Docker Compose powerful? Uh, Compose sets up repeatable, sharable, and portable environments. You can give someone your `docker-compose.yml` and then they can run the exact same environment instantly. And that's Docker Compose in just 5 minutes. Now you can manage multiple services like a pro.

In the next video, we'll go deeper into volumes and persistent uh persistent storage in Docker, so your data doesn't vanish when a container stops. Stay tuned.