Detailed explanation of how to use Docker-Compose commands

Detailed explanation of how to use Docker-Compose commands

You can manage and deploy Docker containers in a variety of ways. You can use Docker commands directly, use one of the many GUI tools (both web-based and for desktop clients), or go the docker-compose route.

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 images, scale containers, repair containers, view the output of a container, list a container's public ports, and more.

So how do you use docker-compose? Let’s take a look.

How to install Docker Compose?

Even if you already have Docker installed on your server, you most likely 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 several application version numbers (Figure A).

Figure A. Docker Compose is installed and ready to go.

Dockerfile

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

mkdir ~/wordpressbuild

Change into this new directory with the following command:

cd ~/wordpressbuild

Create a new Docker Compose file with the following command:

nano docker-compose.yml

Paste the following content (taken from the official Docker Compose documentation) 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: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 we build the project and deploy the container in detached mode using the following commands:

docker-compose up –d

This command will download all the required images (MySQL and Wordpress in this case) and then deploy the service on port 8000. You can point your Web browser to http://SERVER_IP:8000 (where SERVER_IP is the IP address of your hosting server) to view the WordPress installation page (Figure B).

Figure B. The WordPress installer

How to check your deployment?

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

docker-compose logs

You should see a lot of information from the previous deployment (Figure C).

Figure C. Viewing the logs from the Docker Compose deployment of Wordpress

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

  • db: database
  • wordpress: WordPress container

So if you only wanted to view the logs for the wordpress service, the command would be:

docker-compose logs wordpress

You can also view the log output (just like using the tail command) as follows:

docker-compose logs -f wordpress

Whenever new information is logged to the WordPress service, it will appear in the Terminal window (Figure D).

Figure D. View the WordPress service log

What if you forget which ports were used in your deployment? You can either look in the docker-compose.yml file or use the port option with the docker-compose command. You need to know the internal commands of the service. For example, WordPress uses port 80 by default, so we know that this is the internal port. But what do we assign as the network-facing port? Let's find out with the following command:

docker-compose port wordpress 80

The output of this command will show that we mapped internal port 80 to external port 8000 (Figure E).

Figure E. Port mapping for Wordpress

If you don't remember the deployed container, you can execute the command:

docker-compose ps

The output lists each container that was deployed (Figure F).

Figure F. Container list

Just getting started

This should allow you to start to appreciate the power of Docker Compose. We will take a closer look at 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 full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Two simplest ways to install docker-compose
  • Detailed explanation of common Docker Compose commands
  • Docker Compose Tutorial
  • Detailed examples of using Docker-Compose
  • Detailed installation and use of docker-compose
  • Docker compose configuration file writing and command usage examples

<<:  Summary of installation steps and problems encountered in decompressing the mysql5.7.24 version

>>:  Vue implements dynamic query rule generation component

Recommend

Linux disk sequential writing and random writing methods

1. Introduction ● Random writing will cause the h...

Optimized implementation of count() for large MySQL tables

The following is my judgment based on the data st...

MySQL index usage monitoring skills (worth collecting!)

Overview In a relational database, an index is a ...

JavaScript - Using slots in Vue: slot

Table of contents Using slots in Vue: slot Scoped...

Negative margin function introduction and usage summary

As early as in the CSS2 recommendations in 1998, t...

An example of vertical centering of sub-elements in div using Flex layout

1. Flex is the abbreviation of Flexible Box, whic...

CentOS uses expect to remotely execute scripts and commands in batches

Sometimes we may need to operate servers in batch...

Tomcat parses XML and creates objects through reflection

The following example code introduces the princip...

How to implement element floating and clear floating with CSS

Basic Introduction to Floating In the standard do...

How to prohibit vsftpd users from logging in through ssh

Preface vsftp is an easy-to-use and secure ftp se...

Example to explain the size of MySQL statistics table

Counting the size of each table in each database ...