{"id":8468,"date":"2026-05-22T08:51:25","date_gmt":"2026-05-22T06:51:25","guid":{"rendered":"https:\/\/mybox.com\/help\/?post_type=manual_kb&#038;p=8468"},"modified":"2026-06-14T23:34:58","modified_gmt":"2026-06-14T21:34:58","slug":"example-docker-configuration-files-dockerfile-and-docker-compose-yml","status":"publish","type":"manual_kb","link":"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/","title":{"rendered":"Sample Docker Compose Configuration File"},"content":{"rendered":"\n<div class=\"translation-block translation-block-merged\">\n<p class=\"wp-block-paragraph\">Docker containers can be started using command-line arguments, but as applications become more complex, managing them manually becomes difficult. Docker Compose solves this problem by allowing you to define containers, networks, volumes, and environment variables in a single YAML configuration file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Docker Compose file makes application deployment easier, more consistent, and easier to maintain.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_86 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<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:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#Example_Docker_Compose_File\" >Example Docker Compose File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#Understanding_the_Configuration\" >Understanding the Configuration<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#container_name\" >container_name<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#image\" >image<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#ports\" >ports<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#environment\" >environment<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#volumes\" >volumes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#command\" >command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#restart\" >restart<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#Starting_the_Application\" >Starting the Application<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#Viewing_Running_Containers\" >Viewing Running Containers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#Example_WordPress_with_MariaDB\" >Example: WordPress with MariaDB<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/mybox.com\/help\/en\/knowledgebase\/example-docker-configuration-files-dockerfile-and-docker-compose-yml\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_Docker_Compose_File\"><\/span>Example Docker Compose File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>services:\n  app:\n    container_name: my_container\n\n    image: ubuntu:latest\n\n    ports:\n      - \"8080:80\"\n      - \"443:443\"\n\n    environment:\n      MYSQL_ROOT_PASSWORD: my_password\n      MYSQL_DATABASE: my_database\n      MYSQL_USER: my_user\n      MYSQL_PASSWORD: my_database_password\n\n    volumes:\n      - .\/data:\/app\/data\n\n    command: &#91;\"npm\", \"start\"]\n\n    restart: always\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Understanding_the_Configuration\"><\/span>Understanding the Configuration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"container_name\"><\/span>container_name<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<\/div>\n\n<div id=\"mybox-1965928126\" class=\"mybox-content mybox-entity-placement\"><div class=\"early-access-banner-inpost\">\r\n  <div class=\"banner-left-inpost\">\r\n    <div class=\"icon-box-inpost\">\r\n      <img decoding=\"async\" src=\"https:\/\/mybox.com\/help\/wp-content\/uploads\/2026\/02\/square-info-icon.svg\" alt=\"Info\">\r\n    <\/div>\r\n    <div class=\"text-box-inpost\">\r\n      <span class=\"label-inpost\"><span class=\"translation-block translation-block-banner-text\">Early access<\/span><\/span>\r\n      <h4><span class=\"translation-block translation-block-banner-text\">Still need help?<\/span><\/h4>\r\n      <p><span class=\"translation-block translation-block-banner-text\">Contact our customer service team.<\/span><\/p>\r\n    <\/div>\r\n  <\/div>\r\n\r\n  <div class=\"banner-right-inpost\">\r\n    <a href=\"https:\/\/panel.mybox.com\/helpdesk2\/v\/list\/\" class=\"banner-button-inpost\"><span class=\"translation-block translation-block-banner-text\">Message us<\/span><\/a>\r\n  <\/div>\r\n<\/div><\/div>\n\n<div class=\"translation-block translation-block-merged\"><p class=\"wp-block-paragraph\">The container name defines how the container will appear in Docker.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>container_name: my_container\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Without this option, Docker automatically generates a random name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"image\"><\/span>image<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The image specifies which Docker image should be used when creating the container.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>image: ubuntu:latest\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Docker will automatically download the image if it is not already available on the server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"ports\"><\/span>ports<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Port mappings allow external traffic to reach services running inside the container.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ports:\n  - \"8080:80\"\n  - \"443:443\"\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Port 8080 on the server forwards traffic to port 80 inside the container.<\/li>\n\n\n\n<li>Port 443 on the server forwards traffic to port 443 inside the container.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"environment\"><\/span>environment<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Environment variables provide configuration values to applications running inside the container.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>environment:\n  MYSQL_ROOT_PASSWORD: my_password\n  MYSQL_DATABASE: my_database\n  MYSQL_USER: my_user\n  MYSQL_PASSWORD: my_database_password\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">These values are commonly used by databases, web applications, and other services.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"volumes\"><\/span>volumes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Volumes allow data to persist even if the container is removed and recreated.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>volumes:\n  - .\/data:\/app\/data\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.\/data<\/code> is a directory on the host server.<\/li>\n\n\n\n<li><code>\/app\/data<\/code> is the directory inside the container.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Any changes made in either location are synchronized.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"command\"><\/span>command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The command option overrides the image&#8217;s default startup command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>command: &#91;\"npm\", \"start\"]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When the container starts, Docker executes this command.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"restart\"><\/span>restart<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The restart policy controls what happens if a container stops unexpectedly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>restart: always\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Common options include:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Policy<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>no<\/td><td>Never restart automatically<\/td><\/tr><tr><td>always<\/td><td>Always restart the container<\/td><\/tr><tr><td>unless-stopped<\/td><td>Restart unless manually stopped<\/td><\/tr><tr><td>on-failure<\/td><td>Restart only after failures<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Starting_the_Application\"><\/span>Starting the Application<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Save the configuration as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose.yml\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then start the container:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker compose up -d\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>-d<\/code> flag runs the container in the background.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Viewing_Running_Containers\"><\/span>Viewing Running Containers<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To display running containers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker ps\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To view application logs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker compose logs\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To stop the application:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker compose down\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_WordPress_with_MariaDB\"><\/span>Example: WordPress with MariaDB<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Docker Compose is often used to deploy multiple services together.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>services:\n  db:\n    image: mariadb:latest\n    environment:\n      MYSQL_ROOT_PASSWORD: strongpassword\n      MYSQL_DATABASE: wordpress\n\n  wordpress:\n    image: wordpress:latest\n    ports:\n      - \"8080:80\"\n    environment:\n      WORDPRESS_DB_HOST: db\n      WORDPRESS_DB_NAME: wordpress\n      WORDPRESS_DB_PASSWORD: strongpassword\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This configuration launches both a WordPress container and a MariaDB database container that automatically communicate with each other.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Docker Compose allows you to define container configurations in a simple YAML file instead of managing long Docker commands manually. It simplifies application deployment by centralizing configuration such as images, ports, environment variables, storage volumes, and restart policies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For modern applications that use multiple containers, Docker Compose is one of the most commonly used tools for local development, testing, and small-to-medium production environments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"template":"","format":"standard","manualknowledgebasecat":[25],"manual_kb_tag":[4353,7398,7405,7413,7414,7420,7421,7422,7423,7424],"class_list":["post-8468","manual_kb","type-manual_kb","status-publish","format-standard","hentry","manualknowledgebasecat-hosting","manual_kb_tag-docker-images","manual_kb_tag-docker-compose","manual_kb_tag-docker-container","manual_kb_tag-docker-compose-file","manual_kb_tag-docker-compose-yaml","manual_kb_tag-docker-compose-configuration","manual_kb_tag-docker-containers","manual_kb_tag-docker-volumes","manual_kb_tag-docker-networks","manual_kb_tag-environment-variables"],"_links":{"self":[{"href":"https:\/\/mybox.com\/help\/en\/wp-json\/wp\/v2\/manual_kb\/8468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mybox.com\/help\/en\/wp-json\/wp\/v2\/manual_kb"}],"about":[{"href":"https:\/\/mybox.com\/help\/en\/wp-json\/wp\/v2\/types\/manual_kb"}],"author":[{"embeddable":true,"href":"https:\/\/mybox.com\/help\/en\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":2,"href":"https:\/\/mybox.com\/help\/en\/wp-json\/wp\/v2\/manual_kb\/8468\/revisions"}],"predecessor-version":[{"id":8469,"href":"https:\/\/mybox.com\/help\/en\/wp-json\/wp\/v2\/manual_kb\/8468\/revisions\/8469"}],"wp:attachment":[{"href":"https:\/\/mybox.com\/help\/en\/wp-json\/wp\/v2\/media?parent=8468"}],"wp:term":[{"taxonomy":"manualknowledgebasecat","embeddable":true,"href":"https:\/\/mybox.com\/help\/en\/wp-json\/wp\/v2\/manualknowledgebasecat?post=8468"},{"taxonomy":"manual_kb_tag","embeddable":true,"href":"https:\/\/mybox.com\/help\/en\/wp-json\/wp\/v2\/manual_kb_tag?post=8468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}