/ html / wiki / tutorials / docker-compose / docker-compose.html
docker-compose.html
  1  <!DOCTYPE html>
  2  <html lang="de">
  3     <head>
  4        <meta charset="UTF-8" />
  5        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6        <meta http-equiv="onion-location" content="http://bopbopl6lohkl2rts3ltesjnag4hzs4jrx2h6k6etgq5xasbpqekzlqd.onion" />
  7        <title>BOP Wiki: Using docker-compose files</title>
  8        <link rel="stylesheet" href="/assets/stylesheet.css" />
  9        <link rel="icon" type="image/x-icon" href="/assets/img/favicon.png">
 10     </head>
 11     <body>
 12        <header>
 13           <!-- --------------------------------------------------------------------------------------------------------------------------------- -->
 14           <script src="/assets/js/navbar-OpenClose.js"></script>
 15           <script src="/assets/js/lightbox.js"></script>
 16            <script src="/assets/js/copyCodeButton.js"></script>
 17           <link rel="stylesheet" href="/resources/js-libraries/highlightJS/atom-one-dark.min.css">
 18           <script src="/resources/js-libraries/highlightJS/highlight.min.js"></script>
 19           <script src="/resources/js-libraries/highlightJS/highlightjs-line-numbers.min.js"></script>
 20           <script>hljs.highlightAll();</script>
 21           <script>hljs.initLineNumbersOnLoad();</script>
 22           <!-- --------------------------------------------------------------------------------------------------------------------------------- -->
 23           <div class="branding">
 24              <button class="toggle-btn-navbar" id="navbarOpenButton">☰</button>
 25              <a href="/">
 26              <img class="logo" src="/assets/img/logo.png">
 27              </a>
 28              <div class="typing-animation">BytesOfProgress</div>
 29           </div>
 30        </header>
 31        <div id="navbarContainer" class="navbar-container">
 32           <iframe class="navbar-iframe" src="/assets/navbar/navbar.html" frameBorder= "0"></iframe>
 33        </div>
 34        <main>
 35           <article class="blog-post">
 36              <header class="post-header">
 37                 <h1 class="post-title">Using docker-compose files</h1>
 38              </header>
 39           </article>
 40           <nav class="breadcrumb">
 41              <a href="/">Home</a>
 42              <span class="divider">›</span>
 43              <a href="/wiki/">Wiki</a>
 44              <span class="divider">›</span>
 45              <a href="/wiki/tutorials/tutorials.html">Tutorials</a>
 46              <span class="divider">›</span>
 47              <span class="current">Using docker-compose files</span>
 48           </nav>
 49           <section class="post-content">
 50              <p>
 51                 This method is tested on Debian 12 (Stable). It should work the same way
 52                 on all Debian derivatives. On distributions that are not using the APT
 53                 package manager, the commands for installing Docker and Docker Compose will
 54                 be different.
 55              </p>
 56              <hr>
 57              <h2>Install Docker and Docker Compose</h2>
 58              <p>Install Docker and Docker Compose:</p>
 59              <blockquote>
 60                 sudo apt install docker.io docker-compose -y
 61              </blockquote>
 62              <h2>Create Your Docker Compose File</h2>
 63              <p>Create your Compose file (using nano in this example):</p>
 64              <blockquote>
 65                 nano docker-compose.yml
 66              </blockquote>
 67              <p>
 68                 Paste the contents of your Compose file (CTRL + Shift + V).
 69                 Save with <strong>CTRL + O</strong>, press <strong>Enter</strong>, and exit nano with <strong>CTRL + X</strong>.
 70              </p>
 71              <p><strong>Note:</strong> YAML is indentation sensitive!</p>
 72              <h2>Start Your Containers</h2>
 73              <p>
 74                 Start your container(s) from the directory where your
 75                 <code>docker-compose.yml</code> is located:
 76              </p>
 77              <blockquote>
 78                 sudo docker-compose up
 79              </blockquote>
 80              <p>
 81                 This will show the logs of the container. To stop the container, press <strong>CTRL + C</strong>.
 82              </p>
 83              <p>
 84                 If you want to start the container in the background (detached mode),
 85                 use the <code>-d</code> flag:
 86              </p>
 87              <blockquote>
 88                 sudo docker-compose up -d
 89              </blockquote>
 90              <h2>Stopping and Removing Containers</h2>
 91              <p>To stop the container:</p>
 92              <blockquote>
 93                 sudo docker-compose stop
 94              </blockquote>
 95              <p>This does not remove the containers; you can restart them later.</p>
 96              <p>To stop and remove all containers, networks, and volumes:</p>
 97              <blockquote>
 98                 sudo docker-compose down
 99              </blockquote>
100              <p>To stop, remove everything, and also clean up orphan containers:</p>
101              <blockquote>
102                 sudo docker-compose down --remove-orphans
103              </blockquote>
104              <p>
105                 <strong>What's the difference?</strong>
106              </p>
107              <ul>
108                 <li><strong>stop:</strong> Temporarily pauses your containers without removing them.</li>
109                 <li><strong>down:</strong> Completely stops and cleans up your environment.</li>
110                 <li><strong>down --remove-orphans:</strong> Additionally removes orphan containers 
111                    (those created by previous <code>docker-compose up</code> commands but 
112                    no longer present in the <code>docker-compose.yml</code>).
113                 </li>
114              </ul>
115              <p><strong>Warning:</strong> Using <code>down</code> or <code>down --remove-orphans</code> will also remove application settings/data unless volumes are persisted properly.</p>
116              <h2>Example docker-compose.yml</h2>
117              <p>Here is an example docker-compose file for an Nginx container with comments explaining the basics:</p>
118              <div class="code-box">
119                 <pre><code>services:
120    nginx:
121      image: nginx:latest  # Use the latest official Nginx image from Docker Hub.
122      container_name: nginx  # Name the container "nginx".
123                
124      volumes:
125        - ./nginx/conf.d:/etc/nginx/conf.d  # Local folder mapped to Nginx’s config directory.
126        - ./nginx/html:/usr/share/nginx/html  # Local folder mapped to Nginx’s web root.
127                
128      ports:
129        - "80:80"   # Expose port 80 on host and map to port 80 in container.
130        - "8080:8080"  # Expose port 8080.
131        - "443:443"  # Expose port 443.
132                
133      restart: unless-stopped  # Restart automatically unless stopped manually.
134          </code></pre>
135              </div>
136              <h2>More Compose Examples</h2>
137              <p>
138                 You can find a collection of working docker-compose files 
139                 <a class="text-link" href="/resources/docker-compose/docker-compose.html" target="_blank">here</a>.
140              </p>
141           </section>
142           <footer class="post-footer">
143              <a href="/wiki/tutorials/tutorials.html" class="cta-button">← Back</a>
144           </footer>
145        </main>
146     </body>
147  </html>