Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker Compose to build complex multi-container App

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use Docker Compose to build complex multi-container App". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Docker Compose to build complex multi-container App" can help you solve the problem.

1 Why do you need Docker Compose

In the process of building a container, it is generally necessary to run some commands repeatedly, such as docker build, docker run and so on. These commands are sometimes lengthy and complex, and it is very tedious to run these commands one by one, especially for complex applications with multiple containers. Using Docker Compose tools can greatly simplify the creation, management, and maintenance of containers.

2 what is Docker Compose

The Docker Compose tool is equivalent to the make tool of CCompact +. Makefile is required to use make, and all compilation link settings are specified in Makefile. There is no need to manually enter a long string of instructions in the terminal each time in order to run the program.

Docker Compose also needs a configuration file: docker-compose.yaml (the file extension can also be yml).

YAML is a human-readable data serialization language that is commonly used to configure files and applications that store or transfer data, and YAML uses indentation like python to indicate nested relationships. Indentation usually uses two spaces, but there are actually no restrictions, like python, using # for comments.

3 use of Docker Compose

The following is a Mern Stack App project structure:

The docker-compose.yaml file is as follows:

# version of the docker compose specification, the following website can be found in # https://docs.docker.com/compose/compose-file/compose-versioning/version: "3.8" # this mern will create 3 containers The following three services # mongodb, backend, frontend will be automatically translated into three containers services: mongodb: # get the official image image from hub.docker.com: "mongo" # named volume: persistent storage of database data volumes:-data:/data/db # environment variable Save env_file: -. / env/mongo.env backend: build:. / backend ports:-"80:80" volumes: # named volume-logs:/app/logs # bind mount in the file. / env/mongo.env, unlike the command, relative paths can be used in this file # to run the command manually, you need an absolute path -. / backend:/app # Anonymous Volume-/ app/node_modules env_file: -. / env/backend.env depends_on:-mongodb frontend: build:. / frontend ports:-"3000 backend:/app 3000" volumes: -. / frontend/src:/app/src # stdin_open and tty correspond to-it stdin_open: true tty: true # for manual command execution only after the container backend is running To run this frontend depends_on:-backend-# this is the named volume of the entire App # Anonymous volume and bind-mounts cannot specify volumes: data: logs:3.1 startup container docker-compose up here

Docker-compose up runs in additional mode by default, and if you need to run in detached mode, add-d after the command:

Docker-compose up-d

For this reason, there is no option to set-d in docker-compose.yaml.

3.2 stop and delete the container docker-compose down

With Docker Compose, Dockerfile files are still essential. Docker Compose creates a default network and adds all containers to that network, so there is no need to specify a network in docker-compose.yaml.

Docker-compose down will stop and delete the container, so docker-compose.yaml has no option-- rm

Whether single container or multi-container, using Docker Compose can greatly simplify the creation, management, and maintenance of containers, so Docker Compose is a powerful and useful tool.

That's all for "how to use Docker Compose to build a complex multi-container App". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report