2nd chunk of `content/get-started/docker-concepts/running-containers/multi-container-applications.md`
16af45b8a329b1eef8fdd322e50e72626cb88905d5b143290000000100000fe7
- You can run containers in a specific order and manage network connections easily.
- You can simply scale individual services up or down within the multi-container setup. This allows for efficient allocation based on real-time needs.
- You can implement persistent volumes with ease.
- It's easy to set environment variables once in your Docker Compose file.
By leveraging Docker Compose for running multi-container setups, you can build complex applications with modularity, scalability, and consistency at their core.
## Try it out
In this hands-on guide, you'll first see how to build and run a counter web application based on Node.js, an Nginx reverse proxy, and a Redis database using the `docker run` commands. You’ll also see how you can simplify the entire deployment process using Docker Compose.
### Set up
1. Get the sample application. If you have Git, you can clone the repository for the sample application. Otherwise, you can download the sample application. Choose one of the following options.
{{< tabs >}}
{{< tab name="Clone with git" >}}
Use the following command in a terminal to clone the sample application repository.
```console
$ git clone https://github.com/dockersamples/nginx-node-redis
```
Navigate into the `nginx-node-redis` directory:
```console
$ cd nginx-node-redis
```
Inside this directory, you'll find two sub-directories - `nginx` and `web`.
{{< /tab >}}
{{< tab name="Download" >}}
Download the source and extract it.
{{< button url="https://github.com/dockersamples/nginx-node-redis/archive/refs/heads/main.zip" text="Download the source" >}}
Navigate into the `nginx-node-redis-main` directory:
```console
$ cd nginx-node-redis-main
```
Inside this directory, you'll find two sub-directories - `nginx` and `web`.
{{< /tab >}}
{{< /tabs >}}
2. [Download and install](/get-started/get-docker.md) Docker Desktop.
### Build the images
1. Navigate into the `nginx` directory to build the image by running the following command:
```console
$ docker build -t nginx .
```
2. Navigate into the `web` directory and run the following command to build the first web image:
```console
$ docker build -t web .
```
### Run the containers
1. Before you can run a multi-container application, you need to create a network for them all to communicate through. You can do so using the `docker network create` command:
```console
$ docker network create sample-app
```
2. Start the Redis container by running the following command, which will attach it to the previously created network and create a network alias (useful for DNS lookups):
```console
$ docker run -d --name redis --network sample-app --network-alias redis redis
```
3. Start the first web container by running the following command:
```console
$ docker run -d --name web1 -h web1 --network sample-app --network-alias web1 web
```
4. Start the second web container by running the following:
```console
$ docker run -d --name web2 -h web2 --network sample-app --network-alias web2 web
```
5. Start the Nginx container by running the following command:
```console
$ docker run -d --name nginx --network sample-app -p 80:80 nginx
```
> [!NOTE]
>
> Nginx is typically used as a reverse proxy for web applications, routing traffic to backend servers. In this case, it routes to the Node.js backend containers (web1 or web2).
6. Verify the containers are up by running the following command:
```console
$ docker ps
```
You will see output like the following:
```text
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2cf7c484c144 nginx "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 0.0.0.0:80->80/tcp nginx
7a070c9ffeaa web "docker-entrypoint.s…" 19 seconds ago Up 18 seconds web2