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

Solution for VMware Workstation Pro not running on Windows

After the National Day holiday, did any of you fi...

Use Shell scripts to batch start and stop Docker services

Table of contents Start Docker Stop Docker Python...

Pure CSS to adjust Div height according to adaptive width (percentage)

Under the requirements of today's responsive ...

Implementing access control and connection restriction based on Nginx

Preface Nginx 's built-in module supports lim...

CentOS 6-7 yum installation method of PHP (recommended)

1. Check the currently installed PHP packages yum...

Example of implementing QR code scanning effects with CSS3

Online Preview https://jsrun.pro/AafKp/ First loo...

Vue uses vue meta info to set the title and meta information of each page

title: vue uses vue-meta-info to set the title an...

Mybatis implements SQL query interception and modification details

Preface One of the functions of an interceptor is...

Detailed explanation of JS homology strategy and CSRF

Table of contents Overview Same Origin Policy (SO...

Mysql 8.0 installation and password reset issues

Mysql 8.0 installation problems and password rese...

Solution to the routing highlighting problem of Vue components

Preface Before, I used cache to highlight the rou...

The grid is your layout plan for the page

<br /> English original: http://desktoppub.a...

Three ways to align div horizontal layout on both sides

This article mainly introduces three methods of i...