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

Example Analysis of Compose in Docker

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

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of Compose in Docker, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

Docker Compose can implement the orchestration of Docker container clusters. Through docker-compose.yml files, we can define our services and their needed dependencies, and easily run them in test, production and other environments.

Install Compose

Compose depends on Docker Engine. To ensure that Docker is installed in the environment, you can refer to the official tutorial, which is divided into two steps:

# 1. Download Compose only executable file to usr/local/bin/ directory # download failed, please refer to the next summary to provide the address to install sudo curl-L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname-s)-$(uname-m)"-o / usr/local/bin/docker-compose# 2. Add run permissions to the Compose executable sudo chmod + x / usr/local/bin/docker-compose# enter the following command to view help and test whether the installation is successful or not docker-compose-h

Compose is open source in Docker's official GitHub repository: docker/compose, and all Compose will be published in the Releases of the repository. Step 1 is to download the executable file from Releases using the curl command. Uname-s and uname-m can read the kernel name and hardware architecture of the system to match the required Compose version. The-L parameter of curl will make the HTTP request follow the redirection (not followed by default). -o (lowercase o) will save the server response as a file and download it directly to: usr/local/bin/. The file name is: docker-compose. Because this path is already in the environment variable, step 2 is completed. After adding executable permissions, you can use it anywhere.

It is slow to download directly from GitHub. You can download it at the following address:

# https://vuepress.mirror.docker-practice.com/compose/install/sudo curl-L https://download.fastgit.org/docker/compose/releases/download/1.27.4/docker-compose-`uname-s`-`uname-m`-o / usr/local/bin/docker-composesudo chmod + x / usr/local/bin/docker-compose getting started

The template instruction of Compose is very similar to the parameters related to the run command of Docker, forgetting that the docker command can refer to a previous blog: Docker practice and command carding

There are two important concepts in Compose:

Service (service): a container for an application that can actually include several container instances running the same image

Project (project): a complete business unit consisting of a set of associated application containers, defined in the docker-compose.yml file

The docker-compose.yml format is as follows. Note: YAML files must be followed by a space, indented to indicate the level, and pay attention to indentation

Volumes and networks that are used must declare

# specified version of version: "3" # Collection of services services: # one of the services, service name: webapp webapp: # specify the image image: examples/web # port mapping used by the service ports:-"80:80" # data volume volumes:-"/ data" easy to get started

Start Tomcat, MySQL, redis in a Compose, and create docker-compose.yml

Version: "3.0" services: tomcat: container_name: mytomcat #-- name image: tomcat:8.0-jre8 ports: -" 8080container_name "volumes: -" tomcatwebapps:/usr/local/tomcat/webapps "networks:-some_network # tomcat service depends on mysql and redis depends_on:-mysql-redis mysql: container_name: mysql image: mysql : 5.7.32 ports:-"3306 mysqlconf:/etc/mysql 3306" volumes:-"mysqldata:/var/lib/mysql"-"mysqlconf:/etc/mysql" environment:-MYSQL_ROOT_PASSWORD=1234 networks: some_network: container_name: redis image: redis:5.0.10 ports:-"6379 volumes: -" redisdata:/ Data "command:" redis-server-- appendonly yes "networks: volumes and networks used by some_network:# must declare volumes: tomcatwebapps: mysqldata: mysqlconf: redisdata: networks: # declare the network some_network named" some_network ":

Execute docker-compose up to start the Compose project in the path where docker-compose.yml is located. It will download the used image and run the print log in the foreground, which can be terminated using Ctrl + C.

If you need to run and execute docker-compose up-d in the background, you can use docker ps to see that Compose has created the relevant container according to yaml. Use docker-compose down to stop Compse and remove the automatically created bridge.

Use docker network ls to view the network or docker volume ls to view the data volume. The format of the network or data volume name defined by Compose is: the name of the folder where docker-compose.yml is located, followed by an underscore and the name defined in yaml. If you create a yaml file under the "dockerfile" folder and start it, then the network name is: dockerfile_some_network.

The tomcat service uses depends_on, which means that it depends on redis and mysql services, and Compose will first start its dependency and then start it

Command carding

The command of Docker Compose is similar to Dokcer. You can use the-- help parameter to find out how to use the corresponding command.

Docker-compose-help

The template file that starts by default is called docker-compose.yml. You can use-f to specify a custom template file.

You can check whether the syntax of the template file is correct through the config command

Docker-compse also contains many subcommands:

Start and stop related: up, down, restart, stop, pasue, unpause

Resource related: ps, top, kill, run

Enter the container: exec

View the log: logs

Many subcommands can be followed by a specific service name and operate directionally. Here are some examples.

You can use docker-compose help followed by the name of the subcommand to query its usage

# launch all containers defined by yaml docker-compose up-d # launch only the service of mysql, and will start the dependent servicedocker-compose up mysql that specifies the server name of the startup # stop the container and remove the automatically created bridge docker-compose down # after restarting all service, you can specify a specific servicedocker-compose restart# to pause and resume docker-compose pausedocker-compose unpause# to enter redis this service uses exit to exit docker-compose exec redis bash# to list the information of containers defined in the current yaml docker-compose ps# to delete the containers defined in the current yaml, you need to stop first Later, you can specify a specific servicedocker-compose rm# to view the processes running in each service container docker-compose top# view log by default view yaml all, you can keep up with the specific service#-f can keep tracking, the new log will be immediately displayed on the screen docker-compose logs thank you for reading this article carefully, I hope the article "sample Analysis of Compose in Docker" shared by the editor will be helpful to you At the same time, I also hope that you will support and pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

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