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

Explain in detail how to use the Docker-Compose command

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

You can manage and deploy Docker containers in a variety of ways. You can use the Docker command directly, with one of the many GUI tools (Web-based tools and desktop client-oriented tools), or you can choose to take the docker-compose path.

What is Docker Compose? Docker Compose is used to create containers and connections between containers. However, the docker-compose command is actually much more versatile. Use this command to build an image, extend the container, repair the container, view the output of the container, list the common ports of the container, and so on.

So how do you use docker-compose? You might as well find out.

How do I install Docker Compose?

Even if you already have Docker installed on the server, you probably don't have Docker Compose installed. To install Docker Compose, execute the following command:

Sudo curl-L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname-s)-$(uname-m)"-o / usr/local/bin/docker-compose sudo chmod + x / usr/local/bin/docker-compose

Verify the installation using the following command:

Docker-compose version

You should see the version numbers of several applications (figure A).

Figure A. Docker Compose is installed and ready

Docker file

To deploy the container, Docker Compose relies on the docker-compose.yml file, which is used to deploy the Docker container to your specific environment. Suppose you want to deploy the Wordpress container. First create a new directory using the following command:

Mkdir ~ / wordpressbuild

Change to the new directory using the following command:

Cd ~ / wordpressbuild

Create a new Docker Compose file using the following command:

Nano docker-compose.yml

Paste the following (from the official Docker Compose document) into the file:

Version: '3.3' services: db: image: mysql:5.7 volumes:-db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on:-db image: wordpress:latest ports:-"8000 image 80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes: db_data: {}

Save and close the file.

Now let's build the project and deploy the container in detached mode using the following command:

Docker-compose up-d

This command downloads all the necessary images (MySQL and Wordpress in this case) and then deploys the service on port 8000. You can point your Web browser to http://SERVER_IP:8000(, where SERVER_IP is the IP address of the managed server, and view the Wordpress installation page (figure B).

Figure B. Wordpress installer

How do I check your deployment?

Suppose you want to check the logs from the deployment. To do this, execute the following command:

Docker-compose logs

You should see a lot of information from the last deployment (figure C).

Figure C. View logs for docker-compose deployments from Wordpress

This command will output a lot of information (especially if you deploy many containers). Instead, you can specify the service for which you want to view log files. How do I know which service name to use? Check the docker-compose.yml file. In the example in this article, we have two services:

Db: database wordpress:Wordpress container

Therefore, if you only want to view the log of the wordpress service, the command will be:

Docker-compose logs wordpress

You can also view the output of the log (just like using the tail command), as shown below:

Docker-compose logs-f wordpress

As long as the new information about the wordpress service is logged, it appears in the terminal window (figure D).

Figure D. View the wordpress service log

What if you forget which ports are used in your deployment? You can view the docker-compose.yml file or use the port option with the docker-compose command. You need to know the internal orders of the service. For example, Wordpress defaults to port 80, so we know this is the internal port. But what do we assign as a network-facing port? Check it out with the following command:

Docker-compose port wordpress 80

The output of this command will show that we are mapping internal port 80 to external port 8000 (figure E).

Figure E. Port mapping for Wordpress

If you can't remember the container you have to deploy, you can execute the command:

Docker-compose ps

The output lists each container that has been deployed (figure F).

Figure F. Container list

It's just the beginning.

This should allow you to begin to appreciate the power of Docker Compose. We'll detail the docker-compose.yml file in the next article to figure out how to build our own containers.

Original title: How to use the docker-compose command, author: Jack Wallen

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

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