美文网首页菜鸟学编程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 Ubuntu 18.04

    Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a YAML file to configure the application’s services, networks and volumes.

    Compose can be used for different purposes. Single host application deployments, automated testing and local development are the most popular use cases for Docker Compose.

    In this tutorial we’ll show you how to install the latest version of Docker Compose on Ubuntu 18.04 and explore the basic Docker Compose concepts and commands.

    The same instructions apply for Ubuntu 16.04 and any other Debian based distribution, including Debian, Linux Mint and Elementary OS.

    Prerequisites

    Make sure that you have met the following prerequisites before continuing with this tutorial:

    Install Docker Compose on Ubuntu

    The Docker Compose installation package is available in the official Ubuntu 18.04 repositories but it may not always be the latest version. The recommended approach is to install Docker Compose 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.

    To install Docker Compose on Ubuntu 18.04, follow these steps:

    1. Download the Docker Compose binary into the /usr/local/bin directory with 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, apply executable permissions to the Compose binary:

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

      Copy

    3. Verify the installation by running the following command which will display 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 will show how to use Docker Compose to setup a multi-container WordPress application on your Ubuntu 18.04 machine.

    Start by creating a project directory and navigating into it:

    mkdir my_appcd my_app
    

    Copy

    Launch your text editor and 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, start up the WordPress application by running the following command:

    docker-compose up
    

    Copy

    The output should look something like this:

    ...
    wordpress_1  | [Sun Sep 23 22:31:43.499055 2018] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.2.10 configured -- resuming normal operations
    wordpress_1  | [Sun Sep 23 22:31:43.499796 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.

    Enter http://0.0.0.0:8080/ in your browser 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.

    You can also start the Compose in a detached mode by passing 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

    If you want to remove the containers entirely 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 for any reason you want to uninstall Docker Compose you can simply delete the binary by typing:

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

    Copy

    Conclusion

    You have learned how to install and use Docker Compose on Ubuntu 18.04. If you have any question, please leave a comment below.

    相关文章

      网友评论

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

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