In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use Compose, a sharp tool for Docker container choreography. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Introduction to Compose
The predecessor of Compose was officially renamed Compose,Compose backward compatible Fig after Fig,Fig was acquired by Docker. Compose is a tool for defining and running multi-container Docker applications. You only need a Compose configuration file and a simple command to create and run all the containers needed for the application. In the configuration file, all containers are defined by services and use the docker-compose command to start or stop containers and all dependent containers.
Install Compose
There are many ways to install Compose. It is recommended to use the curl command to install it. To make sure that Docker is installed on your machine before installation, you can run the sudo docker version command to confirm that Docker has been installed. So far, the latest release of Compose is 1.11.2. Here is a demonstration of installing Compose on a Linux host that already has Docker installed.
The installation is simple, simply by executing the following command:
Sudo curl-L "https://github.com/docker/compose/releases/download/1.11.2/docker-compose-$(uname-s)-$(uname-m)"-o / usr/local/bin/docker-compose
After waiting for the installation to complete, execute the following command to add executable permissions to docker-compose:
Chmod + x / usr/local/bin/docker-compose
Enter the docker-compose-- version command to view the installation results.
In addition to this installation, you can also install or install Compose as a Docker container through the Python pip command, see https://docs.docker.com/compose/install/#install-as-a-container for details.
If you want to uninstall Compose, you can execute the sudo rm / usr/local/bin/docker-compose command.
Getting started with Compose
Let's demonstrate the steps of using Compose through a simple example, using Python to build a Web application that uses the Flask framework and maintains a hit count in Redis (it doesn't matter if you're not familiar with Python, you don't even need to install Python and Redis, we'll get these dependent environments from the container).
Create a project
First, you need a folder as the project folder:
Mkdir composetestcd composetest
Create an app.py file under the project folder, and copy and paste the following code into the file:
From flask import Flaskfrom redis import Redisapp = Flask (_ _ name__) redis = Redis (host='redis', port=6379) @ app.route ('/') def hello (): count = redis.incr ('hits') return' Hello World! I have been seen {} times.\ n'.format (count) if _ name__ = "_ _ main__": app.run (host= "0.0.0.0", debug=True)
Create a requirements.txt file under the project folder, and copy and paste the following code into the file:
Flaskredis
At this point, we have completed the new project, coding, adding dependencies and so on.
Create Dockerfile
Let's create a Dockerfile file to build a Docker image that contains all the dependencies for running the Web application, including the Python runtime environment.
Create a Dockerfile file under the project folder, and copy and paste the following into the file:
FROM python:3.4-alpineADD. / codeWORKDIR / codeRUN pip install-r requirements.txtCMD ["python", "app.py"]
Roughly explain this configuration file:
Use python-3.4-alpine as the base image
Add the current directory to the mirror / code directory
Set / code to the working directory
Install Python dependencies
Set the default execution command
Define services in the Compose file
Create a docker-compose.yml file under the project folder, and copy and paste the following into the file:
Version: '2'services: web: build: .ports:-"5000 build 5000" volumes: -.: / code redis: image: "redis:alpine"
The configuration file contains two services, web and redis. Web builds the image using the Dockerfile file in the current directory, exposes port 5000 of the container to the host, and then mounts the project folder to the / code directory in the container; redis is built using the officially released image.
Build and run
Execute the following command to build and run the container:
Sudo docker-compose up
After the container is built and started, you can enter http://localhost:5000 in the browser to view the results. The page prints "Hello World! I have been seen 1 times." and when the page is refreshed, the count adds up to 2.
Update application
Because the project folder is mounted in the container, we can modify the application of the project folder directly, and the result of the modification is immediately reflected in the container without having to restart the container. Modify the return value in the hello method in the app.py file to the following:
Return 'Hello from Docker! I have been seen {} times.\ n'.format (count)
Refresh the browser after saving and find that the print result has been updated.
Other commands of Compose
The Componse mentioned above uses the command to build and start the container, which is started in the foreground. If you want to start it in the background, you can add the parameter-d, such as the following:
Sudo docker-compose up-d
The docker-compose ps command can view the containers that are running:
Liuwei@liuwei-Ubuntu:~$ sudo docker-compose psName Command State Ports-composetest_redis_1 docker-entrypoint.sh redis... Up 6379/tcpcomposetest_web_1 python app.py Up 0.0.0.0 5000-> 5000/tcp
If you use the sudo docker-compose up-d command to start in the background, you can stop it with the docker-compose stop command. The docker-compose down-- volumes command stops the container and deletes it, and-- volumns means to delete the redis data file directory as well.
More commands about Compose can be seen at sudo docker-compose-- help.
This is a basic use of Compose. You can find that Compose integrates docker run commands into a docker-compose.yml configuration file, which is convenient for the management of large Docker clusters. For example, you can combine multiple service into more complex service groups, specify a different Dockerfile for each service, and then link them together.
This is the end of this article on "how to use Compose, a sharp tool for Docker container layout". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.