{"id":58816,"date":"2023-04-17T15:53:59","date_gmt":"2023-04-17T15:53:59","guid":{"rendered":"https:\/\/linuxiac.com\/?p=58816"},"modified":"2024-09-04T21:19:14","modified_gmt":"2024-09-04T21:19:14","slug":"how-to-install-and-use-docker-compose","status":"publish","type":"post","link":"https:\/\/linuxiac.com\/how-to-install-and-use-docker-compose\/","title":{"rendered":"How to Install and Use Docker Compose: A Beginners Guide"},"content":{"rendered":"\n<div class=\"wp-block-ultimate-post-table-of-content ultp-block-4be037\"><div class=\"ultp-block-wrapper\"><div class=\"ultp-block-toc\"><div class=\"ultp-toc-header\"><div class=\"ultp-toc-heading\">Table of Contents<\/div><div class=\"ultp-collapsible-toggle  ultp-collapsible-right\"><a class=\"ultp-collapsible-text ultp-collapsible-open\" href=\"javascript:;\">[Open]<\/a><a class=\"ultp-collapsible-text ultp-collapsible-hide\" href=\"javascript:;\">[Close]<\/a><\/div><\/div><div class=\"ultp-block-toc-style1 ultp-block-toc-body\" style=\"display:block;\"><ul class=\"ultp-toc-lists\"><li><a href=\"#Whats_Docker_Compose?\">What&#8217;s Docker Compose?<\/a><\/li><li><a href=\"#Why_Use_Docker_Compose?\">Why Use Docker Compose?<\/a><\/li><li><a href=\"#How_to_Install_Docker_Compose\">How to Install Docker Compose<\/a><\/li><li><a href=\"#Creating_a_Docker-Compose_File\">Creating a Docker-Compose File<\/a><\/li><li><a href=\"#Commonly_Used_Docker_Compose_Commands\">Commonly Used Docker Compose Commands<\/a><ul class=\"ultp-toc-lists\"><li><a href=\"#Build_amp;_Run_Containers\">Build &amp; Run Containers<\/a><\/li><li><a href=\"#Stop_amp;_Remove_Containers\">Stop &amp; Remove Containers<\/a><\/li><li><a href=\"#List_Docker_Containers\">List Docker Containers<\/a><\/li><li><a href=\"#Stop_Containers\">Stop Containers<\/a><\/li><li><a href=\"#Start_Containers\">Start Containers<\/a><\/li><li><a href=\"#View_Logs\">View Logs<\/a><\/li><li><a href=\"#Remove_Container\">Remove Container<\/a><\/li><\/ul><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div><\/div><\/div><\/div>\n\n\n\n<p><a href=\"https:\/\/linuxiac.com\/what-is-docker-container\/\">Docker<\/a> is a tool that revolutionizes the way we build and deploy applications. Docker Compose is an essential addition to it that expands its capabilities and makes it easier to work with.<\/p>\n\n\n\n<p>However, getting started can be overwhelming if you&#8217;re new to Docker Compose. To facilitate this, in this article, we&#8217;ll cover the basics of Docker Compose and show you how to create a simple multi-container application.<\/p>\n\n\n\n<p>By the end of this guide, you&#8217;ll understand how to install and use Docker Compose to build and deploy your own applications. But first, let&#8217;s answer an important question.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Whats_Docker_Compose?\">What&#8217;s Docker Compose?<\/h2>\n\n\n\n<p>Docker Compose is a platform-neutral tool for defining and running multi-container Docker applications. It allows you to define a group of services, their dependencies, and the configuration to run them declaratively using a YAML file.<\/p>\n\n\n\n<p>Then, Docker Compose uses this file to create and start all the necessary containers with a single command. It is a powerful tool widely used for development, testing, and production environments for managing containerized applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Why_Use_Docker_Compose?\">Why Use Docker Compose?<\/h2>\n\n\n\n<p>Docker Compose simplifies managing multi-container applications by providing an easy way to orchestrate and manage their dependencies, network, and storage configuration.<\/p>\n\n\n\n<p>Here are some of the main reasons why to use Docker Compose.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Simplified Configuration<\/strong>: With Docker Compose, you can define all the services and their configuration in a single YAML file, which makes it easier to manage complex setups with multiple containers and dependencies.<\/li>\n\n\n\n<li><strong>Seamless Communication<\/strong>: Docker Compose automatically creates a network between the containers in your application, allowing them to communicate seamlessly without any extra setup.<\/li>\n\n\n\n<li><strong>Variables Support<\/strong>: Docker Compose supports variables in Compose&#8217;s YAML file. These variables can be used to customize your deployments to different environments and needs.<\/li>\n\n\n\n<li><strong>Consistent Development and Testing Environments<\/strong>: With Docker Compose, you can define the same environment for your development and testing machines, which helps avoid compatibility issues and ensures that your application behaves consistently across different environments.<\/li>\n<\/ol>\n\n\n\n<p>Having specified all that, let us show you how to install it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"How_to_Install_Docker_Compose\">How to Install Docker Compose<\/h2>\n\n\n\n<p>First, ensure you have Docker installed on your system before installing Docker Compose, as it must be present for Docker Compose to work. So, let&#8217;s check if Docker is available on our system:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker -v<\/code><\/span><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-version-check.jpg\"><img decoding=\"async\" width=\"948\" height=\"188\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-version-check.jpg\" alt=\"Docker's version check.\" class=\"wp-image-59581\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-version-check.jpg 948w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-version-check-380x75.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-version-check-768x152.jpg 768w\" sizes=\"(max-width: 948px) 100vw, 948px\" \/><\/a><figcaption class=\"wp-element-caption\">Docker&#8217;s version check.<\/figcaption><\/figure>\n\n\n\n<p>As you can see from the command output above, Docker is installed on our machine. However, if you get an error message like &#8220;<em>bash: docker: command not found\u2026<\/em>&#8220;, you&#8217;ll need to install Docker before proceeding.<\/p>\n\n\n\n<p>We&#8217;ve got you covered if you need help with how to do it. Here are our easy-to-follow guides for&nbsp;<a href=\"https:\/\/linuxiac.com\/install-docker-on-ubuntu\/\" target=\"_blank\" rel=\"noreferrer noopener\">Ubuntu<\/a>,&nbsp;<a href=\"https:\/\/linuxiac.com\/how-to-install-docker-on-debian-11-bullseye\/\" target=\"_blank\" rel=\"noreferrer noopener\">Debian<\/a>,&nbsp;<a href=\"https:\/\/linuxiac.com\/how-to-install-docker-on-linux-mint-21\/\" target=\"_blank\" rel=\"noreferrer noopener\">Linux Mint<\/a>, <a href=\"https:\/\/linuxiac.com\/how-to-install-docker-on-fedora-linux\/\">Fedora<\/a>, <a href=\"https:\/\/linuxiac.com\/how-to-install-docker-on-raspberry-pi\/\">Raspberry Pi<\/a>,&nbsp;<a href=\"https:\/\/linuxiac.com\/how-to-install-docker-on-almalinux-or-rocky-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">AlmaLinux<\/a>, and&nbsp;<a href=\"https:\/\/linuxiac.com\/how-to-install-docker-on-almalinux-or-rocky-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">Rocky Linux<\/a>. Now back on topic.<\/p>\n\n\n\n<p>To start with Docker Compose, you&#8217;ll need to install it on your local machine first. Fortunately, the process is relatively straightforward and can be completed with just two commands.<\/p>\n\n\n\n<p>The first one is to download Docker Compose, which is a single binary:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code lang=\"bash\" class=\"hljs language-javascript language-bash\">sudo curl -L <span class=\"hljs-string\">\"https:\/\/github.com\/docker\/compose\/releases\/latest\/download\/docker-compose-linux-$(uname -m)\"<\/span> -o \/usr\/local\/bin\/docker-compose<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Then, set executable permissions on the file:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">sudo chmod +x \/usr\/local\/bin\/docker-compose<\/code><\/span><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>That&#8217;s it. Let&#8217;s see whether we already have Docker Compose installed:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose version<\/code><\/span><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-version-check.jpg\"><img decoding=\"async\" width=\"945\" height=\"187\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-version-check.jpg\" alt=\"Docker Compose's version check.\" class=\"wp-image-59587\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-version-check.jpg 945w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-version-check-380x75.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-version-check-768x152.jpg 768w\" sizes=\"(max-width: 945px) 100vw, 945px\" \/><\/a><figcaption class=\"wp-element-caption\">Docker Compose&#8217;s version check.<\/figcaption><\/figure>\n\n\n\n<p>Everything seems fine &#8211; time to test our Docker Compose installation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Creating_a_Docker-Compose_File\">Creating a Docker-Compose File<\/h2>\n\n\n\n<p>To use Docker Compose, you must first create a &#8220;<em>docker-compose.yaml<\/em>&#8221; file describing the containers in your application&#8217;s stack. It describes the services, environment variables, volumes, and networks our containers will use.<\/p>\n\n\n\n<p>It is important to remember that containers declared in the &#8220;<em>docker-compose.yaml<\/em>&#8221; file automatically become members on the same stack.<\/p>\n\n\n\n<p>In other words, they&#8217;re linked in their own Docker network, which is automatically created by Docker Compose when they are started, so they can communicate using their service names as hostnames described in the &#8220;<em>docker-compose.yaml<\/em>&#8221; file.<\/p>\n\n\n\n<p>For example, the Docker Compose YAML file below deploys two containers, MySQL and <a href=\"https:\/\/linuxiac.com\/how-to-install-phpmyadmin-with-docker-compose\/\"  data-wpil-monitor-id=\"89\">phpMyAdmin<\/a>. As a result, you&#8217;ll have a running database and a convenient web-based UI to administer it in minutes.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code lang=\"docker\" class=\"hljs language-php language-docker\">version: <span class=\"hljs-string\">'3.8'<\/span>\n\n<span class=\"hljs-comment\"># Services<\/span>\nservices:\n\n    <span class=\"hljs-comment\"># MySQL Service<\/span>\n    mysql:\n        image: mysql:latest\n        environment:\n            MYSQL_ROOT_PASSWORD: your_password\n        volumes:\n            - mysqldata:\/<span class=\"hljs-keyword\">var<\/span>\/lib\/mysql\n\n    <span class=\"hljs-comment\"># phpMyAdmin Service<\/span>\n    phpmyadmin:\n        image: phpmyadmin\/phpmyadmin:latest\n        ports:\n            - <span class=\"hljs-number\">8080<\/span>:<span class=\"hljs-number\">80<\/span>\n        environment:\n            PMA_HOST: mysql\n        depends_on:\n            - mysql\n\n<span class=\"hljs-comment\"># Volumes<\/span>\nvolumes:\n  mysqldata:<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Copy and save the above content to a file named &#8220;<em>docker-compose.yaml<\/em>.&#8221; Don&#8217;t forget to change the password for the &#8220;<em>MYSQL_ROOT_PASSWORD<\/em>&#8221; variable to another of your choices.<\/p>\n\n\n\n<p>Then, from the directory where the file is placed, open the terminal and type:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose up -d<\/code><\/span><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-run-test-deployment.jpg\"><img decoding=\"async\" width=\"1024\" height=\"358\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-run-test-deployment-1024x358.jpg\" alt=\"Starting a deployment with Docker Compose.\" class=\"wp-image-59602\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-run-test-deployment-1024x358.jpg 1024w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-run-test-deployment-380x133.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-run-test-deployment-768x269.jpg 768w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-run-test-deployment.jpg 1098w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Starting a deployment with Docker Compose.<\/figcaption><\/figure>\n\n\n\n<p>The command will begin to pull (download) the MySQL and phpMyAdmin Docker images from <a href=\"https:\/\/hub.docker.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Docker Hub<\/a>. This will take a while. Once the download is complete and the containers are successfully run, their status will change to &#8220;<em>Started<\/em>.&#8221;<\/p>\n\n\n\n<p>Congratulations! Your first Docker Compose deployment was created successfully. Now, open your browser and navigate to &#8220;<em>http:\/\/localhost:8080<\/em>.&#8221; The phpMyAdmin login page will greet you.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-login.jpg\"><img decoding=\"async\" width=\"1024\" height=\"682\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-login-1024x682.jpg\" alt=\"phpMyAdmin login page.\" class=\"wp-image-59614\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-login-1024x682.jpg 1024w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-login-380x253.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-login-768x512.jpg 768w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-login.jpg 1151w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">phpMyAdmin login page.<\/figcaption><\/figure>\n\n\n\n<p>Use the user &#8220;<em>root<\/em>&#8221; and the password you specified in the &#8220;<em>docker-compose.yaml<\/em>&#8221; file to log into phpMyAdmin and start administrating your dockerized MySQL container.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-ui.jpg\"><img decoding=\"async\" width=\"1024\" height=\"681\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-ui-1024x681.jpg\" alt=\"phpMyAdmin UI\" class=\"wp-image-59617\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-ui-1024x681.jpg 1024w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-ui-380x253.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-ui-768x511.jpg 768w, https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/phpmyadmin-ui.jpg 1263w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">phpMyAdmin UI<\/figcaption><\/figure>\n\n\n\n<p>This simple example should give you an idea of how to use Docker Compose to deploy multi-container applications. Now, let&#8217;s review the essential Docker Compose commands for managing them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Commonly_Used_Docker_Compose_Commands\">Commonly Used Docker Compose Commands<\/h2>\n\n\n\n<p>If you&#8217;re familiar with Docker, using Docker Compose commands will seem very familiar, as they overlap and have similar functionality. So let&#8217;s take a look at the most important ones.<\/p>\n\n\n\n<p>We stress that the <code>docker-compose<\/code> command must always be run from the directory containing the &#8220;<em>docker-compose.yaml<\/em>&#8221; file defining the corresponding containerized service(s).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Build_amp;_Run_Containers\">Build &amp; Run Containers<\/h3>\n\n\n\n<p>The&nbsp;<code>docker-compose up<\/code>&nbsp;command builds and runs the Docker images, starts the containers, and attaches to containers for a service. However, it is often used with an added &#8220;<code>-d<\/code> (<code>--detach<\/code>)&#8221; flag, indicating that a Docker container is running in the background of the terminal.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose up -d<\/code><\/span><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If you omit to add it, the containers will remain stuck to your terminal session and will be terminated when you press &#8220;<em>Ctrl+C<\/em>,&#8221; for example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Stop_amp;_Remove_Containers\">Stop &amp; Remove Containers<\/h3>\n\n\n\n<p>The command shown below will&nbsp;stop and remove&nbsp;containers and all the networks:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose down<\/code><\/span><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In addition, if you add the &#8220;<code>-v<\/code> (<code>--volumes<\/code>)&#8221; flag to it, all volumes associated with the services defined in &#8220;<em>docker-compose.yaml<\/em>&#8221; will also be deleted. So use it with caution!<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose down -v<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"List_Docker_Containers\">List Docker Containers<\/h3>\n\n\n\n<p>To view the&nbsp;current status&nbsp;of the running containers in a Docker Compose application, execute the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose ps<\/code><\/span><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>However, this will not show you the stopped containers. So, add the &#8220;<code>-a<\/code> (<code>--all<\/code>)&#8221; argument to the command to show the status of all service-defined ones in the &#8220;<em>docker-compose.yaml<\/em>&#8221; file.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose ps -a<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"Stop_Containers\">Stop Containers<\/h3>\n\n\n\n<p>You can&nbsp;stop&nbsp;all the running containers defined in the &#8220;<em>docker-compose.yaml<\/em>&#8221; file without removing them by using the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose stop<\/code><\/span><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>To stop only an individual container, add a reference after the command to the service name defined as such in the &#8220;<em>docker-compose.yaml<\/em>&#8221; file. For example:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose stop mysql<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"Start_Containers\">Start Containers<\/h3>\n\n\n\n<p>To&nbsp;start all existing containers&nbsp;that were previously created with the&nbsp;<code>docker-compose up<\/code> command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose start<\/code><\/span><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Add the service name after the command to run only a single one. For example:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose start mysql<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"View_Logs\">View Logs<\/h3>\n\n\n\n<p>Sometimes, our Docker Compose deployments fail. One of the main ways to find out what went wrong in such a situation is by examining the containers&#8217; logs.<\/p>\n\n\n\n<p>To do so, enter the <code>docker-compose logs<\/code> command followed by the service name as you defined it in the &#8220;<em>docker-compose.yaml<\/em>&#8221; file. For example:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose logs mysql<\/code><\/span><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>A handy option is to add the &#8220;<code>-f<\/code> (<code>--follow<\/code>)&#8221; argument, allowing you to track what is happening in the corresponding container in real-time.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose logs -f mysql<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"Remove_Container\">Remove Container<\/h3>\n\n\n\n<p>To remove stopped service containers, type the following:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker-compose rm<\/code><\/span><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In addition, to remove any anonymous volumes attached to containers, use the &#8220;<code>-v<\/code> (<code>--volume<\/code>)&#8221; argument:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">docker compose rm -v<\/code><\/span><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Finally, we highly recommend checking out <a href=\"https:\/\/docs.docker.com\/compose\/reference\/\" target=\"_blank\" rel=\"noreferrer noopener\">Compose&#8217;s CLI documentation<\/a> for a detailed reference and in-depth coverage of all Docker Compose commands.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Conclusion\">Conclusion<\/h2>\n\n\n\n<p>Docker Compose is an essential tool for any developer or system administrator looking to streamline their workflow and manage multiple containers.<\/p>\n\n\n\n<p>With its easy-to-use syntax and powerful features, Docker Compose makes it possible to quickly set up complex multi-container environments and test your applications in various scenarios.<\/p>\n\n\n\n<p>By following the steps outlined in this guide, you should now understand how to install and use Docker Compose to simplify your development process.<\/p>\n\n\n\n<p>In addition, check out the&nbsp;<a href=\"https:\/\/docs.docker.com\/compose\/\" target=\"_blank\" rel=\"noreferrer noopener\">project\u2019s official documentation<\/a>&nbsp;to learn more about Docker Compose.<\/p>\n\n\n\n<p>We hope that we were helpful to you. Any comments or suggestions are welcome in the comments section below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>New to Docker Compose? This guide will help you get started with installation and usage basics for your multi-container applications.<\/p>\n","protected":false},"author":10,"featured_media":58819,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,4417],"tags":[5233,3470,1439,4915],"class_list":["post-58816","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","category-linux-knowledge","tag-containerization","tag-containers","tag-docker","tag-docker-compose"],"blocksy_meta":{"has_hero_section":"default","hero_section":"type-2","pageTitleFont":{"family":"Default","variation":"Default","size":{"desktop":"2.8em","tablet":"2.8em","mobile":"2.8em","__changed":[]},"line-height":"CT_CSS_SKIP_RULE","letter-spacing":"CT_CSS_SKIP_RULE","text-transform":"CT_CSS_SKIP_RULE","text-decoration":"CT_CSS_SKIP_RULE"},"hero_elements":[{"id":"custom_title","enabled":true,"heading_tag":"h1","title":"Home","__id":"R8sCUd99YIU7gmNkmf1xZ"},{"id":"custom_description","enabled":false,"description_visibility":{"desktop":true,"tablet":true,"mobile":false},"__id":"cgaKbvkLIBdoOrZeqAnb8"},{"id":"custom_meta","enabled":true,"meta_elements":[{"id":"author","enabled":true,"label":"By","has_author_avatar":"no","avatar_size":25,"__id":"K19AM0E0hfrQBU42cMumn"},{"id":"post_date","enabled":true,"label":"On","date_format_source":"default","date_format":"M j, Y","__id":"x-C-K35cNbH4huMf3QIRg"},{"id":"updated_date","enabled":false,"label":"On","date_format_source":"default","date_format":"M j, Y","__id":"LilSh5IBVE4KkEsUQtdgL"},{"id":"categories","enabled":false,"label":"In","style":"simple","__id":"aHiFWOPtRFvNb9kSsR3EH"},{"id":"comments","enabled":true,"__id":"wZ1DT0QGS8LG7HfNl1rCh"}],"page_meta_elements":{"joined":true,"articles_count":true,"comments":true},"__id":"Cu1Swym9R4NkZLqBwNJz1","meta_type":"label"},{"id":"breadcrumbs","enabled":false,"__id":"IchH5zCyA4E6IJ87YfBW2"}],"hero_structure":"normal","page_title_bg_type":"custom_image","custom_hero_background":{"attachment_id":58822,"url":"https:\/\/linuxiac.com\/wp-content\/uploads\/2023\/04\/docker-compose-header-1024x237.jpg"},"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"_links":{"self":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts\/58816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/comments?post=58816"}],"version-history":[{"count":0,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts\/58816\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/media\/58819"}],"wp:attachment":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/media?parent=58816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/categories?post=58816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/tags?post=58816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}