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

Docker three Musketeers-- docker-compose

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

A brief introduction to Docker-Compose

Compose is a tool for defining and running container docker applications. With Compose, you can use YAML files to match all the services your application needs. Then, with a single command, you can create and start all services from the YAML file fit. Then you need to understand the basic syntax of YAML files.

Basic syntax for YAML files:

Case-sensitive; use indentation to indicate hierarchical relationships; indentation does not allow tab, only spaces; the number of indented spaces is not important, as long as elements of the same level are left-aligned; "#" indicates comments

Docker-Compose is a container orchestration tool. Through a .yml or .yaml file, write all container deployment methods, file mappings, container port mappings, and so on in a configuration file, and execute the docker-compose up command just like executing a script to install and deploy the container one by one.

Dockerfile allows users to manage a separate application container, while Compose allows users to define a set of associated application containers in a template (YAML format)

Docker Compose divides the managed container into three layers:

Engineering (project); service (service); container (container)

All the yml files under the docker compose running directory form a project, and a project contains multiple services, each of which defines the images, parameters and dependencies of the container. A service can include multiple container instances.

Docker-compose is the orchestration tool for docker containers, which mainly deals with the management of multiple containers that depend on each other.

II. Installation and use of docker-compose tools

If you want to use docker-comppose as a container orchestration tool, the host must be a docker-based environment, and you can refer to the detailed installation tutorial of docker. After the docker environment is solved, download the command docker-compose, which can be downloaded on the official website of GitHub, as shown below:

When downloading the compose tool, you need to check the local docker version first!

[root@docker ~] # docker-v / / View the version information of docker Docker version 18.09.0, and build 4d60db4// version 18.9.0 this time

If the docker version is too low, you can find other versions of the docker-compose tool yourself. After selecting the appropriate version, execute the commands found on the github website.

[root@docker ~] # curl-L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-`uname-s`-`uname-m`-o / usr/local/bin/docker-compose [root@docker ~] # chmod + x / usr/local/bin/docker-compose

If the network speed is not good, you can use the following command:

[root@docker ~] # curl-L https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-`uname-s`-`uname-m` > / usr/local/bin/docker-compose [root@docker ~] # chmod + x / usr/local/bin/docker-compose// use Daoyun's accelerator to download [root@docker ~] # docker-compose-v / / View the version information of the tool docker-compose version 1.25.0 Build 0a186604 3. Write .yml file (1) build a simple Nginx service. Yyml file [root@docker ~] # vim / root/.vimrc set tabstop=2 [root@docker ~] # source / root/.vimrc// due to the heavy use of the tab key So set in advance the number of spaces represented by the tab key [root@docker ~] # mkdir compose_ test [root @ docker ~] # cd compose_test/ to create a test directory Used to store docker-compose.yml files / / it is recommended that there is only one docker-compose.yml file in a directory [root@docker compose_test] # vim docker-compose.yml / / write a docker-compose.yml file version: "3" / / version of the specified syntax services: / / define service nginx: container _ name: web_nginx / / the name of the container running image: nginx / / the image restart: always / / used to start ports:-90:80 / / mapped port when the docker service is started Volumes:-/ root/compose_test/webserver:/usr/share/nginx/html / / Local and container mounted directories / / write files note indentation [root@docker compose_test] # docker-compose up-d bind / generate the corresponding container / / "- d" option using the docker-compose.yml file in the current directory It means running in the background. If it is not specified, it will run in the foreground by default, which will occupy the terminal [root@docker compose_test] # docker ps / / view the container CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc674041cc65b nginx "nginx-g 'daemon of …" 8 minutes ago Up 8 minutes 0.0.0.0 80/tcp web_ 90-> 80/tcp web_ nginx [root @ docker compose_test] # echo "hello world" > webserver/index.html// create a test page [root@docker compose_test] # curl 127.0.0.1:90hello world// access test [root@docker compose_test] # docker-compose stop / / stop the container specified in the file [root] through the .yml file @ docker compose_test] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES// View effect [root@docker ~] # docker-compose-f / root/compose_test/docker-compose.yml up-dash / you can use the "- f" option to specify the container defined in the yml file startup file (2) compose+dockerfile build image [root@localhost ~] # mkdir compose & & cd compose / / create a test directory and enter [root@localhost compose] # vim Dockerfile / / create dockerfileFROM nginx:latestADD html / usr/share/nginx/html [root@localhost compose] # vim docker-compose.yml / / write the yml file version: "3" services: nginx: build:. / / specify the path to dockerfile here Can write relative path or absolute path container_name: mynginx / / generated container name image: mynginx / / use the image name generated by dockerfile restart: always ports:-70:80 [root@localhost compose] # mkdir html [root@localhost compose] # echo "hello world" > html/index.html / / To create a web page directory [root@localhost compose] # docker-compose build / / is to generate a dockerfile file into an image [root@localhost compose] # docker-compose up-d / / directly generate a container The previous command can ignore [root@localhost compose] # curl 127.0.0.1:70hello world// test effect (3) use .yml file to build a blog platform [root@localhost ~] # mkdir wordpress & & cd wordpress / / create a test directory [root@localhost wordpress] # vim docker-compose.yml / / write the yml file version: "3.1" services: wordprss: image: wordpress / / specify the image restart: always ports:-8080 environment: / / specify the mapped port environment: / / modify the environment variable WORDPRESS_DB_HOST: db WORDPRESS_DB_USER inside the container: Wordpress WORDPRESS_DB_PASSWORD: 123.com WORDPRESS_DB_NAME: wordpress db: image: mysql:5.7 restart: always environment: MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: 123.com MYSQL_ROOT_PASSWORD: 123.com [root@localhost wordpress] # docker-compose up-d / / generate the appropriate container And run [root@localhost wordpress] # netstat-anpt in the background | grep 8080 / / make sure the port is listening tcp6 0 0:: 8080: * LISTEN 5546/docker-proxy [root@localhost wordpress] # docker ps | grep word / / make sure the container is running 81dd5fe032a6 wordpress "docker-entrypoint.s …" 7 minutes ago Up 7 minutes 0.0.0.0 80/tcp wordpress_wordprss_1702b530d7679 mysql:5.7 8080-> 80/tcp wordpress_wordprss_1702b530d7679 mysql:5.7 "docker-entrypoint.s …" 7 minutes ago Up 7 minutes 3306/tcp, 33060/tcp wordpress_db_1 [root@localhost wordpress] # echo "net.ipv4.ip_forward = 1" > > / etc/sysctl.conf [root@localhost wordpress] # sysctl-pnet.ipv4.ip_forward = 1

You can now access the test page. As shown in the figure:

Visit the blog you set up successfully!

-this is the end of this article. Thank you for watching-

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

Servers

Wechat

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

12
Report