美文网首页菜鸟学编程Docker容器
How To Install and Use Docker Co

How To Install and Use Docker Co

作者: 菜鸟飞不动 | 来源:发表于2019-04-19 00:00 被阅读1次
    How To Install and Use Docker Compose on CentOS 7

    Docker Compose is a tool that allows you to define and run multi-container Docker applications.

    With Compose, you define the application’s services, networks and volumes in a single YAML file, then spin your application with a single command.

    Compose can be used for different purposes such as single host application deployments, automated testing and local development.

    This tutorial walks you through installing the latest version of Docker Compose on CentOS 7. We will also cover the basic Docker Compose concepts and commands.

    Prerequisites

    Ensure that you met the following prerequisites before continuing with this tutorial:

    Install Docker Compose on CentOS

    The recommended method for installing Docker Compose on CentOS 7 is by downloading the Compose binary from the Docker’s GitHub repository.

    At the time of writing this article, the latest stable version of Docker Compose is version 1.23.1. Before downloading the Compose binary visit the Compose repository release page on GitHub and check if there is a new version available for download.

    Complete the following steps to install Docker Compose on CentOS 7:

    1. Start by downloading the Docker Compose binary into the /usr/local/bin directory using the following curl 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
      

      Copy

    2. Once the download is complete, make the binary executable by typing:

      sudo chmod +x /usr/local/bin/docker-compose
      

      Copy

    3. To verify the installation type the following command to print the Compose version:

      docker-compose --version
      

      Copy

      The output will look something like this:

      docker-compose version 1.23.1, build b02f1306
      

      Copy

    Getting started with Docker Compose

    In this section we’ll show how to use Docker Compose to run a WordPress stack on your CentOS 7 machine.

    First create a new directory for the project and change into it:

    mkdir my_app && cd my_app
    

    Copy

    Next, create a file named docker-compose.yml inside the project directory:

    nano docker-compose.yml
    

    Copy

    Paste the following content:

    docker-compose.yml

    version: '3.3'
    
    services:
      db:
        image: mysql:5.7
        restart: always
        volumes:
          - db_data:/var/lib/mysql
        environment:
          MYSQL_ROOT_PASSWORD: password
          MYSQL_DATABASE: wordpress
    
      wordpress:
        image: wordpress
        restart: always
        volumes:
          - ./wp_data:/var/www/html
        ports:
          - "8080:80"
        environment:
          WORDPRESS_DB_HOST: db:3306
          WORDPRESS_DB_NAME: wordpress
          WORDPRESS_DB_USER: root
          WORDPRESS_DB_PASSWORD: password
        depends_on:
           - db
    
    volumes:
        db_data:
        wp_data:
    

    Copy

    Let’s analyze the code line by line.

    In the first line we are specifying the Compose file version. There are several different versions of the Compose file format with support for specific Docker releases.

    Next we are defining two services, db and wordpress. Each service runs one image and it will create a separate container when docker-compose is run.

    The db service:

    • Uses the mysql:5.7 image. In the image is not present on your system Compose will pull it from the Docker Hub public repository.
    • Uses the restart always policy which will instruct the container to always restart.
    • Creates a named volume db_data to persist the database.
    • Defines the environment variables for the mysql:5.7 image.

    The wordpress service:

    • Uses the wordpress image. In the image is not present on your system Compose will pull it from the Docker Hub public repository.
    • Uses the restart always policy which will instruct the container to always restart.
    • Mounts the wp_data directory on the host to /var/lib/mysql inside the container.
    • Forwards the exposed port 80 on the container to port 8080 on the host machine.
    • Defines the environment variables for the wordpress image.
    • The depends_on instruction defines the dependency between the two services. In this example, db will be started before wordpress.

    From the project directory, spin up the WordPress application using the following command:

    docker-compose up
    

    Copy

    The output should look something like this:

    ...
    wordpress_1  | [Sat Oct 13 21:30:48.286382 2018] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.2.10 configured -- resuming normal operations
    wordpress_1  | [Sat Oct 13 21:30:48.286425 2018] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
    

    Copy

    Compose will pull both images, start two containers and create the wp_data directory in your project directory.

    Open your browser, type http://0.0.0.0:8080/ in and you will see the Wordpress installation screen.

    At this point the Wordpress application is up and running and you can start working your theme or plugin.

    To stop Compose press CTRL+C.

    If you want to start the Compose in a detached mode use the -d flag:

    docker-compose up -d
    

    Copy

    To check the running services use the ps option:

    docker-compose ps
    

    Copy

           Name                     Command               State          Ports        
    ----------------------------------------------------------------------------------
    my_app_db_1          docker-entrypoint.sh mysqld      Up      3306/tcp, 33060/tcp 
    my_app_wordpress_1   docker-entrypoint.sh apach ...   Up      0.0.0.0:8080->80/tcp
    

    Copy

    When Compose is running in detached mode to stop the services use:

    docker-compose stop
    

    Copy

    To completely remove the containers use the down option:

    docker-compose down
    

    Copy

    Passing the --volumes switch will also remove the data volumes:

    docker-compose down --volumes
    

    Copy

    Uninstalling Docker Compose

    If you want to uninstall Docker Compose, simply delete the binary using the following command:

    sudo rm /usr/local/bin/docker-compose
    

    Copy

    Conclusion

    You have learned how to install and use Docker Compose on a CentOS 7.

    If you have any question, please leave a comment below.

    相关文章

      网友评论

        本文标题:How To Install and Use Docker Co

        本文链接:https://www.haomeiwen.com/subject/xmqxgqtx.html