{"id":93377,"date":"2020-10-20T12:00:53","date_gmt":"2020-10-20T09:00:53","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-is-docker-compose-and-how-do-you-use-it-cloudsavvy-it\/"},"modified":"2020-10-20T12:00:53","modified_gmt":"2020-10-20T09:00:53","slug":"what-is-docker-compose-and-how-do-you-use-it-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-is-docker-compose-and-how-do-you-use-it-cloudsavvy-it\/","title":{"rendered":"#What Is Docker Compose, and How Do You Use It? \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_89 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6aee4b5c3c318\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6aee4b5c3c318\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-docker-compose-and-how-do-you-use-it-cloudsavvy-it\/#What_Does_Docker_Compose_Do\" >What Does Docker Compose Do?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-docker-compose-and-how-do-you-use-it-cloudsavvy-it\/#How_Do_You_Use_Docker_Compose\" >How Do You Use Docker Compose?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-docker-compose-and-how-do-you-use-it-cloudsavvy-it\/#Building_With_Docker_Compose\" >Building With Docker Compose<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What Is Docker Compose, and How Do You Use It? \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-503\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/576bdb68675c300a17d82c131b23c927\/p\/uploads\/2019\/06\/c454d054.png\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Docker Compose is a tool you can use to centrally manage the deployments of many different Docker containers. It\u2019s an important tool for any <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lication that needs multiple microservices, as it allows each service to easily be in a separately managed container.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_Does_Docker_Compose_Do\"><\/span>What Does Docker Compose Do?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker containers are used for running applications in an isolated environment. It\u2019s quite common nowadays to see application deployments done in Docker for the numerous benefits it brings. However, it\u2019s often not as simple as just running a single container. Usually you may have many containers coming together to act as one cohesive service made up of many moving parts.<\/p>\n<p>Managing all of these at deployment time is messy, so to clean it up, Docker provides <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/compose\/\">Docker Compose<\/a>, a configuration tool used for running multiple containers at once. You can define all of the configuration in one YAML file, and then start all the containers with one command.<\/p>\n<p>Rather than having all your services in one big container, Docker Compose allows you to split them up into individually manageable containers. This is both better for building and deployment, as you can manage all of them in separate codebases, and don\u2019t need to manually start each individual container.<\/p>\n<p>Using Docker Compose is a three step process:<\/p>\n<ul>\n<li>Build the component images using their Dockerfiles, or pull them from a registry.<\/li>\n<li>Define all of the component services in a <code>docker-compose.yml<\/code>\u00a0file.<\/li>\n<li>Run all of them together using the <code>docker-compose<\/code>\u00a0CLI.<\/li>\n<\/ul>\n<p>Docker Compose isn\u2019t another kind of Dockerfile. You will still need to build and publish your Docker containers using a Dockerfile. But, instead of running them directly, you can use Docker Compose to manage the configuration of a multi-container deployment.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_Do_You_Use_Docker_Compose\"><\/span>How Do You Use Docker Compose?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The configuration for a docker compose file is done in <code>docker-compose.yml<\/code>. You don\u2019t need to place this at the root of your project like a Dockerfile. In fact, it can go anywhere, as it doesn\u2019t depend on any other code. However, if you\u2019re building the images locally, it will need to go in a project folder with the code being built.<\/p>\n<p>A Compose configuration file will look something like the following. This configuration runs a WordPress instance using the <code>wordpress<\/code>\u00a0container off the Docker Hub. However, this depends on a MySQL database, which is also created by Compose.<\/p>\n<pre>version: '3'&#13;\n&#13;\nservices:&#13;\n   db:&#13;\n     image: mysql:5.7&#13;\n     volumes:&#13;\n       - db_data:\/var\/lib\/mysql&#13;\n     restart: always&#13;\n     environment:&#13;\n       MYSQL_ROOT_PASSWORD: rootpasswordchangeme&#13;\n       MYSQL_DATABASE: wordpress&#13;\n       MYSQL_USER: usernamechangeme&#13;\n       MYSQL_PASSWORD: passwordchangeme&#13;\n&#13;\n   wordpress:&#13;\n     depends_on:&#13;\n       - db&#13;\n     image: wordpress:latest&#13;\n     ports:&#13;\n       - \"8000:80\"&#13;\n     restart: always&#13;\n     environment:&#13;\n       WORDPRESS_DB_HOST: db:3306&#13;\n       WORDPRESS_DB_USER: usernamechangeme&#13;\n       WORDPRESS_DB_PASSWORD: passwordchangeme&#13;\nvolumes:&#13;\n    db_data:<\/pre>\n<p>Lets take a look at the format of this file. First, a version number, since the syntax can change <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/compose\/compose-file\/\">depending on which version you\u2019re using<\/a>.<\/p>\n<p>Next a list of Services. The first is named \u201cdb,\u201d and uses the <code>mysql:5.7<\/code>\u00a0container, set to always restart, and with environment variables to configure the database with a user and password. To maintain data across restarts, this image is configured with a Docker volume mounted to the MySQL data directory.<\/p>\n<p>The other service is \u201cwordpress,\u201d which depends on the database service, ensuring that Docker will make sure the database is started before running. It exposes port 80 as port 8000, and sets some environment variables so that it can connect to MySQL. Note that the host for the database is set to <code>db:3306<\/code>, which tells the WordPress container to connect to the \u201cdb\u201d service.<\/p>\n<p>Lastly, the volumes are defined for persistent storage. Optionally, you can <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/compose\/compose-file\/#networks\">also define custom networking<\/a> for the containers. There are plenty of extended options you can configure, so if you\u2019re looking to do something specific, you should check the documentation for <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/compose\/compose-file\/#service-configuration-reference\">Docker Compose<\/a>.<\/p>\n<p>Once configured, starting this service is easy. Simply run <code>docker-compose up<\/code>, which will pull all the required containers and start your services.<\/p>\n<pre>docker-compose up -d<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7449\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/6d46fdfee8137f78275496638feb613b\/p\/uploads\/2020\/10\/8ca29ea4.png\" alt=\"\" width=\"700\" height=\"304\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>And you should see the services running on the system with <code>docker ps<\/code>. In this case, you\u2019ll see WordPress up and running properly.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7450\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/00647186cfe70090d27fd7e8ccfd38ae\/p\/uploads\/2020\/10\/3fcd6285.png\" alt=\"\" width=\"700\" height=\"347\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Building_With_Docker_Compose\"><\/span>Building With Docker Compose<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker Compose can also be used within a Dockerfile project, and can be set up to build and run an image locally rather than pulling from the Docker Hub.<\/p>\n<p>To do so, you simply need to add a <code>build<\/code>\u00a0section on the service. You can set different context directories, as well as different Dockerfiles for different images.<\/p>\n<pre>version: \"3.8\"&#13;\nservices:&#13;\n  webapp:&#13;\n    build:&#13;\n      context: .\/dir&#13;\n      dockerfile: Dockerfile-alternate&#13;\n    image: imagename:tag<\/pre>\n<p>In this case, the <code>image:<\/code>\u00a0variable is also set, but here it is used to tag the image that is built by Docker Compose.\n<\/div>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/7446\/what-is-docker-compose-and-how-do-you-use-it\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What Is Docker Compose, and How Do You Use It? \u2013 CloudSavvy IT&#8221; Docker Compose is a tool you can use to centrally manage the deployments of many different Docker containers. It\u2019s an important tool for any application that needs multiple microservices, as it allows each service to easily be in a separately managed container&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":93378,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-93377","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/93377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=93377"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/93377\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/93378"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=93377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=93377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=93377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}