<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prasenjeet Kumar</title>
    <description>The latest articles on DEV Community by Prasenjeet Kumar (@prasenjeetsymon).</description>
    <link>https://dev.to/prasenjeetsymon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F839432%2Fb4be0409-a36c-4283-ba8d-b270bedb75ff.jpg</url>
      <title>DEV Community: Prasenjeet Kumar</title>
      <link>https://dev.to/prasenjeetsymon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prasenjeetsymon"/>
    <language>en</language>
    <item>
      <title>Docker Swarn : From Newbie to Master ( part 2 )</title>
      <dc:creator>Prasenjeet Kumar</dc:creator>
      <pubDate>Sun, 28 May 2023 05:52:49 +0000</pubDate>
      <link>https://dev.to/prasenjeetsymon/docker-swarn-from-newbie-to-master-part-2--54pn</link>
      <guid>https://dev.to/prasenjeetsymon/docker-swarn-from-newbie-to-master-part-2--54pn</guid>
      <description>&lt;p&gt;Welcome to Part 2 of this series on Docker Swarm. If you have not read the previous article, please check out my profile to find it. In this article, we will discuss the remaining Docker Swarm commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker service update&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker service update&lt;/code&gt;&lt;/strong&gt; command is used to update a running service in a Docker swarm. It can be used to change the configuration of a service, such as the number of replicas, the image used, or the environment variables.&lt;/p&gt;

&lt;p&gt;The basic syntax of the command is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service update &lt;span class="o"&gt;[&lt;/span&gt;OPTIONS] SERVICE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are some common options that can be used with the &lt;strong&gt;&lt;code&gt;docker service update&lt;/code&gt;&lt;/strong&gt; command:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-replicas&lt;/code&gt;&lt;/strong&gt;: Change the number of replicas for the service&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-image&lt;/code&gt;&lt;/strong&gt;: Update the image used for the service&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-env-add&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;-env-rm&lt;/code&gt;&lt;/strong&gt;: Add or remove environment variables for the service&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-force&lt;/code&gt;&lt;/strong&gt;: Force an update of the service even if there are no changes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of using the &lt;strong&gt;&lt;code&gt;docker service update&lt;/code&gt;&lt;/strong&gt; command to update the number of replicas for a service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service update &lt;span class="nt"&gt;--replicas&lt;/span&gt; 3 my-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will change the number of replicas for the &lt;strong&gt;&lt;code&gt;my-service&lt;/code&gt;&lt;/strong&gt; service to 3.&lt;/p&gt;

&lt;p&gt;Here is another example that updates the image used by a service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service update &lt;span class="nt"&gt;--image&lt;/span&gt; my-image:latest my-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will update the image used by the &lt;strong&gt;&lt;code&gt;my-service&lt;/code&gt;&lt;/strong&gt; service to &lt;strong&gt;&lt;code&gt;my-image:latest&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can also use the &lt;strong&gt;&lt;code&gt;--env-add&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;--env-rm&lt;/code&gt;&lt;/strong&gt; options to add or remove environment variables for the service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service update &lt;span class="nt"&gt;--env-add&lt;/span&gt; &lt;span class="nv"&gt;MY_VAR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;my_value my-service
docker service update &lt;span class="nt"&gt;--env-rm&lt;/span&gt; MY_VAR my-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first command will add an environment variable &lt;strong&gt;&lt;code&gt;MY_VAR&lt;/code&gt;&lt;/strong&gt; with the value &lt;strong&gt;&lt;code&gt;my_value&lt;/code&gt;&lt;/strong&gt; to the &lt;strong&gt;&lt;code&gt;my-service&lt;/code&gt;&lt;/strong&gt; service. The second command will remove the &lt;strong&gt;&lt;code&gt;MY_VAR&lt;/code&gt;&lt;/strong&gt; environment variable from the service.&lt;/p&gt;

&lt;p&gt;Finally, you can use the &lt;strong&gt;&lt;code&gt;--force&lt;/code&gt;&lt;/strong&gt; option to force an update of the service even if there are no changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service update &lt;span class="nt"&gt;--force&lt;/span&gt; my-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will force an update of the &lt;strong&gt;&lt;code&gt;my-service&lt;/code&gt;&lt;/strong&gt; service even if there are no changes.&lt;/p&gt;

&lt;p&gt;💡 The &lt;strong&gt;&lt;code&gt;--force&lt;/code&gt;&lt;/strong&gt; flag is used with the &lt;strong&gt;&lt;code&gt;docker service update&lt;/code&gt;&lt;/strong&gt; command to force the immediate update of a service even if it results in service disruption. By default, Docker Swarm will try to update a service without disrupting it by gradually updating one replica at a time. However, in certain cases, it may be necessary to force an immediate update of all replicas to ensure that the changes are applied quickly. This can be useful in situations where the new changes are critical and need to be implemented as soon as possible, even if it causes temporary service disruption.&lt;/p&gt;

&lt;p&gt;It is important to use the &lt;strong&gt;&lt;code&gt;--force&lt;/code&gt;&lt;/strong&gt; flag with caution as it may cause service disruption and should be used only when necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker service scale&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker service scale&lt;/code&gt;&lt;/strong&gt; command is used to scale the number of replicas of a service in a Docker swarm.&lt;/p&gt;

&lt;p&gt;The syntax for the command is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service scale &amp;lt;service_name&amp;gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;replica_count&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;&amp;lt;service_name&amp;gt;&lt;/code&gt;&lt;/strong&gt; is the name of the service to be scaled and &lt;strong&gt;&lt;code&gt;&amp;lt;replica_count&amp;gt;&lt;/code&gt;&lt;/strong&gt; is the new number of replicas to be set.&lt;/p&gt;

&lt;p&gt;For example, if we have a service named &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; with 3 replicas and we want to scale it to 5 replicas, we can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service scale &lt;span class="nv"&gt;web&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will update the service with the new replica count. Note that this command only works for replicated services and not for global services.&lt;/p&gt;

&lt;p&gt;💡 Replicated services have a specific number of replicas defined, while global services run one replica on every node in the swarm. Replicated services are useful for running a fixed number of tasks, while global services are useful for tasks that need to run on every node in the swarm, such as logging or monitoring agents.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker service rm&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;docker service rm&lt;/code&gt;&lt;/strong&gt; is a command used to remove one or more services from a Docker swarm. It stops and removes all tasks associated with the specified services. The command takes one or more service IDs or service names as arguments.&lt;/p&gt;

&lt;p&gt;Here is an example of how to use it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service &lt;span class="nb"&gt;rm &lt;/span&gt;my-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will remove the service named &lt;strong&gt;&lt;code&gt;my-service&lt;/code&gt;&lt;/strong&gt; from the swarm.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker stack deploy&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker stack deploy&lt;/code&gt;&lt;/strong&gt; command is used to deploy a Docker Compose file to a Swarm cluster as a stack. A stack is a group of services that are deployed together as a single unit.&lt;/p&gt;

&lt;p&gt;Here is an example command that deploys a stack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack deploy &lt;span class="nt"&gt;-c&lt;/span&gt; docker-compose.yml mystack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this command, &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; is the name of the Docker Compose file, and &lt;strong&gt;&lt;code&gt;mystack&lt;/code&gt;&lt;/strong&gt; is the name of the stack.&lt;/p&gt;

&lt;p&gt;The options available with the &lt;strong&gt;&lt;code&gt;docker stack deploy&lt;/code&gt;&lt;/strong&gt; command are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;c&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;-compose-file&lt;/code&gt;&lt;/strong&gt;: Specifies the Docker Compose file to use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-prune&lt;/code&gt;&lt;/strong&gt;: Removes any services or containers that are not defined in the Compose file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-resolve-image&lt;/code&gt;&lt;/strong&gt;: Forces the cluster to resolve the image name to a specific digest, rather than using the default "latest" tag.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-orchestrator&lt;/code&gt;&lt;/strong&gt;: Specifies the orchestrator to use, either "swarm" or "kubernetes".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of deploying a stack with the &lt;strong&gt;&lt;code&gt;--prune&lt;/code&gt;&lt;/strong&gt; option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack deploy &lt;span class="nt"&gt;--prune&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; docker-compose.yml mystack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command removes any services or containers that are not defined in the Compose file.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;--resolve-image&lt;/code&gt;&lt;/strong&gt; option for the &lt;strong&gt;&lt;code&gt;docker stack deploy&lt;/code&gt;&lt;/strong&gt; command is used to resolve image digest and tag to the corresponding image ID before deploying a stack.&lt;/p&gt;

&lt;p&gt;The possible values for &lt;strong&gt;&lt;code&gt;--resolve-image&lt;/code&gt;&lt;/strong&gt; option are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;always&lt;/code&gt;&lt;/strong&gt;: This value tells Docker to always attempt to pull the image, even if a local copy exists. This can be useful if you want to make sure you're always using the latest version of an image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;changed&lt;/code&gt;&lt;/strong&gt;: This value tells Docker to only pull the image if the image's tag has changed since the last time it was pulled. This can be useful if you want to minimize network traffic and avoid unnecessary pulls.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;never&lt;/code&gt;&lt;/strong&gt;: This value tells Docker to never attempt to pull the image, and to only use a local copy if it exists. This can be useful if you know that you won't need to update the image, or if you want to avoid network traffic altogether. However, be careful when using this option, as it can result in using an outdated image if a newer one is available.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example command using the &lt;strong&gt;&lt;code&gt;--resolve-image&lt;/code&gt;&lt;/strong&gt; option with the &lt;strong&gt;&lt;code&gt;always&lt;/code&gt;&lt;/strong&gt; value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack deploy &lt;span class="nt"&gt;--compose-file&lt;/span&gt; docker-compose.yml &lt;span class="nt"&gt;--resolve-image&lt;/span&gt; always myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;docker stack ls&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker stack ls&lt;/code&gt;&lt;/strong&gt; command is used to list all the stacks that are currently deployed on the swarm. It provides a quick summary of the deployed stack, including the name, the number of services in the stack, and the status of the stack.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack &lt;span class="nb"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output a table that lists all the deployed stacks, their number of services, and their status. Here's an example output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;NAME                SERVICES            STATUS             
my_stack            3                   &lt;span class="k"&gt;*&lt;/span&gt; Running
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;code&gt;NAME&lt;/code&gt;&lt;/strong&gt; column shows the name of the stack. The &lt;strong&gt;&lt;code&gt;SERVICES&lt;/code&gt;&lt;/strong&gt; column shows the number of services in the stack. The &lt;strong&gt;&lt;code&gt;STATUS&lt;/code&gt;&lt;/strong&gt; column shows the current status of the stack. In this example, the stack named &lt;strong&gt;&lt;code&gt;my_stack&lt;/code&gt;&lt;/strong&gt; has 3 services and is currently running.&lt;/p&gt;

&lt;p&gt;This command is useful when you need to check the status of your deployed stacks and the number of services running in them.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker stack ps&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker stack ps&lt;/code&gt;&lt;/strong&gt; command is used to list the tasks or replicas of a given stack.&lt;/p&gt;

&lt;p&gt;Here is the syntax of the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack ps &lt;span class="o"&gt;[&lt;/span&gt;OPTIONS] STACK_NAME
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;strong&gt;&lt;code&gt;STACK_NAME&lt;/code&gt;&lt;/strong&gt; is the name of the stack.&lt;/p&gt;

&lt;p&gt;Here are some options that can be used with the &lt;strong&gt;&lt;code&gt;docker stack ps&lt;/code&gt;&lt;/strong&gt; command:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-filter&lt;/code&gt;&lt;/strong&gt;: It can be used to filter the tasks based on various parameters like desired state, node, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-no-trunc&lt;/code&gt;&lt;/strong&gt;: It is used to disable truncating the output of the command.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-quiet&lt;/code&gt;&lt;/strong&gt;: It is used to display only the task IDs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example command to list the tasks of a stack named &lt;strong&gt;&lt;code&gt;my_stack&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack ps my_stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of &lt;strong&gt;&lt;code&gt;docker stack ps&lt;/code&gt;&lt;/strong&gt; command shows the running processes/replicas of the services in the specified stack along with their current status. It displays a table with the following columns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ID&lt;/code&gt;&lt;/strong&gt;: The unique ID of the task.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Name&lt;/code&gt;&lt;/strong&gt;: The name of the service the task belongs to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Image&lt;/code&gt;&lt;/strong&gt;: The Docker image used to create the task.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Node&lt;/code&gt;&lt;/strong&gt;: The hostname of the node where the task is running.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Desired State&lt;/code&gt;&lt;/strong&gt;: The desired state of the task (running or shutdown).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Current State&lt;/code&gt;&lt;/strong&gt;: The current state of the task (running, completed, or shutdown).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Error&lt;/code&gt;&lt;/strong&gt;: Any error message associated with the task, if applicable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ID                  NAME                IMAGE                    NODE                DESIRED STATE       CURRENT STATE            ERROR
qn41eabryqw1        myapp_web.1         myapp:latest             swarm-node-1        Running             Running 2 minutes ago    
fb48n1mzsqxd        myapp_web.2         myapp:latest             swarm-node-2        Running             Running 2 minutes ago    
iz4f7ifg5ue5        myapp_worker.1      myapp-worker:latest      swarm-node-1        Running             Running 2 minutes ago    
7nzq44tigzrw        myapp_db.1          postgres:10.1            swarm-node-2        Running             Running 2 minutes ago
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, there are four tasks running for the &lt;code&gt;my_stack&lt;/code&gt; stack. Two replicas of the &lt;strong&gt;&lt;code&gt;myapp_web&lt;/code&gt;&lt;/strong&gt; service are running on &lt;strong&gt;&lt;code&gt;swarm-node-1&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;swarm-node-2&lt;/code&gt;&lt;/strong&gt;, one replica of the &lt;strong&gt;&lt;code&gt;myapp_worker&lt;/code&gt;&lt;/strong&gt; service is running on &lt;strong&gt;&lt;code&gt;swarm-node-1&lt;/code&gt;&lt;/strong&gt;, and one replica of the &lt;strong&gt;&lt;code&gt;myapp_db&lt;/code&gt;&lt;/strong&gt; service is running on &lt;strong&gt;&lt;code&gt;swarm-node-2&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker stack rm&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker stack rm&lt;/code&gt;&lt;/strong&gt; command is used to remove a Docker stack, which includes all the services and networks associated with that stack. Here is the syntax for the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;OPTIONS] STACK_NAME &lt;span class="o"&gt;[&lt;/span&gt;STACK_NAMES...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;strong&gt;&lt;code&gt;STACK_NAME&lt;/code&gt;&lt;/strong&gt; is the name of the stack to be removed.&lt;/p&gt;

&lt;p&gt;For example, to remove a stack named &lt;strong&gt;&lt;code&gt;my_stack&lt;/code&gt;&lt;/strong&gt;, the command would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack &lt;span class="nb"&gt;rm &lt;/span&gt;my_stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will remove all the services and networks associated with the &lt;strong&gt;&lt;code&gt;my_stack&lt;/code&gt;&lt;/strong&gt; stack.&lt;/p&gt;

&lt;p&gt;There are options available for the &lt;strong&gt;&lt;code&gt;docker stack rm&lt;/code&gt;&lt;/strong&gt; command. Here are some commonly used options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-force&lt;/code&gt;&lt;/strong&gt;: This option forces the removal of the stack even if it is currently running.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-orchestrator&lt;/code&gt;&lt;/strong&gt;: This option specifies which orchestrator to use for the removal operation. By default, the command uses the swarm mode orchestrator.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-volumes&lt;/code&gt;&lt;/strong&gt;: This option removes the volumes associated with the stack along with the stack itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example command that uses the &lt;strong&gt;&lt;code&gt;--force&lt;/code&gt;&lt;/strong&gt; option to forcefully remove a stack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;--force&lt;/span&gt; myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that this command does not remove any volumes that were created by the services in the stack. However, you can use the &lt;strong&gt;&lt;code&gt;--volumes&lt;/code&gt;&lt;/strong&gt; option to remove the volumes as well.&lt;/p&gt;

&lt;p&gt;Here is an example command to remove a stack with associated volumes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;--volumes&lt;/span&gt; mystack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will remove the &lt;strong&gt;&lt;code&gt;mystack&lt;/code&gt;&lt;/strong&gt; stack along with its associated volumes. If you do not want to remove the volumes, simply omit the &lt;strong&gt;&lt;code&gt;--volumes&lt;/code&gt;&lt;/strong&gt; option.&lt;/p&gt;

&lt;p&gt;💡 Just remember that docker is not going to remove any volume except anonymous volumes without your confirmation due to data loss risk&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker stack services&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker stack services&lt;/code&gt;&lt;/strong&gt; command is used to list the services that are running as part of a stack.&lt;/p&gt;

&lt;p&gt;Here's the basic syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack services &amp;lt;stack_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will display a table with the following columns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ID: The unique identifier of the service&lt;/li&gt;
&lt;li&gt;NAME: The name of the service&lt;/li&gt;
&lt;li&gt;MODE: Whether the service is running in &lt;strong&gt;&lt;code&gt;replicated&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;global&lt;/code&gt;&lt;/strong&gt; mode&lt;/li&gt;
&lt;li&gt;REPLICAS: The number of replicas of the service (only applicable for replicated services)&lt;/li&gt;
&lt;li&gt;IMAGE: The Docker image used by the service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example of how to use the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stack services myapp_stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will display a table of all the services running in the &lt;code&gt;myapp_stack&lt;/code&gt; stack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ID                  NAME                 MODE                REPLICAS            IMAGE                      PORTS
8h1fc2y1ffaz        myapp_stack_web      replicated          3/3                 myapp:latest               &lt;span class="k"&gt;*&lt;/span&gt;:80-&amp;gt;80/tcp
e96fa5zjdt0a        myapp_stack_redis    replicated          1/1                 redis:latest               &lt;span class="k"&gt;*&lt;/span&gt;:6379-&amp;gt;6379/tcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to scale services in Docker Swarm stack?
&lt;/h2&gt;

&lt;p&gt;To scale a service in a Docker stack, you can use the &lt;strong&gt;&lt;code&gt;docker service scale&lt;/code&gt;&lt;/strong&gt; command followed by the name of the service and the desired number of replicas. For example, to scale a service named &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; in a stack named &lt;strong&gt;&lt;code&gt;my_stack&lt;/code&gt;&lt;/strong&gt; to 5 replicas, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service scale &lt;span class="nv"&gt;my_stack_web&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create or remove replicas as necessary to ensure that there are 5 replicas of the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; service running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker swarn secret
&lt;/h2&gt;

&lt;p&gt;Swarm Secrets is a feature in Docker Swarm that allows users to securely store and manage sensitive data, such as passwords, authentication tokens, and other confidential information. It follows a client-server architecture, where the Docker Swarm manager node acts as the server and the worker nodes as clients.&lt;/p&gt;

&lt;p&gt;When a secret is created, it is encrypted and stored on the manager node. The encrypted secret is then replicated to all the worker nodes that are part of the Swarm. The workers have access to the secret only when the service that requires the secret is created or updated on the worker node.&lt;/p&gt;

&lt;p&gt;Overall, Swarm Secrets provide a secure and convenient way to manage and distribute sensitive data in a Swarm environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use swarn secret ?
&lt;/h3&gt;

&lt;p&gt;First, create a Swarm Secret using the &lt;strong&gt;&lt;code&gt;docker secret create&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"mysecretpassword"&lt;/span&gt; | docker secret create db_password -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will create a Swarm Secret named &lt;strong&gt;&lt;code&gt;db_password&lt;/code&gt;&lt;/strong&gt; with the value &lt;strong&gt;&lt;code&gt;mysecretpassword&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When you deploy the Node.js application to a Docker Swarm using the &lt;strong&gt;&lt;code&gt;docker stack deploy&lt;/code&gt;&lt;/strong&gt; command, you can specify the Swarm Secret in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3.7"&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-node-app&lt;/span&gt;
    &lt;span class="na"&gt;secrets&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;source&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db_password&lt;/span&gt;
        &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/run/secrets/db_password&lt;/span&gt;
        &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0400&lt;/span&gt;
&lt;span class="na"&gt;secrets&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;db_password&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;external&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="na"&gt;file&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./db_password.txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;strong&gt;&lt;code&gt;secrets&lt;/code&gt;&lt;/strong&gt; section is used to specify the &lt;strong&gt;&lt;code&gt;db_password&lt;/code&gt;&lt;/strong&gt; Swarm Secret. The &lt;strong&gt;&lt;code&gt;external: true&lt;/code&gt;&lt;/strong&gt; option tells Docker that this Secret already exists in the Swarm.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;secrets&lt;/code&gt;&lt;/strong&gt; section in the &lt;strong&gt;&lt;code&gt;app&lt;/code&gt;&lt;/strong&gt; service specifies that the &lt;strong&gt;&lt;code&gt;db_password&lt;/code&gt;&lt;/strong&gt; Secret should be mounted as a file at &lt;strong&gt;&lt;code&gt;/run/secrets/db_password&lt;/code&gt;&lt;/strong&gt; inside the container. The &lt;strong&gt;&lt;code&gt;mode: 0400&lt;/code&gt;&lt;/strong&gt; option sets the permissions of the file to read-only for the owner.&lt;/p&gt;

&lt;p&gt;When you deploy the stack using the &lt;strong&gt;&lt;code&gt;docker stack deploy&lt;/code&gt;&lt;/strong&gt; command, Docker Swarm will automatically create the necessary files and mount the Swarm Secret as a file in the container.&lt;/p&gt;

&lt;p&gt;Here is an example of a Node.js application that uses the Swarm Secret &lt;strong&gt;&lt;code&gt;db_password&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const fs &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'fs'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

const password &lt;span class="o"&gt;=&lt;/span&gt; fs.readFileSync&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/run/secrets/db_password'&lt;/span&gt;, &lt;span class="s1"&gt;'utf8'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Database password:'&lt;/span&gt;, password&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;strong&gt;&lt;code&gt;fs.readFileSync&lt;/code&gt;&lt;/strong&gt; method is used to read the &lt;strong&gt;&lt;code&gt;db_password&lt;/code&gt;&lt;/strong&gt; Swarm Secret file. The file is mounted in the container at the path &lt;strong&gt;&lt;code&gt;/run/secrets/db_password&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;💡 In the development env just mark the&lt;code&gt;external&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt; inside your &lt;code&gt;docker-compose.yml&lt;/code&gt; file and put your database password inside the file &lt;code&gt;db_password.txt&lt;/code&gt; located in the folder of &lt;code&gt;docker-compose.yml&lt;/code&gt; file.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Docker Swarn : From Newbie to Master ( part 1 )</title>
      <dc:creator>Prasenjeet Kumar</dc:creator>
      <pubDate>Wed, 17 May 2023 06:29:22 +0000</pubDate>
      <link>https://dev.to/prasenjeetsymon/docker-swarn-from-newbie-to-master-part-1--31ha</link>
      <guid>https://dev.to/prasenjeetsymon/docker-swarn-from-newbie-to-master-part-1--31ha</guid>
      <description>&lt;p&gt;Docker Swarm is a container orchestration tool provided by Docker that allows you to create a cluster of Docker hosts and deploy services to the cluster. It allows you to manage and scale your Docker containers and services across a cluster of hosts, providing high availability and load balancing for your applications. With Docker Swarm, you can create a highly available and scalable infrastructure that can handle a large number of containerized applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why docker swarn ?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IukwQsOR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/3cXmze4Y8igXdnkc3U/giphy.gif%3Fcid%3D7941fdc6lwd7cq9cr2xfv7yfzxlae7qsv3igz4ls7kgmw9k8%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IukwQsOR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/3cXmze4Y8igXdnkc3U/giphy.gif%3Fcid%3D7941fdc6lwd7cq9cr2xfv7yfzxlae7qsv3igz4ls7kgmw9k8%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media4.giphy.com/media/3cXmze4Y8igXdnkc3U/giphy.gif?cid=7941fdc6lwd7cq9cr2xfv7yfzxlae7qsv3igz4ls7kgmw9k8&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Docker Swarm provides several benefits that make it useful in certain scenarios. Here are a few reasons why someone might choose to use Docker Swarm:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;High availability&lt;/strong&gt;: Docker Swarm is designed to provide high availability of applications by automatically distributing containers across multiple nodes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Docker Swarm allows you to easily scale your application by adding or removing nodes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Load balancing&lt;/strong&gt;: Docker Swarm provides built-in load balancing to distribute traffic across multiple containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt;: Docker Swarm provides security features such as mutual TLS authentication and encryption for network communication.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ease of use&lt;/strong&gt;: Docker Swarm is easy to set up and use, especially if you are already familiar with Docker.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Overall, Docker Swarm provides an efficient and convenient way to manage and scale containerized applications in a distributed environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture of Docker Swarm
&lt;/h2&gt;

&lt;p&gt;Docker Swarm follows a distributed and decentralized architecture with a manager-worker model. The basic components of the Swarm architecture are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Manager Nodes&lt;/strong&gt;: The manager nodes are responsible for managing the cluster state, scheduling tasks, and maintaining the desired state of the services. There can be multiple manager nodes in a Swarm cluster, but only one manager node is the leader at a time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Worker Nodes&lt;/strong&gt;: The worker nodes are responsible for running the tasks and services assigned to them by the manager nodes. There can be multiple worker nodes in a Swarm cluster.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Service&lt;/strong&gt;: A service is a group of containers that perform the same task or function. The service is defined by a Docker Compose file or Docker service create command.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Task&lt;/strong&gt;: A task is a container running on a worker node, which is part of a service. Each task runs a specific instance of a container image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overlay Network&lt;/strong&gt;: An overlay network is a virtual network that connects the containers across different nodes in the Swarm cluster. This network allows the containers to communicate with each other seamlessly, even if they are running on different hosts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Swarm Secret&lt;/strong&gt;: A Swarm secret is a way to securely store sensitive data such as passwords, certificates, or API keys. The Swarm secrets are encrypted and only accessible by authorized containers in the Swarm cluster.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Swarm manager nodes handle the orchestration of tasks and services and make sure that the desired state of the services is maintained. The worker nodes execute the tasks and services assigned to them by the manager nodes. The overlay network connects the containers across the Swarm cluster, and Swarm secrets provide a secure way to store sensitive data.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create Swarn cluster ?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kqCFyq1R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/1zjfPChMwq6u6NQkSY/giphy.gif%3Fcid%3D7941fdc6vhqocxf8x7dmmp4fyfyukxd770qv2e1htmj2644s%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kqCFyq1R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/1zjfPChMwq6u6NQkSY/giphy.gif%3Fcid%3D7941fdc6vhqocxf8x7dmmp4fyfyukxd770qv2e1htmj2644s%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media4.giphy.com/media/1zjfPChMwq6u6NQkSY/giphy.gif?cid=7941fdc6vhqocxf8x7dmmp4fyfyukxd770qv2e1htmj2644s&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To create a Docker Swarm, you need to initialize a Swarm on one or more nodes. You can do this by running the &lt;strong&gt;&lt;code&gt;docker swarm init&lt;/code&gt;&lt;/strong&gt; command on a manager node. Here are the steps to create a Docker Swarm:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose one or more nodes to act as manager nodes. These nodes will be responsible for managing the Swarm cluster.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;On one of the manager nodes, run the following command to initialize the Swarm:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker swarm init &lt;span class="nt"&gt;--advertise-addr&lt;/span&gt; &amp;lt;MANAGER-IP-ADDRESS&amp;gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Replace &lt;strong&gt;&lt;code&gt;&amp;lt;MANAGER-IP-ADDRESS&amp;gt;&lt;/code&gt;&lt;/strong&gt; with the IP address of the manager node.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the Swarm is initialized, the command will output a &lt;strong&gt;&lt;code&gt;docker swarm join&lt;/code&gt;&lt;/strong&gt; command that you can use to add worker nodes to the Swarm. Copy this command and run it on each worker node to join them to the Swarm.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once all nodes are part of the Swarm, you can start deploying services and managing them using Docker Swarm commands.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;💡 Note: In a production environment, you should use secure communication channels and encryption for communication between the Swarm nodes. You can use TLS certificates for this purpose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some common Docker Swarm commands
&lt;/h2&gt;

&lt;p&gt;Here are some of the commonly used Docker Swarm commands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker swarm init&lt;/code&gt;&lt;/strong&gt;: Initializes a new Swarm.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker swarm join&lt;/code&gt;&lt;/strong&gt;: Join an existing Swarm as a node.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker node ls&lt;/code&gt;&lt;/strong&gt;: List all the nodes in the Swarm.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker service create&lt;/code&gt;&lt;/strong&gt;: Create a new service.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker service ls&lt;/code&gt;&lt;/strong&gt;: List all the services running in the Swarm.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker service ps&lt;/code&gt;&lt;/strong&gt;: List all the tasks (replicas) of a service.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker service update&lt;/code&gt;&lt;/strong&gt;: Update an existing service.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker service scale&lt;/code&gt;&lt;/strong&gt;: Scale a service up or down.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker service rm&lt;/code&gt;&lt;/strong&gt;: Remove a service.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker stack deploy&lt;/code&gt;&lt;/strong&gt;: Deploy a new stack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker stack ls&lt;/code&gt;&lt;/strong&gt;: List all the stacks running in the Swarm.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker stack ps&lt;/code&gt;&lt;/strong&gt;: List all the tasks (replicas) of a stack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker stack rm&lt;/code&gt;&lt;/strong&gt;: Remove a stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;**docker stack services**&lt;/code&gt;: List all the services of given stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 In this article, I will only discuss five Swarm commands. The remaining commands will be discussed in upcoming articles. I have invested a lot of time in this Docker Swarm series. If you find this article helpful, please consider liking and sharing it with others, and saving it for future reference.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;docker swarm init&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PRCxQJlh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/v9qXDRYrzhLmIPvtBs/giphy.gif%3Fcid%3D7941fdc6vhqocxf8x7dmmp4fyfyukxd770qv2e1htmj2644s%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PRCxQJlh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/v9qXDRYrzhLmIPvtBs/giphy.gif%3Fcid%3D7941fdc6vhqocxf8x7dmmp4fyfyukxd770qv2e1htmj2644s%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media4.giphy.com/media/v9qXDRYrzhLmIPvtBs/giphy.gif?cid=7941fdc6vhqocxf8x7dmmp4fyfyukxd770qv2e1htmj2644s&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker swarm init&lt;/code&gt;&lt;/strong&gt; command initializes a new Docker Swarm. This command should be executed on the node that you want to become the Swarm Manager. Once the Swarm Manager is initialized, you can add other nodes as Swarm Workers to the Swarm.&lt;/p&gt;

&lt;p&gt;Here's an example command to initialize a new Docker Swarm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker swarm init &lt;span class="nt"&gt;--advertise-addr&lt;/span&gt; &amp;lt;MANAGER-IP-ADDRESS&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;&amp;lt;MANAGER-IP-ADDRESS&amp;gt;&lt;/code&gt;&lt;/strong&gt; should be replaced with the IP address of the node that will act as the Swarm Manager. The &lt;strong&gt;&lt;code&gt;--advertise-addr&lt;/code&gt;&lt;/strong&gt; flag specifies the address that other nodes should use to communicate with the Swarm Manager.&lt;/p&gt;

&lt;p&gt;After running the above command, you should see output similar to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Swarm initialized: current node &amp;lt;NODE-ID&amp;gt; is now a manager.

To add a worker to this swarm, run the following &lt;span class="nb"&gt;command&lt;/span&gt;:

    docker swarm &lt;span class="nb"&gt;join&lt;/span&gt; &lt;span class="nt"&gt;--token&lt;/span&gt; &amp;lt;TOKEN&amp;gt; &amp;lt;MANAGER-IP-ADDRESS&amp;gt;:&amp;lt;MANAGER-PORT&amp;gt;

To add a manager to this swarm, run &lt;span class="s1"&gt;'docker swarm join-token manager'&lt;/span&gt; and follow the instructions.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This output indicates that the Swarm Manager has been initialized successfully. It also provides instructions on how to add other nodes as Workers or Managers to the Swarm.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker swarm init&lt;/code&gt;&lt;/strong&gt; command generates a unique token that is used to authenticate nodes when they join the Swarm. The token is displayed in the output of the &lt;strong&gt;&lt;code&gt;docker swarm init&lt;/code&gt;&lt;/strong&gt; command and is required when joining new nodes to the Swarm.&lt;/p&gt;

&lt;h3&gt;
  
  
  Here's how to reprint the join command for both worker and manager nodes.
&lt;/h3&gt;

&lt;p&gt;To reprint the join command for a Docker Swarm node, you can use the following command on the manager node:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker swarm join-token worker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print the join command for a worker node. If you want to add a manager node, you can run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker swarm join-token manager
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print the join command for a manager node. You can copy and paste this command to the node you want to join the Swarm.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker swarm join&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nrv9Tj24--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/ckriP6xNplIXUA2CBU/giphy.gif%3Fcid%3D7941fdc6mfek1aucnu15ywafffy5hlqkzub7ez0k6lmm1rxm%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nrv9Tj24--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/ckriP6xNplIXUA2CBU/giphy.gif%3Fcid%3D7941fdc6mfek1aucnu15ywafffy5hlqkzub7ez0k6lmm1rxm%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media0.giphy.com/media/ckriP6xNplIXUA2CBU/giphy.gif?cid=7941fdc6mfek1aucnu15ywafffy5hlqkzub7ez0k6lmm1rxm&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;docker swarm join&lt;/code&gt;&lt;/strong&gt; is a command used to add a worker node or a manager node to an existing swarm. The command requires a token generated by a &lt;strong&gt;&lt;code&gt;docker swarm init&lt;/code&gt;&lt;/strong&gt; command from an existing manager node. The syntax of the command is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker swarm &lt;span class="nb"&gt;join&lt;/span&gt; &lt;span class="nt"&gt;--token&lt;/span&gt; &amp;lt;token&amp;gt; &amp;lt;ip-address&amp;gt;:&amp;lt;port&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;&amp;lt;token&amp;gt;&lt;/code&gt;&lt;/strong&gt; is the token generated by &lt;strong&gt;&lt;code&gt;docker swarm init&lt;/code&gt;&lt;/strong&gt; command and &lt;strong&gt;&lt;code&gt;&amp;lt;ip-address&amp;gt;&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;&amp;lt;port&amp;gt;&lt;/code&gt;&lt;/strong&gt; is the IP address and port of the existing manager node.&lt;/p&gt;

&lt;p&gt;Once the command is executed successfully, the node will join the swarm and can be listed using the &lt;strong&gt;&lt;code&gt;docker node ls&lt;/code&gt;&lt;/strong&gt; command.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker node ls&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_QQnpgH5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/YLHwkqayc1j7a/giphy.gif%3Fcid%3D7941fdc6a9g5gnbduk9s3b2kv2bptpi9du5w7lk53xxnuvt5%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_QQnpgH5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/YLHwkqayc1j7a/giphy.gif%3Fcid%3D7941fdc6a9g5gnbduk9s3b2kv2bptpi9du5w7lk53xxnuvt5%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media1.giphy.com/media/YLHwkqayc1j7a/giphy.gif?cid=7941fdc6a9g5gnbduk9s3b2kv2bptpi9du5w7lk53xxnuvt5&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="302" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker node ls&lt;/code&gt;&lt;/strong&gt; command is used to list all the nodes in the Docker Swarm. It provides information about the nodes such as their ID, hostname, status, availability, and number of running and stopped containers.&lt;/p&gt;

&lt;p&gt;To use the &lt;strong&gt;&lt;code&gt;docker node ls&lt;/code&gt;&lt;/strong&gt; command, you need to have a Docker Swarm initialized and have at least one node joined to the Swarm&lt;/p&gt;

&lt;p&gt;Once you have one or more nodes joined to the Swarm, you can use the &lt;strong&gt;&lt;code&gt;docker node ls&lt;/code&gt;&lt;/strong&gt; command to list all the nodes in the Swarm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker node &lt;span class="nb"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the command includes information such as the hostname, the status of the node, the availability, the type of the node, and the number of running containers on the node. Here's an example output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
a95syht99niz6h15xy5gry0mp &lt;span class="k"&gt;*&lt;/span&gt;   node-1              Ready               Active              Leader              20.10.5
9mijxl5b5nb3d2a3qzsdtnyho     node-2              Ready               Active                                  20.10.5
0xarz8pya5z5vmpgolcqd00tx     node-3              Ready               Active                                  20.10.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, there are three nodes in the Docker Swarm cluster, with &lt;strong&gt;&lt;code&gt;node-1&lt;/code&gt;&lt;/strong&gt; being the leader. All nodes are ready and active, and are running Docker Engine version 20.10.5.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker service create&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--txzATn_O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media3.giphy.com/media/VF7Cfau1vDMJ6cDwy1/giphy.gif%3Fcid%3D7941fdc6vhqocxf8x7dmmp4fyfyukxd770qv2e1htmj2644s%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--txzATn_O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media3.giphy.com/media/VF7Cfau1vDMJ6cDwy1/giphy.gif%3Fcid%3D7941fdc6vhqocxf8x7dmmp4fyfyukxd770qv2e1htmj2644s%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media3.giphy.com/media/VF7Cfau1vDMJ6cDwy1/giphy.gif?cid=7941fdc6vhqocxf8x7dmmp4fyfyukxd770qv2e1htmj2644s&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="533" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker service create&lt;/code&gt;&lt;/strong&gt; command is used to create a new service in a Docker Swarm. A service is a group of containers that are all running the same image and configuration, working together to provide a single application. The &lt;strong&gt;&lt;code&gt;docker service create&lt;/code&gt;&lt;/strong&gt; command allows you to specify the image, number of replicas, and other configuration options for the service.&lt;/p&gt;

&lt;p&gt;Here is the basic syntax for the &lt;strong&gt;&lt;code&gt;docker service create&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service create &lt;span class="o"&gt;[&lt;/span&gt;OPTIONS] IMAGE &lt;span class="o"&gt;[&lt;/span&gt;COMMAND] &lt;span class="o"&gt;[&lt;/span&gt;ARG...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are some of the most commonly used options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-name&lt;/code&gt;&lt;/strong&gt;: Sets the name of the service&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-replicas&lt;/code&gt;&lt;/strong&gt;: Specifies the number of replicas of the service to create&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-publish&lt;/code&gt;&lt;/strong&gt;: Publishes a container's port to the host&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-mount&lt;/code&gt;&lt;/strong&gt;: Mounts a volume to the service&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-env&lt;/code&gt;&lt;/strong&gt;: Sets environment variables for the service&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-network&lt;/code&gt;&lt;/strong&gt;: Specifies the network to use for the service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;**-label**&lt;/code&gt; :  Sets the label of the service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example command that creates a new service called &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service create &lt;span class="nt"&gt;--name&lt;/span&gt; web &lt;span class="nt"&gt;--replicas&lt;/span&gt; 3 &lt;span class="nt"&gt;--label&lt;/span&gt; &lt;span class="nv"&gt;mylabel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;myvalue &lt;span class="nt"&gt;--publish&lt;/span&gt; &lt;span class="nv"&gt;published&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;8080,target&lt;span class="o"&gt;=&lt;/span&gt;80 nginx:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command creates a new service called &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; that runs the &lt;strong&gt;&lt;code&gt;nginx:latest&lt;/code&gt;&lt;/strong&gt; image with 3 replicas, publishes port 80 in the container to port 8080 on the host, and uses the default network for the service with label &lt;strong&gt;&lt;code&gt;mylabel&lt;/code&gt;&lt;/strong&gt; as the label key, and &lt;strong&gt;&lt;code&gt;myvalue&lt;/code&gt;&lt;/strong&gt; as the label value.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to add env variables from the file ?
&lt;/h3&gt;

&lt;p&gt;To add environment variables from a file while creating a Docker service, you can use the &lt;strong&gt;&lt;code&gt;--env-file&lt;/code&gt;&lt;/strong&gt; option with the &lt;strong&gt;&lt;code&gt;docker service create&lt;/code&gt;&lt;/strong&gt; command.&lt;/p&gt;

&lt;p&gt;Here's an example command that creates a service and sets environment variables from a file named &lt;strong&gt;&lt;code&gt;myenv.list&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service create &lt;span class="nt"&gt;--name&lt;/span&gt; myservice &lt;span class="nt"&gt;--env-file&lt;/span&gt; myenv.list myimage:tag
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;code&gt;myenv.list&lt;/code&gt;&lt;/strong&gt; file should contain one environment variable per line, in the format &lt;strong&gt;&lt;code&gt;VARNAME=value&lt;/code&gt;&lt;/strong&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;DB_HOST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;db.example.com
&lt;span class="nv"&gt;DB_PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5432
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These environment variables will be passed to the container when it is started as part of the service.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker service ls&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g7Visuyg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/SRkVigbErPKC5KW2VG/giphy.gif%3Fcid%3D7941fdc6a9g5gnbduk9s3b2kv2bptpi9du5w7lk53xxnuvt5%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g7Visuyg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/SRkVigbErPKC5KW2VG/giphy.gif%3Fcid%3D7941fdc6a9g5gnbduk9s3b2kv2bptpi9du5w7lk53xxnuvt5%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media4.giphy.com/media/SRkVigbErPKC5KW2VG/giphy.gif?cid=7941fdc6a9g5gnbduk9s3b2kv2bptpi9du5w7lk53xxnuvt5&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker service ls&lt;/code&gt;&lt;/strong&gt; command is used to list all the services running in the swarm. It provides a tabular view of the services, including the service ID, name, mode, replicas, image, and ports.&lt;/p&gt;

&lt;p&gt;Here's an example output of the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ID             NAME          MODE         REPLICAS   IMAGE                 PORTS
bwv5f5m5wmlz   web           replicated   3/3        nginx:latest          &lt;span class="k"&gt;*&lt;/span&gt;:80-&amp;gt;80/tcp
ui01bmy5ue5m   api           replicated   2/2        myapp/api:latest      &lt;span class="k"&gt;*&lt;/span&gt;:8080-&amp;gt;8080/tcp
3bxq77hqlfjf   db            replicated   1/1        mysql:latest          &lt;span class="k"&gt;*&lt;/span&gt;:3306-&amp;gt;3306/tcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This output shows three services running in the swarm: &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;api&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;db&lt;/code&gt;&lt;/strong&gt;. The &lt;strong&gt;&lt;code&gt;REPLICAS&lt;/code&gt;&lt;/strong&gt; column shows the number of replicas for each service, and the &lt;strong&gt;&lt;code&gt;PORTS&lt;/code&gt;&lt;/strong&gt; column shows the published ports for each service. &lt;/p&gt;

&lt;p&gt;To check the current number of replicas for a service, you can look at the &lt;strong&gt;&lt;code&gt;REPLICAS&lt;/code&gt;&lt;/strong&gt; column. In the example above, the &lt;code&gt;web&lt;/code&gt; service has been configured to run 3 replicas, and all 3 are currently running and also*&lt;em&gt;&lt;code&gt;PORTS&lt;/code&gt;&lt;/em&gt;* column shows that the container's port 80 is being published on the host's port 80.&lt;/p&gt;

&lt;p&gt;There are several options available for the &lt;strong&gt;&lt;code&gt;docker service ls&lt;/code&gt;&lt;/strong&gt; command. Here are some commonly used ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;-filter&lt;/code&gt;&lt;/strong&gt;: Allows you to filter the services based on various criteria such as service name, label, mode, etc.&lt;/p&gt;

&lt;p&gt;Suppose we want to filter the list of services by the label &lt;strong&gt;&lt;code&gt;com.example.description=web service&lt;/code&gt;&lt;/strong&gt;. We can use the &lt;strong&gt;&lt;code&gt;--filter&lt;/code&gt;&lt;/strong&gt; option as follows:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker service &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;--filter&lt;/span&gt; &lt;span class="nv"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;com.example.description&lt;span class="o"&gt;=&lt;/span&gt;web service
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This command will list all the services that have the label &lt;strong&gt;&lt;code&gt;com.example.description&lt;/code&gt;&lt;/strong&gt; set to &lt;strong&gt;&lt;code&gt;web service&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;-format&lt;/code&gt;&lt;/strong&gt;: Specifies the format of the output. You can choose from several predefined formats or create your own custom format.&lt;/p&gt;

&lt;p&gt;Suppose we want to display only the service name and the number of replicas, and we want to use a custom output format. We can use the &lt;strong&gt;&lt;code&gt;--format&lt;/code&gt;&lt;/strong&gt; option as follows:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker service &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;--format&lt;/span&gt; &lt;span class="s2"&gt;"table {{.Name}}&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;{{.Replicas}}"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This command will list all the services and display the service name and the number of replicas in a table format. The output will look something like this:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;NAME        REPLICAS
web         3/3
api         2/2
database    1/1
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;In the &lt;strong&gt;&lt;code&gt;--format&lt;/code&gt;&lt;/strong&gt; option, we use the &lt;strong&gt;&lt;code&gt;table&lt;/code&gt;&lt;/strong&gt; format and specify the columns we want to display using the Go template syntax (&lt;strong&gt;&lt;code&gt;{{.Name}}&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;{{.Replicas}}&lt;/code&gt;&lt;/strong&gt;). The &lt;strong&gt;&lt;code&gt;\t&lt;/code&gt;&lt;/strong&gt; character is used to separate the columns with a tab.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;-quiet&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;q&lt;/code&gt;&lt;/strong&gt;: Only displays the IDs of the services.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;-no-trunc&lt;/code&gt;&lt;/strong&gt;: Displays the full service ID instead of truncating it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker service ps&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IXcEit4V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/EqHkOQNczxpPRYNq6F/giphy.gif%3Fcid%3D7941fdc6cxeq087hyziizq06hmek7d548zcg6v01h5ya6cr5%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IXcEit4V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/EqHkOQNczxpPRYNq6F/giphy.gif%3Fcid%3D7941fdc6cxeq087hyziizq06hmek7d548zcg6v01h5ya6cr5%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media0.giphy.com/media/EqHkOQNczxpPRYNq6F/giphy.gif?cid=7941fdc6cxeq087hyziizq06hmek7d548zcg6v01h5ya6cr5&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker service ps&lt;/code&gt;&lt;/strong&gt; command shows the running tasks for a given service. Each task represents a container running a service on a swarm node.&lt;/p&gt;

&lt;p&gt;Here's how to use the &lt;strong&gt;&lt;code&gt;docker service ps&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service ps &amp;lt;service_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, if we have a service named &lt;strong&gt;&lt;code&gt;webapp&lt;/code&gt;&lt;/strong&gt;, we can list its tasks using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service ps webapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will show information about the tasks running for the service, including the container ID, the node it's running on, the service replica number, and its current status.&lt;/p&gt;

&lt;p&gt;Here's an example output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
e9y2pr5z6nca        webapp.1            nginx:latest        node1               Running             Running 5 minutes ago                       
6f9cgm91swxc        webapp.2            nginx:latest        node2               Running             Running 5 minutes ago                       
3f6ap9q6ghor        webapp.3            nginx:latest        node3               Running             Running 5 minutes ago
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also use the &lt;strong&gt;&lt;code&gt;--format&lt;/code&gt;&lt;/strong&gt; option to display specific information about each task in a custom format. For example, to display the task ID and the node it's running on, we can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service ps &lt;span class="nt"&gt;--format&lt;/span&gt; &lt;span class="s2"&gt;"Task ID: {{.ID}} Node: {{.Node}} "&lt;/span&gt; webapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will show the task ID and the node it's running on for each task in the &lt;strong&gt;&lt;code&gt;webapp&lt;/code&gt;&lt;/strong&gt; service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Task ID: e9y2pr5z6nca Node: node1 
Task ID: 6f9cgm91swxc Node: node2 
Task ID: 3f6ap9q6ghor Node: node3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also use the &lt;strong&gt;&lt;code&gt;--filter&lt;/code&gt;&lt;/strong&gt; option to display only the tasks that match certain criteria. For example, to display only the tasks that are currently running, we can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service ps &lt;span class="nt"&gt;--filter&lt;/span&gt; &lt;span class="s2"&gt;"desired-state=running"&lt;/span&gt; webapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will show only the tasks that are currently running for the &lt;strong&gt;&lt;code&gt;webapp&lt;/code&gt;&lt;/strong&gt; service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
e9y2pr5z6nca        webapp.1            nginx:latest        node1               Running             Running 5 minutes ago                        
6f9cgm91swxc        webapp.2            nginx:latest        node2               Running             Running 5 minutes ago                        
3f6ap9q6ghor        webapp.3            nginx:latest        node3               Running             Running 5 minutes ago
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are many values that can be used with the &lt;strong&gt;&lt;code&gt;--filter&lt;/code&gt;&lt;/strong&gt; flag in the &lt;strong&gt;&lt;code&gt;docker service ps&lt;/code&gt;&lt;/strong&gt; command to filter the output. Here are some common ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;id&lt;/code&gt; : The &lt;code&gt;id&lt;/code&gt; filter matches on all or a prefix of a task’s ID.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service ps &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"id=8"&lt;/span&gt; redis
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Output of this command will look something like this  :&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
ID             NAME      IMAGE        NODE      DESIRED STATE  CURRENT STATE      ERROR  PORTS
8ryt076polmc   redis.4   redis:3.0.6  worker1   Running        Running 9 seconds
8eaxrb2fqpbn   redis.10  redis:3.0.6  manager1  Running        Running 8 seconds
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;name : The &lt;code&gt;name&lt;/code&gt; filter matches on task names&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service ps &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"name=redis.1"&lt;/span&gt; redis
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Output of this command will look something like this  :&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
ID            NAME     IMAGE        NODE      DESIRED STATE  CURRENT STATE      ERROR  PORTS
qihejybwf1x5  redis.1  redis:3.0.6  manager1  Running        Running 8 seconds
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;node : The &lt;code&gt;node&lt;/code&gt; filter matches on a node name or a node ID.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker service ps &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"node=manager1"&lt;/span&gt; redis
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Output of this command will look something like this :&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
ID            NAME      IMAGE        NODE      DESIRED STATE  CURRENT STATE      ERROR  PORTS
0qihejybwf1x  redis.1   redis:3.0.6  manager1  Running        Running 8 seconds
1x0v8yomsncd  redis.5   redis:3.0.6  manager1  Running        Running 8 seconds
3w1wu13yupln  redis.9   redis:3.0.6  manager1  Running        Running 8 seconds
8eaxrb2fqpbn  redis.10  redis:3.0.6  manager1  Running        Running 8 seconds
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;desired-state : The &lt;code&gt;desired-state&lt;/code&gt; filter can take the values &lt;code&gt;running&lt;/code&gt;, &lt;code&gt;shutdown&lt;/code&gt;, or &lt;code&gt;accepted&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it for this article. If you found it useful, please share it with your friends and save it for future reference. In the next article, we will discuss remaining command. See you in the next one!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>docker</category>
      <category>kubernetes</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Building TabNode: A Social Media App for Tech Bloggers with Appwrite</title>
      <dc:creator>Prasenjeet Kumar</dc:creator>
      <pubDate>Sun, 14 May 2023 05:51:57 +0000</pubDate>
      <link>https://dev.to/prasenjeetsymon/building-tabnode-a-social-media-app-for-tech-bloggers-with-appwrite-45mj</link>
      <guid>https://dev.to/prasenjeetsymon/building-tabnode-a-social-media-app-for-tech-bloggers-with-appwrite-45mj</guid>
      <description>&lt;p&gt;I am participating in the upcoming Appwrite hackathon, and I am determined to win it no matter what. The hackathon will run from May 15, 2023 to June 14, 2023.&lt;/p&gt;

&lt;h2&gt;
  
  
  "What is Appwrite?”
&lt;/h2&gt;

&lt;p&gt;Appwrite is a backend-as-a-service that helps developers launch applications faster by providing all the necessary services, such as databases, authentication, storage, and cloud functions. You can self-host it, but now they've launched Appwrite Cloud, so there's no need to self-host. You can simply purchase a cloud subscription and start using it, which is awesome 😃.&lt;/p&gt;

&lt;h2&gt;
  
  
  Have you used Appwrite before?
&lt;/h2&gt;

&lt;p&gt;No, I have never coded anything using Appwrite. However, after seeing the hackathon tweet, I am super excited about it. I plan to learn everything about Appwrite by reading its documentation and watching free YouTube videos in just 2 to 3 days. You can count on me. I am passionate about this stuff 😐&lt;/p&gt;

&lt;h2&gt;
  
  
  Have you worked on similar products?
&lt;/h2&gt;

&lt;p&gt;Yes! I am a big fan of Firebase and have built several applications using it in the past. You can find them on my GitHub profile, but note that some of the projects may be private 🤣. I am confident that I can learn Appwrite quickly because I already have experience with similar cloud-based technologies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do you want to participate?
&lt;/h2&gt;

&lt;p&gt;To make a long story short, I used to be very introverted, and to some extent, I still am. I made my computer my best friend and spent much of my time alone in my dark room with it. During this period, I only communicated with my very close school friend and my family. I didn't like making new friends because I didn't want casual friendships. I wanted friends who could trust me, help me become a better person, and stay with me lifelong. You might say I was afraid of making new friends due to past trauma.&lt;/p&gt;

&lt;p&gt;But now I know that I can't do everything alone, especially if my dream is to start my own company. I must go out into the world, make friends, communicate, and seek help from the community and friends. That's why I decided, for the first time, to participate in this hackathon. By doing so, I can involve the Appwrite and Hashnode communities, make new friends, and hopefully land my dream job at one of the companies listed in my Notion's top 10 list. And guess what? Appwrite and Hashnode are both on that list.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where does Appwrite rank on your list of dream companies?
&lt;/h2&gt;

&lt;p&gt;I am unable to disclose it at the moment, but I plan to release it after winning this hackathon. I am confident that I can win, and I will share the list with you all afterward.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do you have experience building large-scale applications?
&lt;/h2&gt;

&lt;p&gt;Yes, I live for this kind of work. I promise to make my repository public after winning this hackathon because I love open-source. I want everyone to be able to contribute to my large-scale applications and learn from them. It would be a dream come true if you would open issues and submit pull requests to my project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do you have experience in UI/UX?
&lt;/h2&gt;

&lt;p&gt;Hey, just so you know, I'm really passionate about this kind of work. I have the skills to build a product from scratch and take it all the way to production-grade level, ready to launch to the market. So you can definitely count on me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can you tell us your age and gender?
&lt;/h2&gt;

&lt;p&gt;Yes, I am a 23-year-old male who turns ideas into reality 😎&lt;/p&gt;

&lt;h2&gt;
  
  
  What are you building?
&lt;/h2&gt;

&lt;p&gt;I plan to develop a social media application for tech bloggers that will include the following features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users can register with GitHub and Twitter only.&lt;/li&gt;
&lt;li&gt;Users can set up an initial profile and choose topics (tags) that interest them.&lt;/li&gt;
&lt;li&gt;Users can create posts using Markdown. Initially, each user is limited to two posts to reduce costs.&lt;/li&gt;
&lt;li&gt;The search feature allows any user to easily search for other users.&lt;/li&gt;
&lt;li&gt;Our AI system will automatically detect up to four tags for each blog post.&lt;/li&gt;
&lt;li&gt;Users can follow each other, and we will use a recommendation engine to suggest other users to follow.&lt;/li&gt;
&lt;li&gt;Users can see a personalized feed.&lt;/li&gt;
&lt;li&gt;Users can comment, upvote, downvote, save, and share blog posts.&lt;/li&gt;
&lt;li&gt;Users can subscribe to bloggers to see their exclusive private blog posts, and they can also purchase individual blog posts.&lt;/li&gt;
&lt;li&gt;The feed page will include a blog story section that shows all the stories of the bloggers the user is following. This section will include an auto-generated summary and a call-to-action to read more. Bloggers can configure the theme of this section from their account.&lt;/li&gt;
&lt;li&gt;Each story will have a TTL of 12 hours and will be sorted according to the published date.&lt;/li&gt;
&lt;li&gt;When bloggers post new blogs, they will have the option to post them as stories that will go to all their followers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Have you decided on the name of this app?
&lt;/h2&gt;

&lt;p&gt;Yes! I am going to call it TabNode, inspired by HashNode.&lt;/p&gt;

&lt;h2&gt;
  
  
  What tech stack are you going to use?
&lt;/h2&gt;

&lt;p&gt;I am planning to use the following technologies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Appwrite, obviously, for the backend part.&lt;/li&gt;
&lt;li&gt;We will use Figma to design and prototype.&lt;/li&gt;
&lt;li&gt;React or Angular in the frontend side, and Netlify for hosting.&lt;/li&gt;
&lt;li&gt;In-app notification system (NOVU).&lt;/li&gt;
&lt;li&gt;GPT-3.5-Turbo or Huggingface for AI Part.&lt;/li&gt;
&lt;li&gt;Stripe as the payment gateway.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Do you need team members?
&lt;/h2&gt;

&lt;p&gt;Yes, of course. Working with a team is something I crave. I have spent a lifetime building things alone and now I want to gain experience working collaboratively with team members and future friends.&lt;/p&gt;

&lt;p&gt;I am looking to form a team of two and am searching for my next team member. Anyone who thinks they can contribute to this project, is willing to work day and night, and can learn the technology on the go is welcome.&lt;/p&gt;

&lt;p&gt;If you have already built projects using the stacks described above, that would be a plus. Please feel free to contact me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where can we contact you?
&lt;/h2&gt;

&lt;p&gt;You can contact me on my Twitter and GitHub.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Twitter: &lt;a href="https://twitter.com/Prasenjeetsymon"&gt;@Prasenjeetsymon&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/prasenjeet-symon"&gt;prasenjeet-symon&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Will you split the prize?
&lt;/h2&gt;

&lt;p&gt;If we win, I will split the prize equally among the team members. I believe that every team member should be credited equally, regardless of the amount or quality of work they contributed. Once someone becomes a team member, they are an equal contributor to the project and should receive equal credit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should we join you?
&lt;/h2&gt;

&lt;p&gt;I believe I have the capability and skillset to successfully complete this project. By joining me as a team member, our chances of winning will increase, as it will speed up project development. However, I am not certain how much time it will take to complete the project or what unseen challenges we may face.&lt;/p&gt;

&lt;p&gt;Joining me on this project may provide us with increased visibility among the Appwrite and Hashnode communities, which could benefit us both. Are you ready to embark on this adventure with me? Let's do it! 💪&lt;/p&gt;

</description>
      <category>appwrite</category>
      <category>hacktoberfest</category>
      <category>webdev</category>
      <category>hackathon</category>
    </item>
    <item>
      <title>Mastering Docker Compose: Essential CLI Commands ( part 6 )</title>
      <dc:creator>Prasenjeet Kumar</dc:creator>
      <pubDate>Sat, 13 May 2023 05:04:18 +0000</pubDate>
      <link>https://dev.to/prasenjeetsymon/mastering-docker-compose-essential-cli-commands-part-6--njj</link>
      <guid>https://dev.to/prasenjeetsymon/mastering-docker-compose-essential-cli-commands-part-6--njj</guid>
      <description>&lt;p&gt;In the previous article, we discussed the use cases and benefits of Docker Compose and multi-container applications. In this article, we will continue our discussion of Docker Compose commands.&lt;/p&gt;

&lt;p&gt;In the previous article, we covered five Docker Compose commands: &lt;code&gt;**docker-compose up**&lt;/code&gt;, &lt;strong&gt;&lt;code&gt;docker-compose down&lt;/code&gt;&lt;/strong&gt;, &lt;code&gt;**docker-compose stop**&lt;/code&gt;, &lt;strong&gt;&lt;code&gt;docker-compose start&lt;/code&gt;&lt;/strong&gt;, and &lt;code&gt;**docker-compose restart**&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we will discuss the remaining commands in great detail so that you can become a Docker hero from scratch. Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker-compose pause&lt;/code&gt; &amp;amp; &lt;code&gt;docker-compose unpause&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kIP6rTzM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/StKiS6x698JAl9d6cx/giphy.gif%3Fcid%3D7941fdc68ha8f4sb2eugku7ijnj9lnmg0ehxbg67xt8pxrhx%26ep%3Dv1_gifs_trending%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kIP6rTzM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/StKiS6x698JAl9d6cx/giphy.gif%3Fcid%3D7941fdc68ha8f4sb2eugku7ijnj9lnmg0ehxbg67xt8pxrhx%26ep%3Dv1_gifs_trending%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media4.giphy.com/media/StKiS6x698JAl9d6cx/giphy.gif?cid=7941fdc68ha8f4sb2eugku7ijnj9lnmg0ehxbg67xt8pxrhx&amp;amp;ep=v1_gifs_trending&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose pause&lt;/code&gt;&lt;/strong&gt; command is used to pause one or more containers in a Docker Compose project. When a container is paused, it will stop responding to any new requests, but its state and configuration will be preserved.&lt;/p&gt;

&lt;p&gt;Here is an example of how to use &lt;strong&gt;&lt;code&gt;docker-compose pause&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Let's say we have a simple Docker Compose file named &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; with two services: a Redis database and a Node.js application that depends on Redis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3"&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;redis&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;redis:alpine&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;REDIS_HOST=redis&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;redis&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3000:3000"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To pause the Redis container, we can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose pause redis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will pause the Redis container, and any new requests to the Redis service will fail until it is resumed.&lt;/p&gt;

&lt;p&gt;To resume the Redis container, we can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose unpause redis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will resume the Redis container, and it will start responding to new requests again.&lt;/p&gt;

&lt;p&gt;Note that &lt;strong&gt;&lt;code&gt;docker-compose pause&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;docker-compose unpause&lt;/code&gt;&lt;/strong&gt; can be used with multiple container names as arguments, allowing you to pause and unpause multiple containers at once.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker-compose ps&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qTNOMXsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/1Fm7jEapE18HwS6fkT/giphy.gif%3Fcid%3D7941fdc68ha8f4sb2eugku7ijnj9lnmg0ehxbg67xt8pxrhx%26ep%3Dv1_gifs_trending%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qTNOMXsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/1Fm7jEapE18HwS6fkT/giphy.gif%3Fcid%3D7941fdc68ha8f4sb2eugku7ijnj9lnmg0ehxbg67xt8pxrhx%26ep%3Dv1_gifs_trending%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media1.giphy.com/media/1Fm7jEapE18HwS6fkT/giphy.gif?cid=7941fdc68ha8f4sb2eugku7ijnj9lnmg0ehxbg67xt8pxrhx&amp;amp;ep=v1_gifs_trending&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose ps&lt;/code&gt;&lt;/strong&gt; command is used to display the status of containers defined in the Compose file. It lists all the containers that are created based on the services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file along with their status, ports, and names.&lt;/p&gt;

&lt;p&gt;The basic syntax of the &lt;strong&gt;&lt;code&gt;docker-compose ps&lt;/code&gt;&lt;/strong&gt; command is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;ps&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;SERVICE&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;SERVICE&lt;/code&gt;&lt;/strong&gt; is an optional argument that allows you to filter the output by specifying the name of the service. If you don't specify any service name, it will show the status of all the services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;Some commonly used options with the &lt;strong&gt;&lt;code&gt;docker-compose ps&lt;/code&gt;&lt;/strong&gt; command are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;q, --quiet&lt;/code&gt;&lt;/strong&gt;: Only display container IDs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-services&lt;/code&gt;&lt;/strong&gt;: Display only service names.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-filter&lt;/code&gt;&lt;/strong&gt;: Filter services by a property.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example of using the &lt;strong&gt;&lt;code&gt;docker-compose ps&lt;/code&gt;&lt;/strong&gt; command:&lt;/p&gt;

&lt;p&gt;Suppose we have a &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file that defines three services: &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;db&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;redis&lt;/code&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3'&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;web&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx:latest&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;8080:80"&lt;/span&gt;
  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mysql:latest&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_ROOT_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;password&lt;/span&gt;
  &lt;span class="na"&gt;redis&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;redis:latest&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To display the status of all the containers defined in this file, we can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;ps&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output the status of all three services like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;                   &lt;span class="nx"&gt;Command&lt;/span&gt;               &lt;span class="nx"&gt;State&lt;/span&gt;            &lt;span class="nx"&gt;Ports&lt;/span&gt;
&lt;span class="o"&gt;-------------------------------------------------------------------------------&lt;/span&gt;
&lt;span class="nx"&gt;compose_db_1&lt;/span&gt;   &lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;entrypoint&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sh&lt;/span&gt; &lt;span class="nx"&gt;mysqld&lt;/span&gt;      &lt;span class="nx"&gt;Up&lt;/span&gt;      &lt;span class="mi"&gt;3306&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;tcp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;33060&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;tcp&lt;/span&gt;
&lt;span class="nx"&gt;compose_redis_1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;usr&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;local&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;entr&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;   &lt;span class="nx"&gt;Up&lt;/span&gt;      &lt;span class="mi"&gt;6379&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;tcp&lt;/span&gt;
&lt;span class="nx"&gt;compose_web_1&lt;/span&gt;   &lt;span class="nx"&gt;nginx&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;g&lt;/span&gt; &lt;span class="nx"&gt;daemon&lt;/span&gt; &lt;span class="nx"&gt;off&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="nx"&gt;Up&lt;/span&gt;      &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;tcp&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we can see the status of all three services, their names, and their respective ports.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker-compose logs&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZScM-ONO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/lQO3dG0scKkkRAY5GN/giphy.gif%3Fcid%3D7941fdc64h3o8ksyium0od3a6a6f17nsqhyzm5vt0mdj2eyl%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZScM-ONO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/lQO3dG0scKkkRAY5GN/giphy.gif%3Fcid%3D7941fdc64h3o8ksyium0od3a6a6f17nsqhyzm5vt0mdj2eyl%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media0.giphy.com/media/lQO3dG0scKkkRAY5GN/giphy.gif?cid=7941fdc64h3o8ksyium0od3a6a6f17nsqhyzm5vt0mdj2eyl&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose logs&lt;/code&gt;&lt;/strong&gt; command is used to display the logs of services running inside the Docker Compose environment. It can be used to view the output of all the services or specific services, and provides options to display timestamps, tail logs and limit the number of lines displayed.&lt;/p&gt;

&lt;p&gt;The basic syntax for using the &lt;strong&gt;&lt;code&gt;docker-compose logs&lt;/code&gt;&lt;/strong&gt; command is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;logs&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;service&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are some of the commonly used options for &lt;strong&gt;&lt;code&gt;docker-compose logs&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-follow&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;f&lt;/code&gt;&lt;/strong&gt;: Follow the logs in real-time like &lt;strong&gt;&lt;code&gt;tail -f&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-timestamps&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;t&lt;/code&gt;&lt;/strong&gt;: Display timestamps along with the log lines.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-tail&lt;/code&gt;&lt;/strong&gt;: Number of lines to display from the end of the logs. For example, &lt;strong&gt;&lt;code&gt;-tail 50&lt;/code&gt;&lt;/strong&gt; will display the last 50 lines.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-no-color&lt;/code&gt;&lt;/strong&gt;: Disable colored output&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Suppose we have a &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file with two services, &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;db&lt;/code&gt;&lt;/strong&gt;. We can use the &lt;strong&gt;&lt;code&gt;docker-compose logs&lt;/code&gt;&lt;/strong&gt; command to view the logs for both services like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;logs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To view the logs for a specific service, we can specify the service name as an argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;logs&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also use the &lt;strong&gt;&lt;code&gt;--follow&lt;/code&gt;&lt;/strong&gt;option to follow the logs in real-time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;logs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To limit the number of log lines displayed, we can use the &lt;strong&gt;&lt;code&gt;--tail&lt;/code&gt;&lt;/strong&gt;option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;logs&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are some of the basic options for using the &lt;strong&gt;&lt;code&gt;docker-compose logs&lt;/code&gt;&lt;/strong&gt;command.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker-compose build&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B4d1zcOz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/hvaAO07wVOArutF1eB/giphy.gif%3Fcid%3D7941fdc6xm9g9yfo08uauay441u74xhqotad9ff1co92bziq%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B4d1zcOz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/hvaAO07wVOArutF1eB/giphy.gif%3Fcid%3D7941fdc6xm9g9yfo08uauay441u74xhqotad9ff1co92bziq%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media1.giphy.com/media/hvaAO07wVOArutF1eB/giphy.gif?cid=7941fdc6xm9g9yfo08uauay441u74xhqotad9ff1co92bziq&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose build&lt;/code&gt;&lt;/strong&gt;command is used to build the Docker images for the services defined in a &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt;file. It reads the &lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;in each service's build context and generates an image based on the instructions defined in the &lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;. It also tags the image with the service name and a version if specified.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose build&lt;/code&gt;&lt;/strong&gt; command supports several options, some of which are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-force-rm&lt;/code&gt;&lt;/strong&gt;: Removes intermediate containers and forces a new build.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-no-cache&lt;/code&gt;&lt;/strong&gt;: Builds the image without using cache.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-pull&lt;/code&gt;&lt;/strong&gt;: Always attempts to pull a newer version of the image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example usage of &lt;strong&gt;&lt;code&gt;docker-compose build&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3.8'&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;web&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
      &lt;span class="na"&gt;dockerfile&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Dockerfile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; service is built using the &lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;in the current directory (&lt;strong&gt;&lt;code&gt;.&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
). We can then run &lt;strong&gt;&lt;code&gt;docker-compose build&lt;/code&gt;&lt;/strong&gt;to build the image for the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker-compose build
Building web
Step 1/5 : FROM node:14
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 0123456789ab
Step 2/5 : WORKDIR /app
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Using cache
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 9876543210fe
Step 3/5 : COPY package&lt;span class="k"&gt;*&lt;/span&gt;.json ./
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Using cache
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; fedcba987654
Step 4/5 : RUN npm &lt;span class="nb"&gt;install&lt;/span&gt;
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Using cache
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 4567890abcde
Step 5/5 : COPY &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
 &lt;span class="nt"&gt;---&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 0123456789ab
Successfully built 0123456789ab
Successfully tagged myapp_web:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will build the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt;service using the &lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;in the current directory, and tag the resulting image with the name &lt;strong&gt;&lt;code&gt;myapp_web:latest&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To build a single service in a Docker Compose file, you can use the &lt;strong&gt;&lt;code&gt;docker-compose build&lt;/code&gt;&lt;/strong&gt; command followed by the name of the service you want to build. Here is an example:&lt;/p&gt;

&lt;p&gt;Suppose you have a &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file that defines two services, &lt;strong&gt;&lt;code&gt;app&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;database&lt;/code&gt;&lt;/strong&gt;. To build only the &lt;strong&gt;&lt;code&gt;app&lt;/code&gt;&lt;/strong&gt; service, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;build&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will build the Docker image for the &lt;strong&gt;&lt;code&gt;app&lt;/code&gt;&lt;/strong&gt; service based on the instructions in its &lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;. The &lt;strong&gt;&lt;code&gt;database&lt;/code&gt;&lt;/strong&gt; service will not be built because it was not specified.&lt;/p&gt;

&lt;p&gt;If you want to rebuild the image for the &lt;strong&gt;&lt;code&gt;app&lt;/code&gt;&lt;/strong&gt; service even if it has already been built, you can add the &lt;strong&gt;&lt;code&gt;--no-cache&lt;/code&gt;&lt;/strong&gt; option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;build&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;no&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will force Docker to rebuild the image from scratch, ignoring any previously cached layers.&lt;/p&gt;

&lt;p&gt;Note that if the &lt;strong&gt;&lt;code&gt;app&lt;/code&gt;&lt;/strong&gt; service depends on other services, those services will also be built if they have not been built yet. If you only want to build the &lt;strong&gt;&lt;code&gt;app&lt;/code&gt;&lt;/strong&gt; service and not its dependencies, you can use the &lt;strong&gt;&lt;code&gt;--no-deps&lt;/code&gt;&lt;/strong&gt; option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;build&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;no&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;deps&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will skip building any services that the &lt;strong&gt;&lt;code&gt;app&lt;/code&gt;&lt;/strong&gt;service depends on.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker-compose config&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bXqWmY0N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/ck4GQsHFbbbcdMhGQk/giphy.gif%3Fcid%3D7941fdc6i1umwjir3yck4jnommqcjipnrrn0xt5t6i5k9hz3%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bXqWmY0N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/ck4GQsHFbbbcdMhGQk/giphy.gif%3Fcid%3D7941fdc6i1umwjir3yck4jnommqcjipnrrn0xt5t6i5k9hz3%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media0.giphy.com/media/ck4GQsHFbbbcdMhGQk/giphy.gif?cid=7941fdc6i1umwjir3yck4jnommqcjipnrrn0xt5t6i5k9hz3&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose config&lt;/code&gt;&lt;/strong&gt; command is used to validate and view the Compose file in its final merged form after variable substitution and interpolation. It can be used to check if there are any syntax errors or configuration issues in the Compose file.&lt;/p&gt;

&lt;p&gt;When run, &lt;strong&gt;&lt;code&gt;docker-compose config&lt;/code&gt;&lt;/strong&gt; reads the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file, processes it and prints the resulting configuration to the standard output.&lt;/p&gt;

&lt;p&gt;Here's an example usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will check and display the final configuration for the services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;You can also specify a specific Compose file by using the &lt;strong&gt;&lt;code&gt;-f&lt;/code&gt;&lt;/strong&gt; option, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;yml&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will check and display the final configuration for the services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.prod.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;Note that &lt;strong&gt;&lt;code&gt;docker-compose config&lt;/code&gt;&lt;/strong&gt; does not actually start or stop any containers, it simply validates and displays the configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker-compose exec&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W4htM6Vx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media2.giphy.com/media/26uf4LsTj87JjVDbO/giphy.gif%3Fcid%3D7941fdc6g3colx38wzqtg839hxn13jt8tqwahkns3lrs79vo%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W4htM6Vx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media2.giphy.com/media/26uf4LsTj87JjVDbO/giphy.gif%3Fcid%3D7941fdc6g3colx38wzqtg839hxn13jt8tqwahkns3lrs79vo%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media2.giphy.com/media/26uf4LsTj87JjVDbO/giphy.gif?cid=7941fdc6g3colx38wzqtg839hxn13jt8tqwahkns3lrs79vo&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose exec&lt;/code&gt;&lt;/strong&gt; command is used to run a command in a running container. This command allows you to interact with a container as if you are logged in to it, without having to start a new shell session.&lt;/p&gt;

&lt;p&gt;The basic syntax of the command is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nx"&gt;service_name&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;service_name&lt;/code&gt;&lt;/strong&gt; is the name of the service you want to execute the command in, and &lt;strong&gt;&lt;code&gt;command&lt;/code&gt;&lt;/strong&gt; is the command you want to execute in the service's container.&lt;/p&gt;

&lt;p&gt;Some common options used with &lt;strong&gt;&lt;code&gt;docker-compose exec&lt;/code&gt;&lt;/strong&gt; are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;u&lt;/code&gt;&lt;/strong&gt;: This option allows you to specify the user context in which the command should be run.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;T&lt;/code&gt;&lt;/strong&gt;: This option disables pseudo-tty allocation, which is useful when running non-interactive commands.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-index&lt;/code&gt;&lt;/strong&gt;: This option allows you to specify which container instance to run the command in. By default, the first instance is used.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, let's say you have a service named &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; that runs a web server, and you want to run a command in its container to check the version of a software package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="nx"&gt;web&lt;/span&gt; &lt;span class="nx"&gt;bash&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;apt show my-package | grep Version&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will execute the &lt;strong&gt;&lt;code&gt;bash -c "apt show my-package | grep Version"&lt;/code&gt;&lt;/strong&gt;command in the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
 service's container, and print the output to the terminal.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker-compose pull&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zsHYNr_B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/SsIZAm4IDC6yUQMa45/giphy.gif%3Fcid%3D7941fdc61129wuqb8xlun900btin2e6lgdr3f2wi5kx92tzo%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zsHYNr_B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/SsIZAm4IDC6yUQMa45/giphy.gif%3Fcid%3D7941fdc61129wuqb8xlun900btin2e6lgdr3f2wi5kx92tzo%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media0.giphy.com/media/SsIZAm4IDC6yUQMa45/giphy.gif?cid=7941fdc61129wuqb8xlun900btin2e6lgdr3f2wi5kx92tzo&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="478" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose pull&lt;/code&gt;&lt;/strong&gt; command is used to pull (download) images for all services listed in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file. This command is used to ensure that the latest image of each service is downloaded before starting the services. If there are any updates available for the images, &lt;strong&gt;&lt;code&gt;docker-compose pull&lt;/code&gt;&lt;/strong&gt; will download them.&lt;/p&gt;

&lt;p&gt;Here is an example of using &lt;strong&gt;&lt;code&gt;docker-compose pull&lt;/code&gt;&lt;/strong&gt; command:&lt;/p&gt;

&lt;p&gt;Assume that you have a &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file that specifies two services, &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;db&lt;/code&gt;&lt;/strong&gt;, as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="nx"&gt;web&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;nginx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;latest&lt;/span&gt;
  &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;mysql&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;latest&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To download the latest images for both services, you can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will pull the latest &lt;strong&gt;&lt;code&gt;nginx&lt;/code&gt;&lt;/strong&gt;and &lt;strong&gt;&lt;code&gt;mysql&lt;/code&gt;&lt;/strong&gt;images from Docker Hub. If you have already pulled the images before, &lt;strong&gt;&lt;code&gt;docker-compose pull&lt;/code&gt;&lt;/strong&gt;will only download the updates for the images, if any.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;docker-compose push&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lwZUBGvg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/IeLIp5J7CeEz3rQjOU/giphy.gif%3Fcid%3D7941fdc60leidesjrvf6e6twhxh09zuhb0d4smujiz89m5ff%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lwZUBGvg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/IeLIp5J7CeEz3rQjOU/giphy.gif%3Fcid%3D7941fdc60leidesjrvf6e6twhxh09zuhb0d4smujiz89m5ff%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media1.giphy.com/media/IeLIp5J7CeEz3rQjOU/giphy.gif?cid=7941fdc60leidesjrvf6e6twhxh09zuhb0d4smujiz89m5ff&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose push&lt;/code&gt;&lt;/strong&gt; command is used to push images of the services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt;file to a Docker registry. This command assume that you are pushing an image you have built locally.&lt;/p&gt;

&lt;p&gt;The basic syntax of the command is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;push&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;SERVICE&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;strong&gt;&lt;code&gt;[SERVICE...]&lt;/code&gt;&lt;/strong&gt;is an optional list of services to push. If no service is specified, all services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file are pushed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="nx"&gt;service1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;build&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;localhost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;yourimage&lt;/span&gt;  &lt;span class="err"&gt;##&lt;/span&gt; &lt;span class="nx"&gt;goes&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;local&lt;/span&gt; &lt;span class="nx"&gt;registry&lt;/span&gt;

  &lt;span class="nx"&gt;service2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;build&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;your&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;dockerid&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;yourimage&lt;/span&gt;  &lt;span class="err"&gt;##&lt;/span&gt; &lt;span class="nx"&gt;goes&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;your&lt;/span&gt; &lt;span class="nx"&gt;repository&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;Docker&lt;/span&gt; &lt;span class="nx"&gt;Hub&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To push the images of these services to a Docker registry, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;push&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will push the &lt;code&gt;service2&lt;/code&gt; to the docker Hub registry and &lt;code&gt;service1&lt;/code&gt; to local registry. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker-compose rm&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G4pmii2u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media3.giphy.com/media/l0HFkA6omUyjVYqw8/giphy.gif%3Fcid%3D7941fdc6rk7uhyvdiz3pubafrftwwivzhdutuf9f31w4swk4%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G4pmii2u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media3.giphy.com/media/l0HFkA6omUyjVYqw8/giphy.gif%3Fcid%3D7941fdc6rk7uhyvdiz3pubafrftwwivzhdutuf9f31w4swk4%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media3.giphy.com/media/l0HFkA6omUyjVYqw8/giphy.gif?cid=7941fdc6rk7uhyvdiz3pubafrftwwivzhdutuf9f31w4swk4&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="320" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose rm&lt;/code&gt;&lt;/strong&gt;command is used to remove stopped containers created by the &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt;command along with their respective volumes. It can be used to clean up any resources that may have been left behind by the containers.&lt;/p&gt;

&lt;p&gt;By default, the &lt;strong&gt;&lt;code&gt;docker-compose rm&lt;/code&gt;&lt;/strong&gt; command will remove all stopped containers associated with the current &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file. However, it is also possible to target a specific service or container using the &lt;strong&gt;&lt;code&gt;--service&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;--container&lt;/code&gt;&lt;/strong&gt; options.&lt;/p&gt;

&lt;p&gt;Here's the basic syntax of the &lt;strong&gt;&lt;code&gt;docker-compose rm&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;SERVICE&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some common options that can be used with the &lt;strong&gt;&lt;code&gt;docker-compose rm&lt;/code&gt;&lt;/strong&gt; command are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-stop&lt;/code&gt;&lt;/strong&gt; : Stops the containers before removing them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-force&lt;/code&gt;&lt;/strong&gt; : Forces the removal of the containers even if they are running.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-volumes&lt;/code&gt;&lt;/strong&gt; : Removes the anonymous volumes associated with the containers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, to remove all stopped containers and their volumes for the services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file, you can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove a specific service and its stopped containers, you can use the &lt;strong&gt;&lt;code&gt;--service&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
 option followed by the name of the service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;service&lt;/span&gt; &lt;span class="nx"&gt;myservice&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to force the removal of running containers, you can use the &lt;strong&gt;&lt;code&gt;--force&lt;/code&gt;&lt;/strong&gt;option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;force&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💡 Running the command with no options also removes one-off containers created by &lt;code&gt;docker compose run&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;docker-compose run&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dNcb7yL9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/30MgfpdDZHMsw/giphy.gif%3Fcid%3D7941fdc6ziz89xbj4uwo3xz82j6qzn6wwp7cubzl3mmp1i4q%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dNcb7yL9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/30MgfpdDZHMsw/giphy.gif%3Fcid%3D7941fdc6ziz89xbj4uwo3xz82j6qzn6wwp7cubzl3mmp1i4q%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media1.giphy.com/media/30MgfpdDZHMsw/giphy.gif?cid=7941fdc6ziz89xbj4uwo3xz82j6qzn6wwp7cubzl3mmp1i4q&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="300" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose run&lt;/code&gt;&lt;/strong&gt; command is used to run a one-off command on a service defined in a Compose file. This allows you to run a command within a specific environment defined by the Compose file.&lt;/p&gt;

&lt;p&gt;The basic syntax of the command is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;service_name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;command&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;[options]&lt;/code&gt;&lt;/strong&gt; are the additional options that you can pass to the command, &lt;strong&gt;&lt;code&gt;[service_name]&lt;/code&gt;&lt;/strong&gt; is the name of the service to run the command on, and &lt;strong&gt;&lt;code&gt;[command]&lt;/code&gt;&lt;/strong&gt; is the command to run.&lt;/p&gt;

&lt;p&gt;Some common options used with the &lt;strong&gt;&lt;code&gt;docker-compose run&lt;/code&gt;&lt;/strong&gt; command are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;d&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;-detach&lt;/code&gt;&lt;/strong&gt;: Run the container in the background and print the new container ID.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;e&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;-env&lt;/code&gt;&lt;/strong&gt;: Set an environment variable for the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-name&lt;/code&gt;&lt;/strong&gt;: Assign a name to the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;v&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;-volume&lt;/code&gt;&lt;/strong&gt;: Mount a volume from the host to the container.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, to run the command &lt;strong&gt;&lt;code&gt;ls&lt;/code&gt;&lt;/strong&gt; on the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; service defined in a Compose file, you would use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="nx"&gt;web&lt;/span&gt; &lt;span class="nx"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would start a container for the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt;service and run the &lt;strong&gt;&lt;code&gt;ls&lt;/code&gt;&lt;/strong&gt;command inside it. Once the command finishes, the container will be stopped and removed.&lt;/p&gt;

&lt;p&gt;Here are some use cases for the &lt;strong&gt;&lt;code&gt;docker-compose run&lt;/code&gt;&lt;/strong&gt;command:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Run a one-time command&lt;/strong&gt;: You can use &lt;strong&gt;&lt;code&gt;docker-compose run&lt;/code&gt;&lt;/strong&gt; to run a command in a service container just once, without starting the service itself. For example, you can run a database migration script with the following command:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;
&lt;span class="nx"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="nx"&gt;webapp&lt;/span&gt; &lt;span class="nx"&gt;python&lt;/span&gt; &lt;span class="nx"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;py&lt;/span&gt; &lt;span class="nx"&gt;migrate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This will start a new container for the &lt;strong&gt;&lt;code&gt;webapp&lt;/code&gt;&lt;/strong&gt; service, run the &lt;strong&gt;&lt;code&gt;python manage.py migrate&lt;/code&gt;&lt;/strong&gt; command inside it, and then stop and remove the container.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Run a command with environment variables&lt;/strong&gt;: You can use &lt;strong&gt;&lt;code&gt;docker-compose run&lt;/code&gt;&lt;/strong&gt; to set environment variables for a command that you want to run. For example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose run -e DEBUG=True webapp python app.py
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This will start a new container for the &lt;strong&gt;&lt;code&gt;webapp&lt;/code&gt;&lt;/strong&gt; service, set the &lt;strong&gt;&lt;code&gt;DEBUG&lt;/code&gt;&lt;/strong&gt; environment variable to &lt;strong&gt;&lt;code&gt;True&lt;/code&gt;&lt;/strong&gt;, run the &lt;strong&gt;&lt;code&gt;python app.py&lt;/code&gt;&lt;/strong&gt; command inside it, and then stop and remove the container.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Debug a service&lt;/strong&gt;: You can use &lt;strong&gt;&lt;code&gt;docker-compose run&lt;/code&gt;&lt;/strong&gt; to start a service container in interactive mode and explore its environment. For example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
docker-compose run &lt;span class="nt"&gt;--service-ports&lt;/span&gt; webapp bash
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This will start a new container for the &lt;strong&gt;&lt;code&gt;webapp&lt;/code&gt;&lt;/strong&gt; service, start a Bash shell inside it, and expose the container's ports so that you can access them from your local machine. This is useful for debugging and troubleshooting.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Test a service&lt;/strong&gt;: You can use &lt;strong&gt;&lt;code&gt;docker-compose run&lt;/code&gt;&lt;/strong&gt; to test a service in isolation, without affecting other services in your composition. For example, you can run unit tests for your web application with the following command:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
docker-compose run webapp python manage.py &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This will start a new container for the &lt;strong&gt;&lt;code&gt;webapp&lt;/code&gt;&lt;/strong&gt; service, run the &lt;strong&gt;&lt;code&gt;python manage.py test&lt;/code&gt;&lt;/strong&gt; command inside it, and then stop and remove the container.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What is &lt;code&gt;-service-ports&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;--service-ports&lt;/code&gt;&lt;/strong&gt; option is used with the &lt;strong&gt;&lt;code&gt;docker-compose run&lt;/code&gt;&lt;/strong&gt; command to publish all exposed ports of the service containers to the host machine. By default, the container's ports are not exposed to the host. This option is useful when you want to run a one-off command in a service container and need to access its exposed ports from the host machine.&lt;/p&gt;

&lt;p&gt;For example, let's say you have a &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; service in your &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file that exposes port 80:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;version: &lt;span class="s1"&gt;'3'&lt;/span&gt;
services:
  web:
    build: &lt;span class="nb"&gt;.&lt;/span&gt;
    ports:
      - &lt;span class="s2"&gt;"80:80"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you want to run a &lt;strong&gt;&lt;code&gt;curl&lt;/code&gt;&lt;/strong&gt;command in the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt;service container to test the website. You can use the &lt;strong&gt;&lt;code&gt;docker-compose run&lt;/code&gt;&lt;/strong&gt;command with the &lt;strong&gt;&lt;code&gt;--service-ports&lt;/code&gt;&lt;/strong&gt;option to access the website from the host machine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose run &lt;span class="nt"&gt;--service-ports&lt;/span&gt; web curl http://localhost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;code&gt;--service-ports&lt;/code&gt;&lt;/strong&gt;option publishes the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt;service container's exposed port 80 to the host machine, so you can access the website using &lt;strong&gt;&lt;code&gt;http://localhost&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you found this article helpful, please give it a thumbs up and save it for future reference. If you have any questions or comments, feel free to leave them below.Thank you for reading! I'll see you in the next article.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>devops</category>
      <category>docker</category>
    </item>
    <item>
      <title>Discover the hidden gems of multi-container applications ( part 5 )</title>
      <dc:creator>Prasenjeet Kumar</dc:creator>
      <pubDate>Mon, 08 May 2023 16:35:45 +0000</pubDate>
      <link>https://dev.to/prasenjeetsymon/discover-the-hidden-gems-of-multi-container-applications-with-these-powerful-docker-compose-commands-and-options-part-5--5243</link>
      <guid>https://dev.to/prasenjeetsymon/discover-the-hidden-gems-of-multi-container-applications-with-these-powerful-docker-compose-commands-and-options-part-5--5243</guid>
      <description>&lt;h2&gt;
  
  
  What is multi-container Docker applications ?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2FKy53ckyw4Y6JnZeMXP%2Fgiphy.gif%3Fcid%3D7941fdc6micms70mdi8ae5o6fc5ardny87wbbsajjl7v7czp%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2FKy53ckyw4Y6JnZeMXP%2Fgiphy.gif%3Fcid%3D7941fdc6micms70mdi8ae5o6fc5ardny87wbbsajjl7v7czp%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media0.giphy.com/media/Ky53ckyw4Y6JnZeMXP/giphy.gif?cid=7941fdc6micms70mdi8ae5o6fc5ardny87wbbsajjl7v7czp&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Multi-container Docker applications are composed of multiple containers that are designed to work together to provide the complete functionality of an application. The containers within a multi-container application are typically interconnected and work in concert to accomplish a specific task or set of tasks.&lt;/p&gt;

&lt;p&gt;For example, a web application might consist of a front-end container that serves HTML pages and a back-end container that handles database queries. Both of these containers would be part of the same multi-container application and would work together to provide a fully functional web application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where do we use multi-container applications?
&lt;/h3&gt;

&lt;p&gt;Multi-container Docker applications are used in several scenarios, including:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Microservices architectur&lt;/strong&gt;e: A microservices architecture breaks down complex applications into smaller, independent services that can be developed, deployed, and scaled separately. Each microservice can be run in its own container, and multiple containers can communicate with each other using Docker networking or other mechanisms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Development and testing environments&lt;/strong&gt;: Docker Compose allows developers to define the entire stack for their applications, including databases, message queues, and other services. This allows them to quickly spin up the environment on their local machine and test their code changes before deploying to production.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;High availability and scaling&lt;/strong&gt;: By running multiple containers for a single application, Docker Swarm or Kubernetes can manage the containers and provide load balancing, high availability, and scaling features. This helps to ensure that the application is always available and can handle varying levels of traffic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separation of concerns&lt;/strong&gt;: Separating the application into multiple containers allows each container to have a specific role or responsibility. For example, one container may handle the application logic, while another container may handle the database. This separation of concerns can help with maintainability and scalability.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Why should we use multi-container applications?
&lt;/h3&gt;

&lt;p&gt;There are several benefits to using multi-container Docker applications:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Scalability:&lt;/strong&gt; With multi-container Docker applications, you can easily scale individual parts of your application independently. For example, if one part of your application is experiencing high traffic, you can scale that part of the application without having to scale the entire application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolation&lt;/strong&gt;: By using multiple containers, you can isolate different parts of your application from each other. This can help to prevent issues in one part of the application from affecting other parts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource utilization:&lt;/strong&gt; By using multiple containers, you can optimize your resource utilization. For example, if one part of your application requires more CPU or memory than another part, you can allocate resources accordingly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplified deployment&lt;/strong&gt;: With multi-container Docker applications, you can deploy all parts of your application together in a single deployment unit, making it easier to manage and deploy your application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved maintainability&lt;/strong&gt;: With multiple containers, you can update and maintain different parts of your application separately, making it easier to keep your application up-to-date and secure.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Overall, multi-container Docker applications provide a flexible, scalable, and maintainable way to deploy complex applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker Compose
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FQTxuZHy9wFzyM4Fjno%2Fgiphy.gif%3Fcid%3D7941fdc6gv4tqbrzp18q5ruk51oyifm6yi9o47qcsv6nrgkh%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FQTxuZHy9wFzyM4Fjno%2Fgiphy.gif%3Fcid%3D7941fdc6gv4tqbrzp18q5ruk51oyifm6yi9o47qcsv6nrgkh%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media4.giphy.com/media/QTxuZHy9wFzyM4Fjno/giphy.gif?cid=7941fdc6gv4tqbrzp18q5ruk51oyifm6yi9o47qcsv6nrgkh&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can define a set of related services in a YAML file, then start and stop them all together with a single command.&lt;/p&gt;

&lt;p&gt;Here are some of the most commonly used Docker Compose commands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;up&lt;/code&gt;&lt;/strong&gt;: Create and start containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;down&lt;/code&gt;&lt;/strong&gt;: Stop and remove containers, networks, images, and volumes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;start&lt;/code&gt;&lt;/strong&gt;: Start existing containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;stop&lt;/code&gt;&lt;/strong&gt;: Stop running containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;restart&lt;/code&gt;&lt;/strong&gt;: Restart running containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;pause&lt;/code&gt;&lt;/strong&gt;: Pause running containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;unpause&lt;/code&gt;&lt;/strong&gt;: Unpause paused containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ps&lt;/code&gt;&lt;/strong&gt;: List containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;logs&lt;/code&gt;&lt;/strong&gt;: View output from containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;build&lt;/code&gt;&lt;/strong&gt;: Build or rebuild services.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;config&lt;/code&gt;&lt;/strong&gt;: Validate and view the Compose file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;exec&lt;/code&gt;&lt;/strong&gt;: Execute a command in a running container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;pull&lt;/code&gt;&lt;/strong&gt;: Pull service images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;push&lt;/code&gt;&lt;/strong&gt;: Push service images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;rm&lt;/code&gt;&lt;/strong&gt;: Remove stopped containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;run&lt;/code&gt;&lt;/strong&gt;: Run a one-time command against a service.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this series on Docker, we will discuss each command in great detail so that you can become proficient in using Docker. However, in this particular article, we will only be covering 5 commands.&lt;/p&gt;

&lt;p&gt;💡 I spent hours writing this article in a way that is easy to understand for those new to Docker. If you found it helpful, please like and save it for future reference.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;docker-compose up&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FRbDKaczqWovIugyJmW%2Fgiphy.gif%3Fcid%3D7941fdc6mx5t5ts83mwa370myy8ck6cn8jzd1qicips2o69p%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FRbDKaczqWovIugyJmW%2Fgiphy.gif%3Fcid%3D7941fdc6mx5t5ts83mwa370myy8ck6cn8jzd1qicips2o69p%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media4.giphy.com/media/RbDKaczqWovIugyJmW/giphy.gif?cid=7941fdc6mx5t5ts83mwa370myy8ck6cn8jzd1qicips2o69p&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt; command is used to build, create, start, and attach to containers for a set of services defined in a &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;Here is an example &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3.9'&lt;/span&gt;  &lt;span class="c1"&gt;# specify the Compose file version&lt;/span&gt;

&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# define the services/containers&lt;/span&gt;

  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# define the MySQL service&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mysql:latest&lt;/span&gt;  &lt;span class="c1"&gt;# specify the MySQL image to use&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;  &lt;span class="c1"&gt;# restart the container automatically if it crashes&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# set environment variables&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_ROOT_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;password&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_DATABASE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db_name&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db_user&lt;/span&gt;
      &lt;span class="na"&gt;MYSQL_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db_password&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# map container port to host port&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3306:3306"&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# mount a host directory or named volume to the container&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;db_data:/var/lib/mysql&lt;/span&gt;

  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# define the Node.js/Express service&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./app&lt;/span&gt;  &lt;span class="c1"&gt;# build the Docker image using the Dockerfile in the app directory&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;  &lt;span class="c1"&gt;# restart the container automatically if it crashes&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# set environment variables&lt;/span&gt;
      &lt;span class="na"&gt;NODE_ENV&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;development&lt;/span&gt;
      &lt;span class="na"&gt;DB_HOST&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db&lt;/span&gt;
      &lt;span class="na"&gt;DB_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db_user&lt;/span&gt;
      &lt;span class="na"&gt;DB_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db_password&lt;/span&gt;
      &lt;span class="na"&gt;DB_NAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db_name&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# map container port to host port&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;8080:8080"&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# mount a host directory or named volume to the container&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./app:/app&lt;/span&gt;

  &lt;span class="na"&gt;client&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# define the React service&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./client&lt;/span&gt;  &lt;span class="c1"&gt;# build the Docker image using the Dockerfile in the client directory&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;  &lt;span class="c1"&gt;# restart the container automatically if it crashes&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# map container port to host port&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3000:3000"&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# mount a host directory or named volume to the container&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./client:/client&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# define named volumes to be used in the services&lt;/span&gt;
  &lt;span class="na"&gt;db_data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To start these services, we can use the &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt; command from the same directory as the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will build and start the &lt;code&gt;db&lt;/code&gt;, &lt;code&gt;client&lt;/code&gt;, and &lt;code&gt;app&lt;/code&gt;services. The containers will be started in the foreground, and we'll see the logs from all containers in the console.&lt;/p&gt;

&lt;p&gt;💡 When you run &lt;code&gt;docker-compose up&lt;/code&gt;, a default network is created. For example, if your project is named &lt;code&gt;myapp&lt;/code&gt;, a network called &lt;code&gt;myapp_default&lt;/code&gt; is created. Each container for a service joins the network under the service’s name. For example, a container created using a service named web would join the network under the name web&lt;/p&gt;

&lt;p&gt;When running &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt;, there are a few options that can be used to modify the behavior of the command. Here are some of the most common options:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;--detach&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;-d&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;--detach&lt;/code&gt;&lt;/strong&gt; option is used with the &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt; command to run the containers in the background and print the container IDs.&lt;/p&gt;

&lt;p&gt;By default, &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt; runs the containers in the foreground and logs their output to the terminal. However, with the &lt;strong&gt;&lt;code&gt;-d&lt;/code&gt;&lt;/strong&gt; option, the command returns immediately, and the containers continue to run in the background.&lt;/p&gt;

&lt;p&gt;For example, to start the containers defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file in the background, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up &lt;span class="nt"&gt;-d&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start the containers in the background and print their IDs.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;--build&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose up --build&lt;/code&gt;&lt;/strong&gt; command is used to build the images for the services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file before starting the containers. This command is useful when changes are made to the application code or Dockerfile of a service, and you want to rebuild the image to include those changes.&lt;/p&gt;

&lt;p&gt;When you run &lt;strong&gt;&lt;code&gt;docker-compose up --build&lt;/code&gt;&lt;/strong&gt;, it will first build the images for any services that have a &lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt; in their build context or use a custom build command specified in the &lt;strong&gt;&lt;code&gt;build&lt;/code&gt;&lt;/strong&gt; section of the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file. Then it will start the containers for all services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;Here is an example command to run &lt;strong&gt;&lt;code&gt;docker-compose up --build&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up &lt;span class="nt"&gt;--build&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will build the images for all services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file before starting the containers.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;--force-recreate&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;--force-recreate&lt;/code&gt;&lt;/strong&gt; option for the &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt; command recreates all containers defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file, even if they are already running, and forces them to be built again. This option can be used to rebuild containers with updated configurations or to recover from a corrupted state.&lt;/p&gt;

&lt;p&gt;By default, Docker Compose will try to reuse existing containers if possible, which can be useful for faster development and testing. However, in some cases, it may be necessary to rebuild and recreate all containers from scratch, such as when changing the Dockerfile or updating dependencies.&lt;/p&gt;

&lt;p&gt;Here is an example of using &lt;strong&gt;&lt;code&gt;--force-recreate&lt;/code&gt;&lt;/strong&gt; with &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up &lt;span class="nt"&gt;--force-recreate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will stop and remove all containers defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file, and then recreate them with the latest configurations and images. It can be useful when deploying changes to a production environment or when troubleshooting issues with containers.&lt;/p&gt;

&lt;p&gt;💡If a service is already running, the &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt; command will not start another instance of that service. Instead, it will leave the running instance as it is and continue to start up any other services that are defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;If you want to force a restart of the running service, you can use the &lt;strong&gt;&lt;code&gt;docker-compose up --force-recreate&lt;/code&gt;&lt;/strong&gt; command. This will stop the running service and start a new instance of it with the latest configuration defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;If you want to recreate a specific service, you can use the &lt;strong&gt;&lt;code&gt;docker-compose up --force-recreate &amp;lt;service&amp;gt;&lt;/code&gt;&lt;/strong&gt; command. This will recreate only the specified service and not the others.&lt;/p&gt;

&lt;p&gt;For example, if you have a &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; and a &lt;strong&gt;&lt;code&gt;db&lt;/code&gt;&lt;/strong&gt; service defined in your &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file, and you want to recreate only the &lt;strong&gt;&lt;code&gt;db&lt;/code&gt;&lt;/strong&gt; service, you can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up &lt;span class="nt"&gt;--force-recreate&lt;/span&gt; db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will recreate the &lt;strong&gt;&lt;code&gt;db&lt;/code&gt;&lt;/strong&gt; service and leave the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; service running as is.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;-no-recreate&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If containers already exist, don't recreate them. This option is useful when you don't want to recreate the containers each time you run the command.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;-no-build&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Don't build an image, even if it's outdated or missing. This option is useful when you don't want to build the image each time you run the command.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;--remove-orphans&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;--remove-orphans&lt;/code&gt;&lt;/strong&gt; option is used with the &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt; command to remove any containers that were created by the current project but are no longer defined in the Compose file. Orphaned containers can occur when a container is created but its service definition is later removed from the Compose file.&lt;/p&gt;

&lt;p&gt;Here is an example of using the &lt;strong&gt;&lt;code&gt;--remove-orphans&lt;/code&gt;&lt;/strong&gt; option with &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up &lt;span class="nt"&gt;--remove-orphans&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start all the services defined in the Compose file and remove any orphaned containers.&lt;/p&gt;

&lt;p&gt;💡 If a container is running that is not defined in the Compose file, it will not be affected by the &lt;strong&gt;&lt;code&gt;--remove-orphans&lt;/code&gt;&lt;/strong&gt; option. This option is only used to remove containers that were created as part of the Compose service definition, but are no longer defined in the Compose file.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;-timeout&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Specify a shutdown timeout in seconds. This option is used to specify how long Compose should wait for containers to shut down before killing them.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;--scale&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;--scale&lt;/code&gt;&lt;/strong&gt; option in &lt;strong&gt;&lt;code&gt;docker-compose&lt;/code&gt;&lt;/strong&gt; is used to scale a service up or down by increasing or decreasing the number of containers running for that service. It allows you to run multiple instances of the same service, with the same configuration, simultaneously.The syntax of using the &lt;strong&gt;&lt;code&gt;--scale&lt;/code&gt;&lt;/strong&gt; option is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up &lt;span class="nt"&gt;--scale&lt;/span&gt; &lt;span class="nv"&gt;service_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;num_instances
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;service_name&lt;/code&gt;&lt;/strong&gt; is the name of the service that you want to scale and &lt;strong&gt;&lt;code&gt;num_instances&lt;/code&gt;&lt;/strong&gt; is the number of instances you want to run for that service.&lt;/p&gt;

&lt;p&gt;For example, if you have a &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; service in your &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file and you want to run three instances of that service, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up &lt;span class="nt"&gt;--scale&lt;/span&gt; &lt;span class="nv"&gt;web&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start three instances of the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; service.&lt;/p&gt;

&lt;p&gt;💡 Note that the &lt;strong&gt;&lt;code&gt;--scale&lt;/code&gt;&lt;/strong&gt; option only works for services that are defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file. If a service is not defined in the file, the &lt;strong&gt;&lt;code&gt;--scale&lt;/code&gt;&lt;/strong&gt; option will have no effect.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker-compose down&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FTHuO7JQbmVdpEZnrMw%2Fgiphy.gif%3Fcid%3D7941fdc6iwmbph6oq4u16m76g69zid0x33f2rm2e10oiqkf7%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FTHuO7JQbmVdpEZnrMw%2Fgiphy.gif%3Fcid%3D7941fdc6iwmbph6oq4u16m76g69zid0x33f2rm2e10oiqkf7%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media4.giphy.com/media/THuO7JQbmVdpEZnrMw/giphy.gif?cid=7941fdc6iwmbph6oq4u16m76g69zid0x33f2rm2e10oiqkf7&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;docker-compose down&lt;/code&gt;&lt;/strong&gt; is a command used to stop and remove the containers, networks, and volumes that were created by &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By default, &lt;strong&gt;&lt;code&gt;docker-compose down&lt;/code&gt;&lt;/strong&gt; will stop and remove the containers declared in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file, and remove the network, but will not remove volumes. To remove the volumes as well, you can use the &lt;strong&gt;&lt;code&gt;-v&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;--volumes&lt;/code&gt;&lt;/strong&gt; option.&lt;/p&gt;

&lt;p&gt;Here is the basic syntax for &lt;strong&gt;&lt;code&gt;docker-compose down&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose down &lt;span class="o"&gt;[&lt;/span&gt;options]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are some commonly used options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;v, --volumes&lt;/code&gt;&lt;/strong&gt;: Remove named volumes declared in the &lt;strong&gt;&lt;code&gt;volumes&lt;/code&gt;&lt;/strong&gt; section of the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-rmi &amp;lt;all|local&amp;gt;&lt;/code&gt;&lt;/strong&gt;: Remove images. The &lt;strong&gt;&lt;code&gt;all&lt;/code&gt;&lt;/strong&gt; option removes all images used by any service defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file. The &lt;strong&gt;&lt;code&gt;local&lt;/code&gt;&lt;/strong&gt; option removes only images that don't have a custom tag or are not used by any other service.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;t, --timeout &amp;lt;timeout&amp;gt;&lt;/code&gt;&lt;/strong&gt; : Specify a shutdown timeout (default is 10 seconds).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-remove-orphans&lt;/code&gt;&lt;/strong&gt; : Remove containers for services not defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example command to stop and remove the containers and network defined in a &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you also want to remove the volumes, you can add the &lt;strong&gt;&lt;code&gt;-v&lt;/code&gt;&lt;/strong&gt; option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose down &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;code&gt;-rmi&lt;/code&gt;&lt;/strong&gt; option is used to remove images created by &lt;strong&gt;&lt;code&gt;docker-compose build&lt;/code&gt;&lt;/strong&gt;. It accepts two values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;all&lt;/code&gt;&lt;/strong&gt;: Remove all images used by any service defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;local&lt;/code&gt;&lt;/strong&gt;: Remove only the images that are not associated with any container. This is the default option.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are some examples:&lt;/p&gt;

&lt;p&gt;To remove all containers, networks, and volumes created by &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt; , and also remove all images used by the services:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose down &lt;span class="nt"&gt;--rmi&lt;/span&gt; all
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove all containers, networks, and volumes created by &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt; , and only remove the images that are not associated with any container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose down &lt;span class="nt"&gt;--rmi&lt;/span&gt; &lt;span class="nb"&gt;local&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;docker compose stop&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2FcKKXNlTYino7hWNXwl%2Fgiphy.gif%3Fcid%3D7941fdc6pupw4p2l4v4ml5avyd2ih88tjmws27nnupouf3gj%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2FcKKXNlTYino7hWNXwl%2Fgiphy.gif%3Fcid%3D7941fdc6pupw4p2l4v4ml5avyd2ih88tjmws27nnupouf3gj%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media0.giphy.com/media/cKKXNlTYino7hWNXwl/giphy.gif?cid=7941fdc6pupw4p2l4v4ml5avyd2ih88tjmws27nnupouf3gj&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose stop&lt;/code&gt;&lt;/strong&gt; command stops the containers defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file. It sends a SIGTERM signal to the container processes, giving them a chance to stop gracefully. If the processes do not stop within the given timeout period (default is 10 seconds), then a SIGKILL signal is sent to forcefully terminate the processes.&lt;/p&gt;

&lt;p&gt;The syntax of the command is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose stop &lt;span class="o"&gt;[&lt;/span&gt;options] &lt;span class="o"&gt;[&lt;/span&gt;SERVICE...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;strong&gt;&lt;code&gt;options&lt;/code&gt;&lt;/strong&gt; can include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;t, --timeout TIMEOUT&lt;/code&gt;&lt;/strong&gt; : Specify a custom timeout value (in seconds) for stopping the containers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And &lt;strong&gt;&lt;code&gt;SERVICE...&lt;/code&gt;&lt;/strong&gt; is an optional list of service names to stop. If no service name is provided, all services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file will be stopped.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use the &lt;strong&gt;&lt;code&gt;docker-compose stop&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose stop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will stop all the containers defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose stop mysql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will stop only the &lt;strong&gt;&lt;code&gt;mysql&lt;/code&gt;&lt;/strong&gt; service defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;💡 If we stop a service that is a dependency for another service, the dependent service will also stop. For example, if we have two services, &lt;strong&gt;&lt;code&gt;db&lt;/code&gt;&lt;/strong&gt;and &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; depends on &lt;strong&gt;&lt;code&gt;db&lt;/code&gt;&lt;/strong&gt;, if we stop the &lt;strong&gt;&lt;code&gt;db&lt;/code&gt;&lt;/strong&gt;service, the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt;service will also stop.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker-compose start&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2FZWbeEcbeo0cKI%2Fgiphy.gif%3Fcid%3D7941fdc6kq9gst2juusi2wui8fa5sazdmi0gkrsmf6tvb5xp%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2FZWbeEcbeo0cKI%2Fgiphy.gif%3Fcid%3D7941fdc6kq9gst2juusi2wui8fa5sazdmi0gkrsmf6tvb5xp%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media0.giphy.com/media/ZWbeEcbeo0cKI/giphy.gif?cid=7941fdc6kq9gst2juusi2wui8fa5sazdmi0gkrsmf6tvb5xp&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose start&lt;/code&gt;&lt;/strong&gt; command is used to start containers that are already created but are currently stopped. It does not recreate containers or rebuild images. This command is useful when you have stopped a container using &lt;strong&gt;&lt;code&gt;docker-compose stop&lt;/code&gt;&lt;/strong&gt; and want to start it again.&lt;/p&gt;

&lt;p&gt;Here's an example of using &lt;strong&gt;&lt;code&gt;docker-compose start&lt;/code&gt;&lt;/strong&gt; command:&lt;/p&gt;

&lt;p&gt;Suppose you have a docker-compose.yml file that defines two services &lt;strong&gt;&lt;code&gt;app&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;db&lt;/code&gt;&lt;/strong&gt;. If you start the services using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up &lt;span class="nt"&gt;-d&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will create and start the containers in detached mode. Now, if you want to stop the containers, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose stop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will stop the running containers. If you want to start the containers again, you can use the &lt;strong&gt;&lt;code&gt;docker-compose start&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start the stopped containers.&lt;/p&gt;

&lt;p&gt;You can start a specific service using the &lt;strong&gt;&lt;code&gt;docker-compose start&lt;/code&gt;&lt;/strong&gt; command followed by the service name. For example, if you have a service called &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; defined in your &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
file, you can start it using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose start web
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start only the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; service and any services that it depends on. If the service is already running, it will not be restarted.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker-compose restart&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2F26G3J4n04974Q%2Fgiphy.gif%3Fcid%3D7941fdc61u0ywlr2dnekmlsn32fn30zikpue0kyzs8td5abe%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2F26G3J4n04974Q%2Fgiphy.gif%3Fcid%3D7941fdc61u0ywlr2dnekmlsn32fn30zikpue0kyzs8td5abe%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media0.giphy.com/media/26G3J4n04974Q/giphy.gif?cid=7941fdc61u0ywlr2dnekmlsn32fn30zikpue0kyzs8td5abe&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker-compose restart&lt;/code&gt;&lt;/strong&gt; command is used to restart one or more services defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;Here is the basic syntax of the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose restart &lt;span class="o"&gt;[&lt;/span&gt;options] &lt;span class="o"&gt;[&lt;/span&gt;SERVICE...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some important options that can be used with this command are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-timeout TIMEOUT&lt;/code&gt;&lt;/strong&gt; : This option sets the timeout for stopping the containers. The default value is 10 seconds.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example command that restarts the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; service defined in the &lt;strong&gt;&lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose restart web
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will stop and then start the &lt;strong&gt;&lt;code&gt;web&lt;/code&gt;&lt;/strong&gt; service.&lt;/p&gt;

&lt;p&gt;In conclusion, Docker Compose is a powerful tool for managing multi-container applications. It allows you to define and run complex applications with ease, and provides a range of options for managing containers and services. In this article, we covered some of the most common Docker Compose commands and options, including &lt;strong&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;docker-compose down&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;docker-compose start&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;docker-compose restart&lt;/code&gt;&lt;/strong&gt;. We also looked at options for managing container builds, scaling services, and removing orphaned containers.&lt;/p&gt;

&lt;p&gt;If you found this article helpful, please give it a thumbs up and save it for future reference. If you have any questions or comments, feel free to leave them below.Thank you for reading! I'll see you in the next article.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>devrel</category>
      <category>docker</category>
      <category>kubernetes</category>
    </item>
    <item>
      <title>Docker : From Zero to Hero ( part 4 )</title>
      <dc:creator>Prasenjeet Kumar</dc:creator>
      <pubDate>Sat, 06 May 2023 17:20:53 +0000</pubDate>
      <link>https://dev.to/prasenjeetsymon/docker-from-zero-to-hero-part-4--18ae</link>
      <guid>https://dev.to/prasenjeetsymon/docker-from-zero-to-hero-part-4--18ae</guid>
      <description>&lt;p&gt;Docker volumes are a way to persist data outside the lifetime of a container. They are used to share data between containers or between a container and the host system. Docker volumes can be managed using the &lt;strong&gt;&lt;code&gt;docker volume&lt;/code&gt;&lt;/strong&gt;command.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wait a minute, why do we need volume?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NM8N8Fka--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media2.giphy.com/media/TwiCDH821fV3asXa6h/giphy.gif%3Fcid%3D7941fdc66yxhl7ithht6nvnlx71zy53pmi71f83exq7e8ues%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NM8N8Fka--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media2.giphy.com/media/TwiCDH821fV3asXa6h/giphy.gif%3Fcid%3D7941fdc66yxhl7ithht6nvnlx71zy53pmi71f83exq7e8ues%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media2.giphy.com/media/TwiCDH821fV3asXa6h/giphy.gif?cid=7941fdc66yxhl7ithht6nvnlx71zy53pmi71f83exq7e8ues&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Docker volumes were invented to provide a mechanism for storing and sharing data between containers, and between containers and the host system. Prior to the introduction of volumes, data storage in Docker was tied to the container's lifecycle. This meant that any data stored in the container would be lost when the container was removed or recreated. Volumes provide a way to persist data even after the container that created them has been deleted, and also enable easy sharing of data between containers. Additionally, volumes allow for more flexibility in how and where data is stored, such as using external storage systems or cloud-based storage solutions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Different type of volume
&lt;/h2&gt;

&lt;p&gt;Docker provides different types of volume to manage the persistent data for containers. The different types of volumes in a Docker container are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Named Volumes&lt;/strong&gt;:When using named volumes in Docker, the data is stored on the Docker host machine in a directory managed by Docker. Use named volumes when you need to persist data generated by a container or share data between multiple containers. For example, you can use a named volume to store a database's data so that it is not lost when the container is removed or restarted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Host Bind Mounts&lt;/strong&gt;: A host bind mount is a way to mount a directory of your choice from the host machine to the container. you can use a bind mount to mount a local directory containing configuration files into a container to customize its behavior. Suppose you want to customize the NGINX config file located inside the container. You can use a host bind mount to override the container's config file with that of the host.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anonymous Volumes&lt;/strong&gt;: Anonymous volumes are created by Docker when a container is started without specifying a named volume. Anonymous volumes are not named, so they cannot be reused. They are deleted when the container is removed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tmpfs Mounts&lt;/strong&gt;: Tmpfs mounts are mounted into the container’s memory, not on the host filesystem. They are temporary volumes and exist only while the container is running. Once the container is stopped, the tmpfs mount is removed along with all the data stored inside it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each type of volume has its own use case, and choosing the right type of volume depends on the needs of the application.&lt;/p&gt;

&lt;p&gt;Volumes can also be created with specific options, such as read-only access, or with a specific driver, which determines where the data is stored.But for now, we will not go into the driver's rabbit hole.&lt;/p&gt;

&lt;p&gt;Some common &lt;strong&gt;&lt;code&gt;docker volume&lt;/code&gt;&lt;/strong&gt; commands include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker volume create&lt;/code&gt;&lt;/strong&gt;: creates a new named or anonymous volume&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker volume ls&lt;/code&gt;&lt;/strong&gt;: lists all available volumes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker volume inspect&lt;/code&gt;&lt;/strong&gt;: displays detailed information about a specific volume&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker volume rm&lt;/code&gt;&lt;/strong&gt;: removes a specific volume&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker volume prune&lt;/code&gt;&lt;/strong&gt;: removes all unused volumes&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker volume create&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AiaRQ9qW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media3.giphy.com/media/v9qXDRYrzhLmIPvtBs/giphy.gif%3Fcid%3D7941fdc6kc5prgzgolx8imak6r7oqgdj5btdcln86adx2dqn%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AiaRQ9qW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media3.giphy.com/media/v9qXDRYrzhLmIPvtBs/giphy.gif%3Fcid%3D7941fdc6kc5prgzgolx8imak6r7oqgdj5btdcln86adx2dqn%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media3.giphy.com/media/v9qXDRYrzhLmIPvtBs/giphy.gif?cid=7941fdc6kc5prgzgolx8imak6r7oqgdj5btdcln86adx2dqn&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;docker volume create&lt;/code&gt;&lt;/strong&gt; is a command used to create a new named or anonymous volume in Docker. A named volume is a volume that has a specific name, while an anonymous volume is a volume that is not given a specific name and is only referenced by its container.&lt;/p&gt;

&lt;p&gt;The syntax for &lt;strong&gt;&lt;code&gt;docker volume create&lt;/code&gt;&lt;/strong&gt; is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;VOLUME&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;strong&gt;&lt;code&gt;OPTIONS&lt;/code&gt;&lt;/strong&gt;are any optional parameters you want to specify for the volume, and &lt;strong&gt;&lt;code&gt;VOLUME&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
 is the name you want to give the volume (if any). If you do not provide a name, an anonymous volume will be created.&lt;/p&gt;

&lt;p&gt;Some common options for &lt;strong&gt;&lt;code&gt;docker volume create&lt;/code&gt;&lt;/strong&gt; include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-driver&lt;/code&gt;&lt;/strong&gt;: Specifies the volume driver to use for the volume.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-label&lt;/code&gt;&lt;/strong&gt;: Adds a label to the volume.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-opt&lt;/code&gt;&lt;/strong&gt;: Allows you to specify additional options for the volume driver.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To create a new named volume named "my_volume", you would run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt; &lt;span class="nx"&gt;my_volume&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new named volume named "my_volume" that can be used by Docker containers.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;-driver&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;--driver&lt;/code&gt;&lt;/strong&gt; option with &lt;strong&gt;&lt;code&gt;docker volume create&lt;/code&gt;&lt;/strong&gt; specifies the driver to use for creating a new volume.&lt;/p&gt;

&lt;p&gt;A volume driver is a plugin that allows Docker to use external storage systems for managing persistent data. By default, Docker comes with a built-in &lt;strong&gt;&lt;code&gt;local&lt;/code&gt;&lt;/strong&gt; driver for managing volumes on the Docker host. However, other volume drivers can be used for managing volumes on remote or cloud storage systems.&lt;/p&gt;

&lt;p&gt;Here is an example of creating a new volume with the &lt;strong&gt;&lt;code&gt;--driver&lt;/code&gt;&lt;/strong&gt; option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;driver&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;local&lt;/span&gt; &lt;span class="nx"&gt;my_volume&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, a new volume named &lt;strong&gt;&lt;code&gt;my_volume&lt;/code&gt;&lt;/strong&gt;is created with the built-in &lt;strong&gt;&lt;code&gt;local&lt;/code&gt;&lt;/strong&gt;driver.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;-label&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;-label&lt;/code&gt;&lt;/strong&gt; option can be used with the &lt;strong&gt;&lt;code&gt;docker volume create&lt;/code&gt;&lt;/strong&gt; command to add metadata labels to a volume. These labels can be used to tag or categorize volumes, and can be helpful for organization and management purposes.&lt;/p&gt;

&lt;p&gt;For example, you could create a volume for a specific project and add a label to indicate the project name or version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="nx"&gt;project&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;myproject&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="nx"&gt;version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt; &lt;span class="nx"&gt;myvolume&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then use the &lt;strong&gt;&lt;code&gt;docker volume ls&lt;/code&gt;&lt;/strong&gt;command with the &lt;strong&gt;&lt;code&gt;--filter&lt;/code&gt;&lt;/strong&gt;option to filter and list volumes based on their labels:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;ls&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;project&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;myproject&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker volume ls&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qVOfpFFE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/0tvLwQiNjT3G6bnTzl/giphy.gif%3Fcid%3D7941fdc6r5fnaifxphfjxvrg1i2ali92s55m7w8hhlms1vqp%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qVOfpFFE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/0tvLwQiNjT3G6bnTzl/giphy.gif%3Fcid%3D7941fdc6r5fnaifxphfjxvrg1i2ali92s55m7w8hhlms1vqp%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media0.giphy.com/media/0tvLwQiNjT3G6bnTzl/giphy.gif?cid=7941fdc6r5fnaifxphfjxvrg1i2ali92s55m7w8hhlms1vqp&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;docker volume ls&lt;/code&gt;&lt;/strong&gt; command is used to list all the volumes in Docker. When you run this command, it shows a list of volumes along with their names, driver types, and other details such as mountpoint, size, and labels.&lt;/p&gt;

&lt;p&gt;Here is an example of how to use the &lt;strong&gt;&lt;code&gt;docker volume ls&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will show a list of all the volumes that exist on your Docker host. If there are no volumes, the command will not display anything.&lt;/p&gt;

&lt;p&gt;You can also use the &lt;strong&gt;&lt;code&gt;--filter&lt;/code&gt;&lt;/strong&gt;option to filter volumes based on different criteria. For example, you can list only dangling volumes using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;ls&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt; &lt;span class="nx"&gt;dangling&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will only list volumes that are not associated with any containers.&lt;/p&gt;

&lt;p&gt;You can also use the &lt;strong&gt;&lt;code&gt;--format&lt;/code&gt;&lt;/strong&gt; option to customize the output format. For example, you can show only the volume names using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;ls&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;format&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{{.Name}}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will produce output similar to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;my_volume&lt;/span&gt;
&lt;span class="nx"&gt;another_volume&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker volume inspect&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HWG18hVS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/FJznB4jaJLckw/giphy.gif%3Fcid%3D7941fdc6u8rgskd9zhehvc1pr1svxe9znxy6vm07fvg4w1fu%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HWG18hVS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media0.giphy.com/media/FJznB4jaJLckw/giphy.gif%3Fcid%3D7941fdc6u8rgskd9zhehvc1pr1svxe9znxy6vm07fvg4w1fu%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media0.giphy.com/media/FJznB4jaJLckw/giphy.gif?cid=7941fdc6u8rgskd9zhehvc1pr1svxe9znxy6vm07fvg4w1fu&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="305" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker volume inspect&lt;/code&gt;&lt;/strong&gt;command is used to display detailed information about a volume, including its configuration options and labels.&lt;/p&gt;

&lt;p&gt;The basic syntax of the command is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;inspect&lt;/span&gt; &lt;span class="nx"&gt;VOLUME&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;VOLUME&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;strong&gt;&lt;code&gt;VOLUME&lt;/code&gt;&lt;/strong&gt; is the name or ID of the volume to inspect.&lt;/p&gt;

&lt;p&gt;For example, to inspect a volume named &lt;strong&gt;&lt;code&gt;myvolume&lt;/code&gt;&lt;/strong&gt;, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;inspect&lt;/span&gt; &lt;span class="nx"&gt;myvolume&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will display a JSON-formatted output containing information about the volume, such as its name, driver, mountpoint, and labels.&lt;/p&gt;

&lt;p&gt;If you want to get a more readable output, you can use the &lt;strong&gt;&lt;code&gt;--format&lt;/code&gt;&lt;/strong&gt; option to specify a Go template. For example, the following command will display the volume name, driver, and mountpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;inspect&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;format&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{{.Name}} {{.Driver}} {{.Mountpoint}}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="nx"&gt;myvolume&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that you can inspect multiple volumes by specifying their names or IDs as arguments to the command. The output will contain information about all the volumes in a JSON array format.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker volume rm&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VepmBETn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/xT5LMDzs9xYtHXeItG/giphy.gif%3Fcid%3D7941fdc6s6glvexf79ilcbydp8mc1z28zh6g52qfsv0xot7x%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VepmBETn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media4.giphy.com/media/xT5LMDzs9xYtHXeItG/giphy.gif%3Fcid%3D7941fdc6s6glvexf79ilcbydp8mc1z28zh6g52qfsv0xot7x%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media4.giphy.com/media/xT5LMDzs9xYtHXeItG/giphy.gif?cid=7941fdc6s6glvexf79ilcbydp8mc1z28zh6g52qfsv0xot7x&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker volume rm&lt;/code&gt;&lt;/strong&gt; command is used to remove one or more Docker volumes that are no longer needed. The basic syntax of the command is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="nx"&gt;VOLUME&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;VOLUME&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;VOLUME&lt;/code&gt;&lt;/strong&gt; refers to the name or ID of the volume(s) to be removed.&lt;/p&gt;

&lt;p&gt;For example, to remove a single volume named &lt;strong&gt;&lt;code&gt;mydata&lt;/code&gt;&lt;/strong&gt;, the following command can be used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="nx"&gt;mydata&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove multiple volumes at once, simply list their names or IDs separated by a space:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="nx"&gt;mydata1&lt;/span&gt; &lt;span class="nx"&gt;mydata2&lt;/span&gt; &lt;span class="nx"&gt;mydata3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is important to note that once a volume is removed, all data stored in that volume will be lost permanently. Therefore, it is recommended to use this command with caution and ensure that any important data is backed up before proceeding with the removal.&lt;/p&gt;

&lt;p&gt;Additionally, if a volume is currently being used by a container, it cannot be removed until it is disconnected from the container. Therefore, it may be necessary to stop and remove any containers that are currently using the volume before attempting to remove it.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker volume prune&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Jqs15ze5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/YoWOIjnXT1NKIbFP9H/giphy.gif%3Fcid%3D7941fdc6r0q37jwrjrftkcsxwkngendo6vblm6cpgxp7b9m5%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Jqs15ze5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media1.giphy.com/media/YoWOIjnXT1NKIbFP9H/giphy.gif%3Fcid%3D7941fdc6r0q37jwrjrftkcsxwkngendo6vblm6cpgxp7b9m5%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media1.giphy.com/media/YoWOIjnXT1NKIbFP9H/giphy.gif?cid=7941fdc6r0q37jwrjrftkcsxwkngendo6vblm6cpgxp7b9m5&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;docker volume prune&lt;/code&gt;&lt;/strong&gt; is a command used to remove all the unused volumes in Docker. When you delete a container, its associated volumes remain on your system, taking up valuable disk space. The &lt;strong&gt;&lt;code&gt;prune&lt;/code&gt;&lt;/strong&gt; command helps you free up that space by removing all the volumes that are not used by any containers.&lt;/p&gt;

&lt;p&gt;To use the &lt;strong&gt;&lt;code&gt;docker volume prune&lt;/code&gt;&lt;/strong&gt; command, simply type the following in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;prune&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will remove all the unused volumes on your system. If you want to bypass the confirmation prompt, you can use the &lt;strong&gt;&lt;code&gt;-f&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;--force&lt;/code&gt;&lt;/strong&gt; flag, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;prune&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep in mind that this command will remove all the unused volumes, so make sure you don't have any important data stored in those volumes. If you're not sure which volumes are in use and which ones are not, you can use the &lt;strong&gt;&lt;code&gt;docker volume ls&lt;/code&gt;&lt;/strong&gt;command to list all the volumes on your system and their status.&lt;/p&gt;

&lt;p&gt;Before we move on to Docker Compose and multi-container Docker applications, there is one more command we need to teach you quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  One last command &lt;strong&gt;&lt;code&gt;docker exec&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AjQ9u-SE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media3.giphy.com/media/yRXnlNNC9U7FC/giphy.gif%3Fcid%3D7941fdc6q2gw0u58s3j8rgosko5uizgma2ijc8neaogg6lka%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AjQ9u-SE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media3.giphy.com/media/yRXnlNNC9U7FC/giphy.gif%3Fcid%3D7941fdc6q2gw0u58s3j8rgosko5uizgma2ijc8neaogg6lka%26ep%3Dv1_gifs_search%26rid%3Dgiphy.gif%26ct%3Dg" alt="https://media3.giphy.com/media/yRXnlNNC9U7FC/giphy.gif?cid=7941fdc6q2gw0u58s3j8rgosko5uizgma2ijc8neaogg6lka&amp;amp;ep=v1_gifs_search&amp;amp;rid=giphy.gif&amp;amp;ct=g" width="500" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;docker exec&lt;/code&gt;&lt;/strong&gt; is a command used to execute a command in a running container. It allows you to run a command inside a container that is already running.&lt;/p&gt;

&lt;p&gt;The syntax for &lt;strong&gt;&lt;code&gt;docker exec&lt;/code&gt;&lt;/strong&gt; is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nx"&gt;CONTAINER&lt;/span&gt; &lt;span class="nx"&gt;COMMAND&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ARG&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;OPTIONS&lt;/code&gt;&lt;/strong&gt; are optional arguments that can be used to configure the behavior of the &lt;strong&gt;&lt;code&gt;docker exec&lt;/code&gt;&lt;/strong&gt; command. For example, you can use &lt;strong&gt;&lt;code&gt;i&lt;/code&gt;&lt;/strong&gt; to keep STDIN open even if not attached, or &lt;strong&gt;&lt;code&gt;t&lt;/code&gt;&lt;/strong&gt; to allocate a pseudo-TTY.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;CONTAINER&lt;/code&gt;&lt;/strong&gt; is the name or ID of the container you want to execute a command in.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;COMMAND&lt;/code&gt;&lt;/strong&gt; is the command you want to execute in the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ARG&lt;/code&gt;&lt;/strong&gt; is an optional argument that can be passed to the command.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example of how to use &lt;strong&gt;&lt;code&gt;docker exec&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="nx"&gt;my_container_name&lt;/span&gt; &lt;span class="nx"&gt;sh&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will open a shell inside the container named &lt;strong&gt;&lt;code&gt;my_container_name&lt;/code&gt;&lt;/strong&gt;. &lt;strong&gt;&lt;code&gt;-it&lt;/code&gt;&lt;/strong&gt; options are used to allocate a pseudo-TTY and keep STDIN open even if not attached, so that you can interact with the shell.&lt;/p&gt;

&lt;p&gt;You can also run a command in the container, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="nx"&gt;my_container_name&lt;/span&gt; &lt;span class="nx"&gt;ls&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will execute the &lt;strong&gt;&lt;code&gt;ls -l&lt;/code&gt;&lt;/strong&gt; command inside the container named &lt;strong&gt;&lt;code&gt;my_container_name&lt;/code&gt;&lt;/strong&gt;, and show the output on your local terminal.&lt;/p&gt;

&lt;p&gt;Here are some of the commonly used options for the &lt;strong&gt;&lt;code&gt;docker exec&lt;/code&gt;&lt;/strong&gt;command:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;d&lt;/code&gt;&lt;/strong&gt;: Detach from the container after running the command&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="nx"&gt;my_container&lt;/span&gt; &lt;span class="nx"&gt;touch&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;tmp&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;newfile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;i&lt;/code&gt;&lt;/strong&gt;: Keep STDIN open even if not attached&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;t&lt;/code&gt;&lt;/strong&gt;: Allocate a pseudo-TTY&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;u&lt;/code&gt;&lt;/strong&gt;: Username or UID (format: [:])&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;u&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="nx"&gt;my_container&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;e&lt;/code&gt;&lt;/strong&gt;: Set environment variables&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="nx"&gt;ENV_VAR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="nx"&gt;my_container&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;w&lt;/code&gt;&lt;/strong&gt;: Working directory inside the container&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;w&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;dir&lt;/span&gt; &lt;span class="nx"&gt;my_container&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;-privileged&lt;/code&gt;&lt;/strong&gt;: Give extended privileges to the command&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;privileged&lt;/span&gt; &lt;span class="nx"&gt;my_container&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;--env-file&lt;/code&gt;&lt;/strong&gt;: Read in a file of environment variables.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;vars&lt;/span&gt; &lt;span class="nx"&gt;my_container&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 The &lt;strong&gt;&lt;code&gt;-t&lt;/code&gt;&lt;/strong&gt; option is used to allocate a pseudo-TTY, which allows users to interact with the container as if they were sitting at a terminal. The &lt;strong&gt;&lt;code&gt;-i&lt;/code&gt;&lt;/strong&gt; option is used to keep STDIN open, which enables the user to provide input to the container. The combination of &lt;strong&gt;&lt;code&gt;-t&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;-i&lt;/code&gt;&lt;/strong&gt; allows users to run an interactive shell inside a container.&lt;/p&gt;

&lt;p&gt;That concludes today's article. In the next one, we will explore multi-container applications and Docker Compose commands. If you found this article useful and interesting, please share it with your friends and colleagues, and don't forget to save it for future reference. See you in the next article! 😇&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>docker</category>
      <category>devops</category>
      <category>devrel</category>
    </item>
    <item>
      <title>Docker : From Zero to Hero ( part 3 )</title>
      <dc:creator>Prasenjeet Kumar</dc:creator>
      <pubDate>Thu, 04 May 2023 16:06:51 +0000</pubDate>
      <link>https://dev.to/prasenjeetsymon/docker-networking-from-zero-to-hero-part-3--31o6</link>
      <guid>https://dev.to/prasenjeetsymon/docker-networking-from-zero-to-hero-part-3--31o6</guid>
      <description>&lt;p&gt;Docker network is a feature that enables containers to communicate with each other in a networked environment. It provides an isolated network environment for containers so that they can communicate with each other securely and efficiently.&lt;/p&gt;

&lt;p&gt;With Docker network, we can create and manage networks for our Docker containers. These networks can be used to connect containers running on the same host or across multiple hosts, even in different data centers.&lt;/p&gt;

&lt;p&gt;We can also create different types of networks depending on our needs. For example, a bridge network provides communication between containers on the same host, while an overlay network provides communication between containers across multiple hosts.&lt;/p&gt;

&lt;p&gt;Overall, Docker network makes it easier to manage container communication and provides more flexibility and security in container networking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;docker network&lt;/code&gt;&lt;/strong&gt;is a command used to manage Docker networks. Docker provides a way to create virtual networks for containers to communicate with each other, as well as with the host machine or other networks.&lt;/p&gt;

&lt;p&gt;Some of the common commands used with &lt;strong&gt;&lt;code&gt;docker network&lt;/code&gt;&lt;/strong&gt; are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker network create&lt;/code&gt;&lt;/strong&gt;: Creates a new Docker network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker network ls&lt;/code&gt;&lt;/strong&gt;: Lists the available Docker networks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker network inspect&lt;/code&gt;&lt;/strong&gt;: Inspects the details of a Docker network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker network connect&lt;/code&gt;&lt;/strong&gt;: Connects a container to a Docker network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker network disconnect&lt;/code&gt;&lt;/strong&gt;: Disconnects a container from a Docker network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker network rm&lt;/code&gt;&lt;/strong&gt;: Removes a Docker network.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Docker networks are useful for separating containers from each other and allowing them to communicate securely. They can be used to create multi-container applications and microservices that can be scaled and managed easily.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker network create&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker network create&lt;/code&gt;&lt;/strong&gt; command is used to create a new network in Docker. This allows containers to communicate with each other even if they are running on different Docker hosts.&lt;/p&gt;

&lt;p&gt;The basic syntax of the command is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nx"&gt;NETWORK_NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are some of the most commonly used options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-driver&lt;/code&gt;&lt;/strong&gt; specifies the driver that should be used for the network. The default is &lt;strong&gt;&lt;code&gt;bridge&lt;/code&gt;&lt;/strong&gt;, but other options include &lt;strong&gt;&lt;code&gt;overlay&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;macvlan&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-subnet&lt;/code&gt;&lt;/strong&gt; specifies the subnet that should be used for the network. This is only used for networks that use the &lt;strong&gt;&lt;code&gt;bridge&lt;/code&gt;&lt;/strong&gt; driver.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-gateway&lt;/code&gt;&lt;/strong&gt; specifies the default gateway for the network. This is only used for networks that use the &lt;strong&gt;&lt;code&gt;bridge&lt;/code&gt;&lt;/strong&gt; driver.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-ip-range&lt;/code&gt;&lt;/strong&gt; specifies the range of IP addresses that can be assigned to containers on the network. This is only used for networks that use the &lt;strong&gt;&lt;code&gt;bridge&lt;/code&gt;&lt;/strong&gt; driver.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of creating a new network called &lt;strong&gt;&lt;code&gt;my_network&lt;/code&gt;&lt;/strong&gt; using the &lt;strong&gt;&lt;code&gt;bridge&lt;/code&gt;&lt;/strong&gt; driver:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;driver&lt;/span&gt; &lt;span class="nx"&gt;bridge&lt;/span&gt; &lt;span class="nx"&gt;my_network&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a new bridge network named &lt;strong&gt;&lt;code&gt;my_network&lt;/code&gt;&lt;/strong&gt;using the default subnet and gateway settings.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A bridge network is a type of network that connects multiple containers together. It is the default network type for Docker containers.&lt;/p&gt;

&lt;p&gt;When a container is connected to a bridge network, it can communicate with other containers on the same network using their IP addresses. The bridge network provides a simple and efficient way for containers to communicate with each other without exposing ports to the host system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker network ls&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker network ls&lt;/code&gt;&lt;/strong&gt; command lists all the available networks on the Docker host.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;ls&lt;/span&gt;
&lt;span class="nx"&gt;NETWORK&lt;/span&gt; &lt;span class="nx"&gt;ID&lt;/span&gt;     &lt;span class="nx"&gt;NAME&lt;/span&gt;                   &lt;span class="nx"&gt;DRIVER&lt;/span&gt;    &lt;span class="nx"&gt;SCOPE&lt;/span&gt;
&lt;span class="mi"&gt;3614&lt;/span&gt;&lt;span class="nx"&gt;fc1289ac&lt;/span&gt;   &lt;span class="nx"&gt;bridge&lt;/span&gt;                 &lt;span class="nx"&gt;bridge&lt;/span&gt;    &lt;span class="nx"&gt;local&lt;/span&gt;
&lt;span class="mi"&gt;799&lt;/span&gt;&lt;span class="nx"&gt;e5a8e5f53&lt;/span&gt;   &lt;span class="nx"&gt;host&lt;/span&gt;                   &lt;span class="nx"&gt;host&lt;/span&gt;      &lt;span class="nx"&gt;local&lt;/span&gt;
&lt;span class="nx"&gt;dcbdf4e4a4af&lt;/span&gt;   &lt;span class="nx"&gt;none&lt;/span&gt;                   &lt;span class="kc"&gt;null&lt;/span&gt;      &lt;span class="nx"&gt;local&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output lists the &lt;strong&gt;&lt;code&gt;NETWORK ID&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;NAME&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;DRIVER&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;SCOPE&lt;/code&gt;&lt;/strong&gt;of each network. The &lt;strong&gt;&lt;code&gt;bridge&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;host&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;none&lt;/code&gt;&lt;/strong&gt;networks are the default networks that are created automatically when Docker is installed.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker network inspect&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker network inspect&lt;/code&gt;&lt;/strong&gt; command is used to display detailed information about a specific Docker network or multiple networks. It provides a JSON format output containing the configuration details of the specified network(s).&lt;/p&gt;

&lt;p&gt;The basic syntax of the &lt;strong&gt;&lt;code&gt;docker network inspect&lt;/code&gt;&lt;/strong&gt; command is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;inspect&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nx"&gt;NETWORK&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;NETWORK&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;OPTIONS&lt;/code&gt;&lt;/strong&gt; are the additional flags that can be used with the &lt;strong&gt;&lt;code&gt;docker network inspect&lt;/code&gt;&lt;/strong&gt; command and &lt;strong&gt;&lt;code&gt;NETWORK&lt;/code&gt;&lt;/strong&gt; is the name or ID of the network to inspect.&lt;/p&gt;

&lt;p&gt;Some commonly used options with the &lt;strong&gt;&lt;code&gt;docker network inspect&lt;/code&gt;&lt;/strong&gt; command are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-format&lt;/code&gt;&lt;/strong&gt;: This option is used to format the output using a Go template. The default value is &lt;strong&gt;&lt;code&gt;{{json .}}&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-verbose&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;v&lt;/code&gt;&lt;/strong&gt;: This option is used to enable verbose output.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example command that inspects the &lt;strong&gt;&lt;code&gt;bridge&lt;/code&gt;&lt;/strong&gt;network and displays the output in a pretty format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;inspect&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;format&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{{json .Containers}}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="nx"&gt;bridge&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;python&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tool&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command inspects the &lt;strong&gt;&lt;code&gt;bridge&lt;/code&gt;&lt;/strong&gt; network and displays the containers connected to it in a pretty format.&lt;/p&gt;

&lt;p&gt;Note that the &lt;strong&gt;&lt;code&gt;docker network inspect&lt;/code&gt;&lt;/strong&gt; command can also be used to inspect multiple networks by specifying their names or IDs separated by a space.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;inspect&lt;/span&gt; &lt;span class="nx"&gt;network1&lt;/span&gt; &lt;span class="nx"&gt;network2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker network connect&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker network connect&lt;/code&gt;&lt;/strong&gt; command is used to attach a container to an existing Docker network. This allows the container to communicate with other containers and services that are connected to the same network.&lt;/p&gt;

&lt;p&gt;The basic syntax of the command is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;connect&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;network&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;container&lt;/span&gt;&lt;span class="err"&gt;-&lt;/span&gt;&lt;span class="na"&gt;name-or-id&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;&amp;lt;network-name&amp;gt;&lt;/code&gt;&lt;/strong&gt;is the name of the network to which the container should be attached, and &lt;strong&gt;&lt;code&gt;&amp;lt;container-name-or-id&amp;gt;&lt;/code&gt;&lt;/strong&gt;is the name or ID of the container that should be attached to the network.&lt;/p&gt;

&lt;p&gt;For example, let's say we have an existing network named "my-network" and a container named "my-container". We can connect the container to the network using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;connect&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running this command, the container "my-container" will be attached to the "my-network" network and will be able to communicate with other containers and services that are connected to the same network.&lt;/p&gt;

&lt;p&gt;It is also possible to specify additional options with the &lt;strong&gt;&lt;code&gt;docker network connect&lt;/code&gt;&lt;/strong&gt;command . Here are some commonly used options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-alias&lt;/code&gt;&lt;/strong&gt;: Assign a network alias to the container on the specified network. This can be useful for container-to-container communication.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-ip&lt;/code&gt;&lt;/strong&gt;: Assign a static IP address to the container on the specified network. This can be useful for certain network configurations that require fixed IP addresses.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example of using the &lt;strong&gt;&lt;code&gt;docker network connect&lt;/code&gt;&lt;/strong&gt;command with the &lt;strong&gt;&lt;code&gt;--alias&lt;/code&gt;&lt;/strong&gt;option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;connect&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;alias&lt;/span&gt; &lt;span class="nx"&gt;myalias&lt;/span&gt; &lt;span class="nx"&gt;mynetwork&lt;/span&gt; &lt;span class="nx"&gt;mycontainer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command connects the container &lt;strong&gt;&lt;code&gt;mycontainer&lt;/code&gt;&lt;/strong&gt;to the network &lt;strong&gt;&lt;code&gt;mynetwork&lt;/code&gt;&lt;/strong&gt;and assigns the alias &lt;strong&gt;&lt;code&gt;myalias&lt;/code&gt;&lt;/strong&gt;to the container on that network. This means that other containers on the same network can refer to &lt;strong&gt;&lt;code&gt;mycontainer&lt;/code&gt;&lt;/strong&gt;using the alias &lt;strong&gt;&lt;code&gt;myalias&lt;/code&gt;&lt;/strong&gt;instead of the container name or IP address.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to display container ip address ?
&lt;/h3&gt;

&lt;p&gt;To display the IP address of a running container, you can use the &lt;strong&gt;&lt;code&gt;docker inspect&lt;/code&gt;&lt;/strong&gt; command with the &lt;strong&gt;&lt;code&gt;--format&lt;/code&gt;&lt;/strong&gt; option to filter the output and display only the IP address.&lt;/p&gt;

&lt;p&gt;Here's an example command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;inspect&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;format&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;or&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this command, replace &lt;strong&gt;&lt;code&gt;&amp;lt;container-name-or-id&amp;gt;&lt;/code&gt;&lt;/strong&gt;with the name or ID of the container you want to inspect. The output will be the IP address of the container.&lt;/p&gt;

&lt;p&gt;You can also use the &lt;strong&gt;&lt;code&gt;docker network inspect&lt;/code&gt;&lt;/strong&gt;command to display the IP address of all the containers connected to a specific network. Here's an example command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;inspect&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;network&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;strong&gt;&lt;code&gt;&amp;lt;network-name&amp;gt;&lt;/code&gt;&lt;/strong&gt;with the name of the network you want to inspect. The output will include information about all the containers connected to that network, including their IP addresses.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The container IP address is assigned when the container is started and attached to a network. The IP address is dynamically allocated by the Docker daemon from the subnet range of the network. The Docker daemon uses the IPAM (IP Address Management) driver to manage the allocation of IP addresses for the containers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you want to assign statics ip address to the container at the time of attaching it to the network use the &lt;code&gt;-ip&lt;/code&gt; options of &lt;strong&gt;&lt;code&gt;docker network connect&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's an example command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;connect&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;ip&lt;/span&gt; &lt;span class="mf"&gt;192.168&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will connect the &lt;strong&gt;&lt;code&gt;my-container&lt;/code&gt;&lt;/strong&gt;container to the &lt;strong&gt;&lt;code&gt;my-network&lt;/code&gt;&lt;/strong&gt;network and assign it the IP address &lt;strong&gt;&lt;code&gt;192.168.0.2&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It's important to note that the specified IP address must be within the subnet of the network and must not already be in use by another container on the same network.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker network disconnect&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker network disconnect&lt;/code&gt;&lt;/strong&gt;command is used to disconnect a container from a network. It takes two arguments: the network name or ID, and the container name or ID. The syntax is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;disconnect&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nx"&gt;NETWORK&lt;/span&gt; &lt;span class="nx"&gt;CONTAINER&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;NETWORK&lt;/code&gt;&lt;/strong&gt;is the name or ID of the network to disconnect from, and &lt;strong&gt;&lt;code&gt;CONTAINER&lt;/code&gt;&lt;/strong&gt;is the name or ID of the container to disconnect. The available options are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;--force&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;-f&lt;/code&gt;&lt;/strong&gt;: This option forces the container to disconnect from the network. It is useful when the container is stuck in the "disconnecting" state.&lt;/p&gt;

&lt;p&gt;Here is an example command to disconnect a container named "my_container" from a network named "my_network":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;disconnect&lt;/span&gt; &lt;span class="nx"&gt;my_network&lt;/span&gt; &lt;span class="nx"&gt;my_container&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When a container is disconnected from a Docker network using the &lt;strong&gt;&lt;code&gt;docker network disconnect&lt;/code&gt;&lt;/strong&gt;command, its IP address is released and can be used by other containers connected to the same network.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;code&gt;docker network rm&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker network rm&lt;/code&gt;&lt;/strong&gt;command is used to remove one or more Docker networks. The general syntax of the command is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="nx"&gt;NETWORK&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;NETWORK&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;strong&gt;&lt;code&gt;NETWORK&lt;/code&gt;&lt;/strong&gt; is the name or ID of the network(s) to be removed.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;network&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will remove the &lt;strong&gt;&lt;code&gt;my-network&lt;/code&gt;&lt;/strong&gt; network.&lt;/p&gt;

&lt;p&gt;Note that you cannot remove a network that is currently being used by a container. You must disconnect the container from the network before you can remove it. You can use the &lt;strong&gt;&lt;code&gt;docker network disconnect&lt;/code&gt;&lt;/strong&gt; command to disconnect a container from a network.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>kubernetes</category>
      <category>docker</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Docker : From Zero to Hero 🛸 ( part 2)</title>
      <dc:creator>Prasenjeet Kumar</dc:creator>
      <pubDate>Thu, 04 May 2023 07:10:13 +0000</pubDate>
      <link>https://dev.to/prasenjeetsymon/docker-from-zero-to-hero-part-2-55kc</link>
      <guid>https://dev.to/prasenjeetsymon/docker-from-zero-to-hero-part-2-55kc</guid>
      <description>&lt;p&gt;Welcome to the second part of our Docker: From Zero to Hero series, where we will continue exploring essential Docker commands for container management. In the previous article, we covered Docker basics such as installation, images, and container creation. If you missed part one, you can find it &lt;strong&gt;&lt;a href="https://dev.to/prasenjeetsymon/docker-from-zero-to-hero-part-1-3a45"&gt;here&lt;/a&gt;&lt;/strong&gt;. In this article, we will focus on five critical Docker commands: &lt;strong&gt;&lt;code&gt;docker ps&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;docker stop&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;docker rm&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;docker images&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;docker rmi&lt;/code&gt;&lt;/strong&gt;. With these commands, you will be able to manage running containers, stop or remove them, manage images, and clean up your Docker environment. Let's dive in and learn more about these essential commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;docker ps&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker ps&lt;/code&gt;&lt;/strong&gt; command is used to list all running containers on the Docker host. The basic syntax of the &lt;strong&gt;&lt;code&gt;docker ps&lt;/code&gt;&lt;/strong&gt;is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;ps&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;OPTIONS&lt;/code&gt;&lt;/strong&gt;: Optional flags that can be used to modify the output of the command.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some common options include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;a&lt;/code&gt;&lt;/strong&gt;: Show all containers, including stopped ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;q&lt;/code&gt;&lt;/strong&gt;: Only show container IDs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-format&lt;/code&gt;&lt;/strong&gt;: Specify a format for the output.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;ps&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;format&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;table {{.ID}}&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;{{.Names}}&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;{{.Status}}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will list all containers (including stopped ones), and format the output as a table with columns for the container ID, name, and status.&lt;/p&gt;

&lt;p&gt;Here's an example output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;CONTAINER&lt;/span&gt; &lt;span class="nx"&gt;ID&lt;/span&gt;        &lt;span class="nx"&gt;NAMES&lt;/span&gt;               &lt;span class="nx"&gt;STATUS&lt;/span&gt;
&lt;span class="nx"&gt;a21a0498af56&lt;/span&gt;        &lt;span class="nx"&gt;nginx&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;     &lt;span class="nx"&gt;Up&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="nx"&gt;hours&lt;/span&gt;
&lt;span class="nx"&gt;be0fcf8f0c23&lt;/span&gt;        &lt;span class="nx"&gt;mysql&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;     &lt;span class="nx"&gt;Up&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="nx"&gt;days&lt;/span&gt;
&lt;span class="nx"&gt;e4389ac84062&lt;/span&gt;        &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;     &lt;span class="nx"&gt;Exited&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="nx"&gt;weeks&lt;/span&gt; &lt;span class="nx"&gt;ago&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example output, we can see the container ID, name, and status of each container on the host. The &lt;strong&gt;&lt;code&gt;nginx-container&lt;/code&gt;&lt;/strong&gt;and &lt;strong&gt;&lt;code&gt;mysql-container&lt;/code&gt;&lt;/strong&gt;are currently running, while the &lt;strong&gt;&lt;code&gt;redis-container&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
 is stopped.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;docker stop&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker stop&lt;/code&gt;&lt;/strong&gt; command is used to stop one or more running containers gracefully. It sends a &lt;strong&gt;&lt;code&gt;SIGTERM&lt;/code&gt;&lt;/strong&gt; signal to the container process, giving it a chance to shutdown gracefully, and then after a default timeout of 10 seconds, it sends a &lt;strong&gt;&lt;code&gt;SIGKILL&lt;/code&gt;&lt;/strong&gt; signal to forcefully terminate the container.&lt;/p&gt;

&lt;p&gt;Here's the basic syntax of the &lt;strong&gt;&lt;code&gt;docker stop&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;stop&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nx"&gt;CONTAINER&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;CONTAINER&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;OPTIONS&lt;/code&gt;&lt;/strong&gt; are the various flags that you can pass to customize the behavior of the command, and &lt;strong&gt;&lt;code&gt;CONTAINER&lt;/code&gt;&lt;/strong&gt; is the ID or name of the container(s) you want to stop.&lt;/p&gt;

&lt;p&gt;Here's an example of using the &lt;strong&gt;&lt;code&gt;docker stop&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;stop&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;strong&gt;&lt;code&gt;my-container&lt;/code&gt;&lt;/strong&gt; is the name of the container that we want to stop.&lt;/p&gt;

&lt;p&gt;You can also stop multiple containers at once by passing their names or IDs as space-separated arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;stop&lt;/span&gt; &lt;span class="nx"&gt;container1&lt;/span&gt; &lt;span class="nx"&gt;container2&lt;/span&gt; &lt;span class="nx"&gt;container3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some common options that can be used with &lt;strong&gt;&lt;code&gt;docker stop&lt;/code&gt;&lt;/strong&gt; are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;t, --time&lt;/code&gt;&lt;/strong&gt;: Specify a custom timeout value (in seconds) for how long to wait before forcefully stopping the container with &lt;strong&gt;&lt;code&gt;SIGKILL&lt;/code&gt;&lt;/strong&gt;. By default, the timeout is set to 10 seconds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-time=0&lt;/code&gt;&lt;/strong&gt;: Stop the container immediately without waiting for the container process to shut down gracefully. This is equivalent to sending a &lt;strong&gt;&lt;code&gt;SIGKILL&lt;/code&gt;&lt;/strong&gt; signal directly to the container process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;f, --force&lt;/code&gt;&lt;/strong&gt;: Stop the container forcefully by sending a &lt;strong&gt;&lt;code&gt;SIGKILL&lt;/code&gt;&lt;/strong&gt; signal directly to the container process, without waiting for the container process to shut down gracefully.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;stop&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're setting a custom timeout value of 30 seconds for stopping the &lt;strong&gt;&lt;code&gt;my-container&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
 container gracefully. If the container process does not shut down within 30 seconds, Docker will forcefully terminate it with a &lt;strong&gt;&lt;code&gt;SIGKILL&lt;/code&gt;&lt;/strong&gt; signal.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;docker rm&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker rm&lt;/code&gt;&lt;/strong&gt; command is used to remove one or more stopped containers. It takes one or more container IDs or names as arguments and removes them. If a container is running, you need to stop it first using the &lt;strong&gt;&lt;code&gt;docker stop&lt;/code&gt;&lt;/strong&gt; command before removing it.&lt;/p&gt;

&lt;p&gt;Here is the basic syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nx"&gt;CONTAINER&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;CONTAINER&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some of the commonly used options with the &lt;strong&gt;&lt;code&gt;docker rm&lt;/code&gt;&lt;/strong&gt; command are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;f, --force&lt;/code&gt;&lt;/strong&gt;: This option forces the removal of a running container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;v, --volumes&lt;/code&gt;&lt;/strong&gt;: This option removes the associated volumes as well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;l, --link&lt;/code&gt;&lt;/strong&gt;: This option removes the specified link as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, to remove a container named &lt;strong&gt;&lt;code&gt;my_container&lt;/code&gt;&lt;/strong&gt;, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="nx"&gt;my_container&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove multiple containers, you can specify their names or IDs as space-separated values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="nx"&gt;container1&lt;/span&gt; &lt;span class="nx"&gt;container2&lt;/span&gt; &lt;span class="nx"&gt;container3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove a running container, you can use the &lt;strong&gt;&lt;code&gt;-f&lt;/code&gt;&lt;/strong&gt;option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt; &lt;span class="nx"&gt;my_container&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove the associated volumes as well, you can use the &lt;strong&gt;&lt;code&gt;-v&lt;/code&gt;&lt;/strong&gt; option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;rm&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="nx"&gt;my_container&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When a named volume is shared with multiple containers and one container is removed using the &lt;strong&gt;&lt;code&gt;--volumes&lt;/code&gt;&lt;/strong&gt;option, the named volume will still exist and be accessible to other containers. However, if a container is using an anonymous volume and is removed with &lt;strong&gt;&lt;code&gt;--volumes&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
, the anonymous volume will be removed along with the container and any data stored in the volume will be lost. To avoid accidental data loss, use the &lt;strong&gt;&lt;code&gt;--volumes&lt;/code&gt;&lt;/strong&gt;option with caution.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;docker images&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker images&lt;/code&gt;&lt;/strong&gt; command is used to list all the images that are currently stored on your system. This command shows the repository name, tag, image ID, creation date, and size of each image.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;images&lt;/span&gt;

&lt;span class="nx"&gt;REPOSITORY&lt;/span&gt;            &lt;span class="nx"&gt;TAG&lt;/span&gt;                 &lt;span class="nx"&gt;IMAGE&lt;/span&gt; &lt;span class="nx"&gt;ID&lt;/span&gt;            &lt;span class="nx"&gt;CREATED&lt;/span&gt;             &lt;span class="nx"&gt;SIZE&lt;/span&gt;
&lt;span class="nx"&gt;ubuntu&lt;/span&gt;                &lt;span class="nx"&gt;latest&lt;/span&gt;              &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="nx"&gt;e0aa2d69a15&lt;/span&gt;        &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="nx"&gt;weeks&lt;/span&gt; &lt;span class="nx"&gt;ago&lt;/span&gt;         &lt;span class="mf"&gt;72.8&lt;/span&gt;&lt;span class="nx"&gt;MB&lt;/span&gt;
&lt;span class="nx"&gt;nginx&lt;/span&gt;                 &lt;span class="nx"&gt;latest&lt;/span&gt;              &lt;span class="nx"&gt;e37c78c07e2f&lt;/span&gt;        &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="nx"&gt;weeks&lt;/span&gt; &lt;span class="nx"&gt;ago&lt;/span&gt;         &lt;span class="mi"&gt;133&lt;/span&gt;&lt;span class="nx"&gt;MB&lt;/span&gt;
&lt;span class="nx"&gt;mysql&lt;/span&gt;                 &lt;span class="nx"&gt;latest&lt;/span&gt;              &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="nx"&gt;a0d9ac31343&lt;/span&gt;        &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="nx"&gt;weeks&lt;/span&gt; &lt;span class="nx"&gt;ago&lt;/span&gt;         &lt;span class="mi"&gt;551&lt;/span&gt;&lt;span class="nx"&gt;MB&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the output, you can see that there are three images available: Ubuntu, Nginx, and MySQL. The columns show the repository name, tag, image ID, creation date, and size of each image.&lt;/p&gt;

&lt;p&gt;You can use this command to check which images are available on your system and to find the image ID that you need to use when running a container.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;docker rmi&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker rmi&lt;/code&gt;&lt;/strong&gt; command is used to remove one or more Docker images from the host. The syntax of the command is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;rmi&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nx"&gt;IMAGE&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;IMAGE&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;OPTIONS&lt;/code&gt;&lt;/strong&gt;are additional options that can be used with the command, and &lt;strong&gt;&lt;code&gt;IMAGE&lt;/code&gt;&lt;/strong&gt; is the name or ID of the image(s) to remove.&lt;/p&gt;

&lt;p&gt;For example, to remove an image with the name &lt;strong&gt;&lt;code&gt;my-image&lt;/code&gt;&lt;/strong&gt;, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;rmi&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to remove multiple images, you can specify their names or IDs separated by a space, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;rmi&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;image1&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;image2&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;image3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some common options used with the &lt;strong&gt;&lt;code&gt;docker rmi&lt;/code&gt;&lt;/strong&gt; command include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;f&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;-force&lt;/code&gt;&lt;/strong&gt;: This option forces the removal of the image(s) even if there are running containers using them or if they have dependent child images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-no-prune&lt;/code&gt;&lt;/strong&gt;: This option prevents Docker from removing the parent images of the specified image(s) if they are not being used by any other images or containers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that when you remove an image, any containers that were created from it will still exist on the host, but they will be unusable. To remove these containers, you can use the &lt;strong&gt;&lt;code&gt;docker rm&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
 command with the &lt;strong&gt;&lt;code&gt;--force&lt;/code&gt;&lt;/strong&gt;option.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In Docker, a parent image is the image that another image is built upon using a Dockerfile. Each image can have one parent image, with the exception of the base image (e.g. Ubuntu, Alpine, etc.) which has no parent.&lt;/p&gt;

&lt;p&gt;To find the parent image(s) of a specified image, you can use the &lt;strong&gt;&lt;code&gt;docker history&lt;/code&gt;&lt;/strong&gt; command followed by the image name or ID. The output of the command will show the image's layers, with the parent image of each layer listed in the "IMAGE" column.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>devops</category>
      <category>kubernetes</category>
      <category>docker</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Docker : From Zero to Hero 🛸 ( part 1)</title>
      <dc:creator>Prasenjeet Kumar</dc:creator>
      <pubDate>Wed, 03 May 2023 06:04:50 +0000</pubDate>
      <link>https://dev.to/prasenjeetsymon/docker-from-zero-to-hero-part-1-3a45</link>
      <guid>https://dev.to/prasenjeetsymon/docker-from-zero-to-hero-part-1-3a45</guid>
      <description>&lt;p&gt;Docker is a tool that allows developers to package, distribute and run applications as containers.It provides an efficient and consistent way to deploy applications across different environments, from development to production, without worrying about dependencies or underlying infrastructure.&lt;/p&gt;

&lt;p&gt;A real-life example of the benefits of Docker can be seen in web application development. In the past, deploying a web application could be a cumbersome and error-prone process. Developers would have to manually install and configure the necessary software components on each server, and any differences in configuration could lead to issues in production.&lt;/p&gt;

&lt;p&gt;With Docker, the application and its dependencies are packaged together in a container, which can be easily deployed to any environment that supports Docker. This means that developers can be confident that their application will run the same way in development, testing, and production, and that the deployment process will be faster and more reliable.&lt;/p&gt;

&lt;p&gt;In addition, Docker allows for better resource utilization, as multiple containers can run on a single machine without interfering with each other. This can lead to cost savings and improved scalability.&lt;/p&gt;

&lt;p&gt;Overall, Docker simplifies the process of deploying and running applications, making it a valuable tool for developers and IT professionals alike.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why do we need &lt;code&gt;Docker&lt;/code&gt; if there is &lt;code&gt;virtual machine&lt;/code&gt; ?
&lt;/h1&gt;

&lt;p&gt;While both Docker and virtual machines have some similarities, they serve different purposes and have different benefits.&lt;/p&gt;

&lt;p&gt;Virtual machines emulate an entire operating system, including the kernel, and allow you to run multiple operating systems on the same physical machine. Each virtual machine requires its own set of resources, including memory, disk space, and CPU time, which can make it difficult to scale and manage large applications.&lt;/p&gt;

&lt;p&gt;Docker, on the other hand, uses containerization technology to allow you to package and run applications in lightweight, portable containers. Unlike virtual machines, Docker containers share the same host operating system kernel, which makes them more lightweight and efficient.&lt;/p&gt;

&lt;p&gt;Here are some reasons why you might choose to use Docker over virtual machines:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Resource efficiency:&lt;/strong&gt; Docker containers use fewer resources than virtual machines because they share the same host operating system kernel. This means you can run more containers on the same physical machine than you could with virtual machines.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Portability:&lt;/strong&gt; Docker containers are portable across different environments and can run on any machine that has Docker installed, regardless of the underlying infrastructure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fast deployment:&lt;/strong&gt; Docker containers can be deployed and started much faster than virtual machines, which can take several minutes to start up.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolation&lt;/strong&gt;: Docker containers provide a high degree of isolation between applications, which can help to prevent conflicts and security vulnerabilities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Version control:&lt;/strong&gt; Docker containers allow you to version control your application and its dependencies, making it easier to roll back to previous versions or test new versions without affecting the production environment.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In summary, Docker is a lightweight and efficient alternative to virtual machines that offers many benefits for modern application development and deployment.&lt;/p&gt;

&lt;h1&gt;
  
  
  Is &lt;code&gt;Docker&lt;/code&gt; and &lt;code&gt;Container&lt;/code&gt; same thing ?
&lt;/h1&gt;

&lt;p&gt;Docker is a platform that allows you to build, package, and deploy applications in containers. A container is a lightweight, portable, and self-contained environment that contains everything an application needs to run, including code, libraries, and system tools.&lt;/p&gt;

&lt;p&gt;Here are some differences between Docker and containers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Docker is a platform&lt;/strong&gt;: Docker is a platform that provides a set of tools and services to build, deploy, and manage containers. It includes the Docker Engine, which is responsible for running and managing containers, as well as other tools like Docker Compose, Docker Swarm, and Docker Registry.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Containers are the building blocks&lt;/strong&gt;: Containers are the fundamental building blocks of Docker. They provide a lightweight and portable way to package and run applications in a self-contained environment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Docker provides a standardized format&lt;/strong&gt;: Docker provides a standardized format for containers, which makes it easy to build, share, and deploy them across different environments. This format includes everything the container needs to run, including the code, dependencies, and system tools.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Docker provides a container runtime&lt;/strong&gt;: Docker provides a container runtime, which is responsible for creating, starting, stopping, and managing containers. It provides a layer of abstraction between the container and the host operating system, which allows containers to be run on any system that supports Docker.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In summary, Docker is a platform that provides a standardized way to build, package, and deploy applications in containers, while containers are the lightweight and portable environments that contain everything an application needs to run.&lt;/p&gt;

&lt;h1&gt;
  
  
  Docker Engine
&lt;/h1&gt;

&lt;p&gt;The Docker engine is the core component of Docker that allows you to create, run, and manage Docker containers. It is a lightweight and efficient runtime that can be installed on various operating systems, including Windows, macOS, and Linux.&lt;/p&gt;

&lt;p&gt;The Docker engine consists of several key components, including:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The Docker daemon:&lt;/strong&gt; This is the background service that runs on the host system and manages the containers and images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Docker CLI:&lt;/strong&gt; This is the command-line interface that allows you to interact with the Docker daemon and perform various operations such as building and running containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Docker API:&lt;/strong&gt; This is the interface that allows external tools and applications to interact with the Docker daemon.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Docker engine provides a powerful and flexible platform for building, running, and managing containerized applications. It allows you to package your application and its dependencies into a lightweight, portable container that can run on any system with Docker installed. This makes it easy to deploy and scale applications across different environments and platforms.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Docker daemon&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;The Docker daemon is the core component of the Docker engine that runs as a background service on the host system. It is responsible for managing Docker containers, images, networks, and volumes.&lt;/p&gt;

&lt;p&gt;The Docker daemon listens to requests from the Docker CLI, Docker API, and other tools, and takes the necessary actions to create, start, stop, and remove containers. It also manages the networking and storage resources required by containers, as well as the caching and sharing of images.&lt;/p&gt;

&lt;p&gt;The Docker daemon runs as a system service and can be configured to start automatically when the host system boots up. It is designed to be lightweight and efficient, using minimal system resources to provide fast and responsive container management.&lt;/p&gt;

&lt;p&gt;Overall, the Docker daemon plays a critical role in the Docker architecture, enabling the creation, deployment, and management of containerized applications in a consistent and reliable manner.&lt;/p&gt;

&lt;h1&gt;
  
  
  Docker API
&lt;/h1&gt;

&lt;p&gt;The Docker API is the interface that allows external tools and applications to interact with the Docker daemon. It provides a RESTful API that allows you to manage Docker containers, images, networks, and volumes programmatically, using standard HTTP requests.&lt;/p&gt;

&lt;p&gt;The Docker API supports a wide range of operations, including creating and starting containers, managing images and networks, and accessing logs and stats for running containers. It is designed to be platform-agnostic, making it easy to integrate Docker into a wide range of tools and workflows.&lt;/p&gt;

&lt;p&gt;The Docker API can be accessed using various programming languages and tools, including Python, Java, Node.js, and cURL. It is also supported by many popular DevOps tools such as Jenkins, Ansible, and Terraform.&lt;/p&gt;

&lt;p&gt;Overall, the Docker API provides a powerful and flexible interface for automating Docker operations and integrating Docker into your DevOps workflows. It allows you to manage Docker resources programmatically, making it easy to create, deploy, and manage containerized applications in a consistent and scalable manner.&lt;/p&gt;

&lt;h1&gt;
  
  
  Docker CLI
&lt;/h1&gt;

&lt;p&gt;The Docker CLI (Command Line Interface) is a command-line tool that allows you to interact with the Docker daemon and manage Docker containers, images, networks, and volumes. It provides a simple and powerful way to build, run, and manage Docker applications from the command line.&lt;/p&gt;

&lt;p&gt;There are many Docker commands available, but here are some of the most commonly used ones:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker run&lt;/code&gt;&lt;/strong&gt;: This command is used to create and start a new container from an image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker build&lt;/code&gt;&lt;/strong&gt;: This command is used to build a new Docker image from a Dockerfile.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker push&lt;/code&gt;&lt;/strong&gt;: This command is used to upload a Docker image to a registry.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker pull&lt;/code&gt;&lt;/strong&gt;: This command is used to download a Docker image from a registry.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker ps&lt;/code&gt;&lt;/strong&gt;: This command is used to list running containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker stop&lt;/code&gt;&lt;/strong&gt;: This command is used to stop a running container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker rm&lt;/code&gt;&lt;/strong&gt;: This command is used to remove a container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker images&lt;/code&gt;&lt;/strong&gt;: This command is used to list available Docker images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker rmi&lt;/code&gt;&lt;/strong&gt;: This command is used to remove a Docker image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker network&lt;/code&gt;&lt;/strong&gt;: This command is used to manage Docker networks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker volume&lt;/code&gt;&lt;/strong&gt;: This command is used to manage Docker volumes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker exec&lt;/code&gt;&lt;/strong&gt;: This command is used to execute a command inside a running container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docker-compose&lt;/code&gt;&lt;/strong&gt;: This command is used to manage multi-container Docker applications.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are just a few examples of the many Docker commands available. To see a full list of available commands, you can use the &lt;strong&gt;&lt;code&gt;docker --help&lt;/code&gt;&lt;/strong&gt; command or refer to the Docker documentation. We will discuss each and every command in detail now. But for this article we will discuss only 4 commands and in the upcoming articles we explore remaining commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;docker run&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker run&lt;/code&gt;&lt;/strong&gt; command is used to create and start a new Docker container from a specified image. When you run the &lt;strong&gt;&lt;code&gt;docker run&lt;/code&gt;&lt;/strong&gt; command, Docker creates a new container based on the specified image and starts it up.&lt;/p&gt;

&lt;p&gt;Here's the basic syntax for the &lt;strong&gt;&lt;code&gt;docker run&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nx"&gt;IMAGE&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;COMMAND&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ARG&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;OPTIONS&lt;/code&gt;&lt;/strong&gt;: Specifies any additional options or flags to pass to the &lt;strong&gt;&lt;code&gt;docker run&lt;/code&gt;&lt;/strong&gt; command.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;IMAGE&lt;/code&gt;&lt;/strong&gt;: Specifies the Docker image to use as the basis for the new container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;COMMAND&lt;/code&gt;&lt;/strong&gt;: Specifies the command to run inside the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ARG&lt;/code&gt;&lt;/strong&gt;: Specifies any additional arguments to pass to the command.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a list of some of the most commonly used options for the &lt;strong&gt;&lt;code&gt;docker run&lt;/code&gt;&lt;/strong&gt; command and their explanations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;d, --detach&lt;/code&gt;&lt;/strong&gt;: Runs the container in the background (detached mode) and returns control to the terminal. This option is useful when you want to run a container as a background process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;p, --publish&lt;/code&gt;&lt;/strong&gt;: Publishes a container's port(s) to the host machine. This option takes two arguments: the first specifies the port to expose inside the container, and the second specifies the port to map on the host machine. For example, &lt;strong&gt;&lt;code&gt;docker run -p 8080:80&lt;/code&gt;&lt;/strong&gt; maps port 80 inside the container to port 8080 on the host machine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;v, --volume&lt;/code&gt;&lt;/strong&gt;: Mounts a host directory or a named volume as a data volume inside the container. This option takes two arguments: the first specifies the path to the directory or volume on the host machine, and the second specifies the path inside the container to mount the volume. For example, &lt;strong&gt;&lt;code&gt;docker run -v /path/on/host:/path/in/container&lt;/code&gt;&lt;/strong&gt; mounts the host directory &lt;strong&gt;&lt;code&gt;/path/on/host&lt;/code&gt;&lt;/strong&gt; inside the container at &lt;strong&gt;&lt;code&gt;/path/in/container&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-name&lt;/code&gt;&lt;/strong&gt;: Assigns a custom name to the container instead of using a randomly generated one. This option is useful when you need to refer to a container by a specific name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;e, --env&lt;/code&gt;&lt;/strong&gt;: Sets an environment variable inside the container. This option takes an argument in the form of &lt;strong&gt;&lt;code&gt;VAR=value&lt;/code&gt;&lt;/strong&gt;. For example, &lt;strong&gt;&lt;code&gt;docker run -e VAR=value&lt;/code&gt;&lt;/strong&gt; sets the environment variable &lt;strong&gt;&lt;code&gt;VAR&lt;/code&gt;&lt;/strong&gt; to &lt;strong&gt;&lt;code&gt;value&lt;/code&gt;&lt;/strong&gt; inside the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;it, --interactive --tty&lt;/code&gt;&lt;/strong&gt;: Runs the container in interactive mode and allocates a pseudo-TTY. This option is useful when you need to interact with the container's command line.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-rm&lt;/code&gt;&lt;/strong&gt;: Removes the container automatically when it exits. This option is useful when you want to create a container for a specific task that you don't need to keep around after it completes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-network&lt;/code&gt;&lt;/strong&gt;: Specifies the network to use for the container. This option is useful when you need to connect a container to a specific network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-restart&lt;/code&gt;&lt;/strong&gt;: Specifies the restart policy for the container. This option takes an argument in the form of &lt;strong&gt;&lt;code&gt;on-failure&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;always&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;unless-stopped&lt;/code&gt;&lt;/strong&gt;, or a specific number of seconds to wait before restarting. This option is useful when you need to automatically restart a container if it exits unexpectedly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;w, --workdir&lt;/code&gt;&lt;/strong&gt;: Sets the working directory inside the container. This option takes an argument in the form of a path. For example, &lt;strong&gt;&lt;code&gt;docker run -w /app&lt;/code&gt;&lt;/strong&gt; sets the working directory inside the container to &lt;strong&gt;&lt;code&gt;/app&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to set the multiple env variables ?
&lt;/h3&gt;

&lt;p&gt;To set multiple environment variables when running a container using the &lt;strong&gt;&lt;code&gt;docker run&lt;/code&gt;&lt;/strong&gt; command, you can use multiple &lt;strong&gt;&lt;code&gt;-e&lt;/code&gt;&lt;/strong&gt; flags or pass a file containing the variables using the &lt;strong&gt;&lt;code&gt;--env-file&lt;/code&gt;&lt;/strong&gt; flag. Here are some examples:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Using multiple &lt;strong&gt;&lt;code&gt;-e&lt;/code&gt;&lt;/strong&gt; flags:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="nx"&gt;VAR1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;value1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="nx"&gt;VAR2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;value2&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using an env file:&lt;/p&gt;

&lt;p&gt;Create a file named &lt;strong&gt;&lt;code&gt;env.list&lt;/code&gt;&lt;/strong&gt;and add the environment variables with the &lt;strong&gt;&lt;code&gt;key=value&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
 format, one per line:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;VAR1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;value1&lt;/span&gt;
&lt;span class="nx"&gt;VAR2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;value2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Then pass this file to the container using the &lt;strong&gt;&lt;code&gt;--env-file&lt;/code&gt;&lt;/strong&gt;flag:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt; &lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In both cases, the container will have access to the environment variables &lt;strong&gt;&lt;code&gt;VAR1&lt;/code&gt;&lt;/strong&gt;and &lt;strong&gt;&lt;code&gt;VAR2&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Volume overview
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;--volume&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;-v&lt;/code&gt;&lt;/strong&gt; option in Docker is used to mount a directory or file from the host machine into the container. This allows the container to access and modify files on the host system.&lt;/p&gt;

&lt;p&gt;To set up a volume when running a container using the &lt;strong&gt;&lt;code&gt;docker run&lt;/code&gt;&lt;/strong&gt; command, you can use the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="sr"&gt;/container myimag&lt;/span&gt;&lt;span class="err"&gt;e
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will mount the directory located at &lt;strong&gt;&lt;code&gt;/path/on/host&lt;/code&gt;&lt;/strong&gt;on the host machine into the &lt;strong&gt;&lt;code&gt;/path/in/container&lt;/code&gt;&lt;/strong&gt; directory in the container.&lt;/p&gt;

&lt;p&gt;You can also use named volumes or anonymous volumes to manage your data. Here are some examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Using named volumes:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="nx"&gt;myvolume&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="sr"&gt;/container myimag&lt;/span&gt;&lt;span class="err"&gt;e
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This creates a named volume named &lt;strong&gt;&lt;code&gt;myvolume&lt;/code&gt;&lt;/strong&gt;and mounts it into the &lt;strong&gt;&lt;code&gt;/path/in/container&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
directory in the container. You can use the same volume across multiple containers to share data.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using anonymous volumes:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="sr"&gt;/container myimag&lt;/span&gt;&lt;span class="err"&gt;e
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This creates an anonymous volume and mounts it into the &lt;strong&gt;&lt;code&gt;/path/in/container&lt;/code&gt;&lt;/strong&gt; directory in the container. Anonymous volumes are created and managed by Docker and are not intended to be shared across containers.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also set options for your volumes, such as read-only mode, using the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="sr"&gt;/container:ro myimag&lt;/span&gt;&lt;span class="err"&gt;e
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;docker build&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;docker build&lt;/code&gt;&lt;/strong&gt; is a command in Docker that is used to build Docker images from a Dockerfile. A Dockerfile is a text file that contains a set of instructions that are used to build a Docker image. When you run the &lt;strong&gt;&lt;code&gt;docker build&lt;/code&gt;&lt;/strong&gt; command, Docker reads the instructions from the Dockerfile and builds a Docker image that you can use to run a Docker container.&lt;/p&gt;

&lt;p&gt;Here is an example command to build a Docker image from a Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;build&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;latest&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we use the &lt;strong&gt;&lt;code&gt;docker build&lt;/code&gt;&lt;/strong&gt;command to build a Docker image with the tag &lt;strong&gt;&lt;code&gt;myimage:latest&lt;/code&gt;&lt;/strong&gt;. The &lt;strong&gt;&lt;code&gt;.&lt;/code&gt;&lt;/strong&gt;at the end of the command specifies the build context, which is the directory containing the Dockerfile and any other files needed to build the Docker image.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker build&lt;/code&gt;&lt;/strong&gt; command has several options that you can use to customize the build process. Here are some of the most commonly used options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;t&lt;/code&gt;&lt;/strong&gt;: specifies the name and optionally a tag to assign to the built image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;f&lt;/code&gt;&lt;/strong&gt;: specifies the name of the Dockerfile to use for building the image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-build-arg&lt;/code&gt;&lt;/strong&gt;: passes build-time variables to the Dockerfile.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-no-cache&lt;/code&gt;&lt;/strong&gt;: forces Docker to build the image from scratch, without using any cached layers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-pull&lt;/code&gt;&lt;/strong&gt;: forces Docker to pull the latest version of the base image before building the new image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example command that uses some of these options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;build&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;latest&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt; &lt;span class="nx"&gt;Dockerfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prod&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;build&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt; &lt;span class="nx"&gt;ENVIRONMENT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;production&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;no&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we specify a different Dockerfile (&lt;strong&gt;&lt;code&gt;Dockerfile.prod&lt;/code&gt;&lt;/strong&gt;) to use for building the image, and pass a build-time variable (&lt;strong&gt;&lt;code&gt;ENVIRONMENT&lt;/code&gt;&lt;/strong&gt;) to the Dockerfile using the &lt;strong&gt;&lt;code&gt;--build-arg&lt;/code&gt;&lt;/strong&gt; option. We also specify the &lt;strong&gt;&lt;code&gt;--no-cache&lt;/code&gt;&lt;/strong&gt;option to force Docker to build the image from scratch, and the &lt;strong&gt;&lt;code&gt;--pull&lt;/code&gt;&lt;/strong&gt;option to ensure that we are using the latest version of the base image.&lt;/p&gt;

&lt;h3&gt;
  
  
  Docker Image , Dockerfile and Docker Container
&lt;/h3&gt;

&lt;p&gt;Think of a Docker image as a recipe for a dish. Just as a recipe lists all the ingredients and instructions for making a dish, a Docker image lists all the dependencies and configurations required for running an application. When you run a Docker image, it creates a Docker container, just as when you cook a dish using a recipe, it creates a serving of the dish.&lt;/p&gt;

&lt;p&gt;Now, let's compare this to a Dockerfile. A Dockerfile is like a chef's notebook that lists all the steps and ingredients required for creating a recipe. In the same way, a Dockerfile lists all the steps and configurations required for creating a Docker image. Just as a chef can use their notebook to create different versions of a dish, a developer can use a Dockerfile to create different versions of a Docker image.&lt;/p&gt;

&lt;p&gt;To summarize, a Docker image is like a recipe, a Docker container is like a serving of the dish made from the recipe, and a Dockerfile is like a chef's notebook that lists all the steps and ingredients required for creating the recipe.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dockerfile
&lt;/h3&gt;

&lt;p&gt;A Dockerfile is a text file that contains instructions for building a Docker image. The Dockerfile defines how the Docker image should be built and what should be included in it. Here are the components of a Dockerfile:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Base Image&lt;/strong&gt;: A Dockerfile starts with a base image, which serves as the foundation for your application. The base image is the starting point for building your Docker image. You can choose from a variety of base images available on Docker Hub or create your own custom base image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintainer&lt;/strong&gt;: The MAINTAINER instruction allows you to specify the author and email of the person who created the Dockerfile. This is an optional field but it's a good practice to include it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RUN&lt;/strong&gt;: The RUN instruction is used to execute commands during the Docker build process. For example, you can use the RUN instruction to install dependencies, configure the environment, and run tests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;COPY&lt;/strong&gt;: The COPY instruction is used to copy files from the host machine to the Docker image. This is useful for copying your application code, configuration files, and other assets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WORKDIR&lt;/strong&gt;: The WORKDIR instruction sets the working directory for subsequent instructions. This is useful for ensuring that your application is running in the correct directory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EXPOSE&lt;/strong&gt;: The EXPOSE instruction is used to specify the port on which your application listens. This does not actually publish the port, it only documents it. To publish the port, you need to use the -p flag when running the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CMD&lt;/strong&gt;: The CMD instruction specifies the default command that should be run when the container starts. This is usually the command that starts your application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ENV&lt;/strong&gt;: The ENV instruction is used to set environment variables in the Docker image. This is useful for configuring your application.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Base&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;
&lt;span class="nx"&gt;FROM&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;alpine&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt; &lt;span class="nx"&gt;working&lt;/span&gt; &lt;span class="nx"&gt;directory&lt;/span&gt;
&lt;span class="nx"&gt;WORKDIR&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Copy&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;working&lt;/span&gt; &lt;span class="nx"&gt;directory&lt;/span&gt;
&lt;span class="nx"&gt;COPY&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Install&lt;/span&gt; &lt;span class="nx"&gt;dependencies&lt;/span&gt;
&lt;span class="nx"&gt;RUN&lt;/span&gt; &lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Copy&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;working&lt;/span&gt; &lt;span class="nx"&gt;directory&lt;/span&gt;
&lt;span class="nx"&gt;COPY&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Expose&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;
&lt;span class="nx"&gt;EXPOSE&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Define&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;named&lt;/span&gt; &lt;span class="nx"&gt;volume&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;persist&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;
&lt;span class="nx"&gt;VOLUME&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt; &lt;span class="nx"&gt;environment&lt;/span&gt; &lt;span class="nx"&gt;variables&lt;/span&gt;
&lt;span class="nx"&gt;ENV&lt;/span&gt; &lt;span class="nx"&gt;NODE_ENV&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;production&lt;/span&gt;
&lt;span class="nx"&gt;ENV&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Start&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;application&lt;/span&gt;
&lt;span class="nx"&gt;CMD&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;npm&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt; uses the official Node.js 14 Alpine base image and sets the working directory to &lt;strong&gt;&lt;code&gt;/app&lt;/code&gt;&lt;/strong&gt;. It then copies the &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;package-lock.json&lt;/code&gt;&lt;/strong&gt; files to the working directory and installs the dependencies using &lt;strong&gt;&lt;code&gt;npm install&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Next, it copies the source code to the working directory and exposes port 3000 using the &lt;strong&gt;&lt;code&gt;EXPOSE&lt;/code&gt;&lt;/strong&gt; command. It defines a named volume called &lt;strong&gt;&lt;code&gt;data&lt;/code&gt;&lt;/strong&gt; that will be used to persist data even if the container is stopped and removed.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;ENV&lt;/code&gt;&lt;/strong&gt; command sets two environment variables, &lt;strong&gt;&lt;code&gt;NODE_ENV&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;PORT&lt;/code&gt;&lt;/strong&gt;, and the &lt;strong&gt;&lt;code&gt;CMD&lt;/code&gt;&lt;/strong&gt; command specifies that the application should be started using &lt;strong&gt;&lt;code&gt;npm start&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;With this &lt;strong&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;, you can build a Docker image that includes your Node.js application and all its dependencies. You can then run the container using the &lt;strong&gt;&lt;code&gt;docker run&lt;/code&gt;&lt;/strong&gt; command and mount the named volume to persist data, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start the container, map port 3000 on the container to port 3000 on the host machine, mount the named volume &lt;strong&gt;&lt;code&gt;data&lt;/code&gt;&lt;/strong&gt;to the &lt;strong&gt;&lt;code&gt;/app/data&lt;/code&gt;&lt;/strong&gt;directory inside the container, and name the container &lt;strong&gt;&lt;code&gt;my-app&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multiple env ? i am confused 😕
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;--env&lt;/code&gt;&lt;/strong&gt;option in &lt;strong&gt;&lt;code&gt;docker run&lt;/code&gt;&lt;/strong&gt; and the &lt;strong&gt;&lt;code&gt;ENV&lt;/code&gt;&lt;/strong&gt; instruction in the Dockerfile serve the same purpose, which is to set environment variables for the container.&lt;/p&gt;

&lt;p&gt;However, there are a few differences between them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-env&lt;/code&gt;&lt;/strong&gt; is used at runtime, while &lt;strong&gt;&lt;code&gt;ENV&lt;/code&gt;&lt;/strong&gt; is used at build time.&lt;/li&gt;
&lt;li&gt;When using &lt;strong&gt;&lt;code&gt;-env&lt;/code&gt;&lt;/strong&gt;, you can specify the environment variables as key-value pairs in the command-line, while in &lt;strong&gt;&lt;code&gt;ENV&lt;/code&gt;&lt;/strong&gt;, you specify them as environment variable assignments in the Dockerfile.&lt;/li&gt;
&lt;li&gt;The values set with &lt;strong&gt;&lt;code&gt;-env&lt;/code&gt;&lt;/strong&gt; can override the values set in the Dockerfile with &lt;strong&gt;&lt;code&gt;ENV&lt;/code&gt;&lt;/strong&gt;. This means that if the same environment variable is set with both methods, the value set with &lt;strong&gt;&lt;code&gt;-env&lt;/code&gt;&lt;/strong&gt; takes precedence.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, let's say we have the following Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;FROM&lt;/span&gt; &lt;span class="nx"&gt;alpine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;latest&lt;/span&gt;

&lt;span class="nx"&gt;ENV&lt;/span&gt; &lt;span class="nx"&gt;MY_VAR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;

&lt;span class="nx"&gt;CMD&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we build the image with &lt;strong&gt;&lt;code&gt;docker build -t my-image .&lt;/code&gt;&lt;/strong&gt;, the &lt;strong&gt;&lt;code&gt;MY_VAR&lt;/code&gt;&lt;/strong&gt; environment variable will be set to &lt;strong&gt;&lt;code&gt;value&lt;/code&gt;&lt;/strong&gt; in the image.&lt;/p&gt;

&lt;p&gt;However, if we run a container from the image with &lt;strong&gt;&lt;code&gt;docker run --env MY_VAR=new-value my-image&lt;/code&gt;&lt;/strong&gt;, the &lt;strong&gt;&lt;code&gt;MY_VAR&lt;/code&gt;&lt;/strong&gt; environment variable will be set to &lt;strong&gt;&lt;code&gt;new-value&lt;/code&gt;&lt;/strong&gt; instead of &lt;strong&gt;&lt;code&gt;value&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In summary, both &lt;strong&gt;&lt;code&gt;--env&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;ENV&lt;/code&gt;&lt;/strong&gt; are used to set environment variables in Docker containers, but &lt;strong&gt;&lt;code&gt;--env&lt;/code&gt;&lt;/strong&gt; is used at runtime and can override values set in the Dockerfile with &lt;strong&gt;&lt;code&gt;ENV&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;--build-arg&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;-build-arg&lt;/code&gt;&lt;/strong&gt; is a Docker CLI option that allows you to pass build-time variables to a Dockerfile. These variables can be used in the Dockerfile to customize the build process.&lt;/p&gt;

&lt;p&gt;When you use &lt;strong&gt;&lt;code&gt;--build-arg&lt;/code&gt;&lt;/strong&gt; option, you can specify the value of the argument by passing it on the command line, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;build&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;build&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;arg&lt;/span&gt; &lt;span class="nx"&gt;VARIABLE_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;VALUE&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the Dockerfile, you can reference the build-time variable using the syntax &lt;strong&gt;&lt;code&gt;${VARIABLE_NAME}&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;FROM&lt;/span&gt; &lt;span class="nx"&gt;alpine&lt;/span&gt;
&lt;span class="nx"&gt;ARG&lt;/span&gt; &lt;span class="nx"&gt;VARIABLE_NAME&lt;/span&gt;
&lt;span class="nx"&gt;ENV&lt;/span&gt; &lt;span class="nx"&gt;ENV_VARIABLE&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;VARIABLE_NAME&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we are using the &lt;strong&gt;&lt;code&gt;ARG&lt;/code&gt;&lt;/strong&gt; instruction to define the &lt;strong&gt;&lt;code&gt;VARIABLE_NAME&lt;/code&gt;&lt;/strong&gt; build-time variable. We are then using the &lt;strong&gt;&lt;code&gt;ENV&lt;/code&gt;&lt;/strong&gt; instruction to define an environment variable called &lt;strong&gt;&lt;code&gt;ENV_VARIABLE&lt;/code&gt;&lt;/strong&gt; and setting its value to the value of the &lt;strong&gt;&lt;code&gt;VARIABLE_NAME&lt;/code&gt;&lt;/strong&gt; build-time variable.&lt;/p&gt;

&lt;p&gt;Using &lt;strong&gt;&lt;code&gt;--build-arg&lt;/code&gt;&lt;/strong&gt; is useful when you want to pass dynamic information to the Docker build process, such as version numbers, build paths, or other configuration options. By using build-time variables, you can make your Docker builds more flexible and reusable.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;docker push&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;docker push&lt;/code&gt;&lt;/strong&gt; is a command used to push a Docker image from a local machine to a Docker registry. A Docker registry is a service that stores and distributes Docker images. Docker Hub is a public Docker registry that allows users to store and share Docker images.&lt;/p&gt;

&lt;p&gt;To push an image to a registry, you must first tag it with the registry name and repository name. For example, if you want to push an image with the tag &lt;strong&gt;&lt;code&gt;myimage&lt;/code&gt;&lt;/strong&gt; to the Docker Hub repository named &lt;strong&gt;&lt;code&gt;myrepo&lt;/code&gt;&lt;/strong&gt;, you would run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt; &lt;span class="nx"&gt;myusername&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;myrepo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;myimage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command tags the image with the registry name &lt;strong&gt;&lt;code&gt;myusername&lt;/code&gt;&lt;/strong&gt;, the repository name &lt;strong&gt;&lt;code&gt;myrepo&lt;/code&gt;&lt;/strong&gt;, and the tag &lt;strong&gt;&lt;code&gt;myimage&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once you have tagged the image, you can push it to the Docker Hub repository using the &lt;strong&gt;&lt;code&gt;docker push&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;push&lt;/span&gt; &lt;span class="nx"&gt;myusername&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;myrepo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;myimage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command uploads the image with the tag &lt;strong&gt;&lt;code&gt;myimage&lt;/code&gt;&lt;/strong&gt; to the Docker Hub repository &lt;strong&gt;&lt;code&gt;myrepo&lt;/code&gt;&lt;/strong&gt; under the username &lt;strong&gt;&lt;code&gt;myusername&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Note that you must be logged in to the Docker registry before you can push an image. You can use the &lt;strong&gt;&lt;code&gt;docker login&lt;/code&gt;&lt;/strong&gt; command to log in to a registry using your Docker Hub credentials.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;docker tag&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker tag&lt;/code&gt;&lt;/strong&gt;command is used to create a new tag for an existing Docker image. &lt;/p&gt;

&lt;p&gt;The basic syntax of the &lt;strong&gt;&lt;code&gt;docker tag&lt;/code&gt;&lt;/strong&gt; command is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="nx"&gt;SOURCE_IMAGE&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="nx"&gt;TAG&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nx"&gt;TARGET_IMAGE&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="nx"&gt;TAG&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;SOURCE_IMAGE&lt;/code&gt;&lt;/strong&gt;is the name of the source Docker image, and &lt;strong&gt;&lt;code&gt;TARGET_IMAGE&lt;/code&gt;&lt;/strong&gt;is the name of the new image you want to create. You can optionally specify a tag for both the source and target images.&lt;/p&gt;

&lt;p&gt;For example, let's say you have an image named &lt;strong&gt;&lt;code&gt;myimage&lt;/code&gt;&lt;/strong&gt;with the tag &lt;strong&gt;&lt;code&gt;latest&lt;/code&gt;&lt;/strong&gt;. To create a new tag for this image, you can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;latest&lt;/span&gt; &lt;span class="nx"&gt;myrepo&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;myimage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;v1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will create a new tag named &lt;strong&gt;&lt;code&gt;v1.0&lt;/code&gt;&lt;/strong&gt;for the &lt;strong&gt;&lt;code&gt;myimage&lt;/code&gt;&lt;/strong&gt;image, and will associate it with the &lt;strong&gt;&lt;code&gt;myrepo&lt;/code&gt;&lt;/strong&gt; repository on your Docker registry.&lt;/p&gt;

&lt;p&gt;You can also use the &lt;strong&gt;&lt;code&gt;docker tag&lt;/code&gt;&lt;/strong&gt;command to rename an image. For example, if you want to rename the &lt;strong&gt;&lt;code&gt;myimage&lt;/code&gt;&lt;/strong&gt; image to &lt;strong&gt;&lt;code&gt;mynewimage&lt;/code&gt;&lt;/strong&gt;, you can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt; &lt;span class="nx"&gt;mynewimage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new image named &lt;strong&gt;&lt;code&gt;mynewimage&lt;/code&gt;&lt;/strong&gt; that is identical to the &lt;strong&gt;&lt;code&gt;myimage&lt;/code&gt;&lt;/strong&gt; image.&lt;/p&gt;

&lt;p&gt;In summary, the &lt;strong&gt;&lt;code&gt;docker tag&lt;/code&gt;&lt;/strong&gt; command is a useful tool for managing your Docker images and providing additional names or versions for your images.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;docker pull&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;docker pull&lt;/code&gt;&lt;/strong&gt; command is used to download Docker images from a registry. It allows you to fetch a specific version of an image or the latest version of the image. The basic syntax of the &lt;strong&gt;&lt;code&gt;docker pull&lt;/code&gt;&lt;/strong&gt;command is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;OPTIONS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nx"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="nx"&gt;TAG&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;DIGEST&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;&lt;code&gt;OPTIONS&lt;/code&gt;&lt;/strong&gt;are any additional options that you want to pass to the &lt;strong&gt;&lt;code&gt;docker pull&lt;/code&gt;&lt;/strong&gt;command. &lt;strong&gt;&lt;code&gt;NAME&lt;/code&gt;&lt;/strong&gt;is the name of the Docker image that you want to download. You can also specify the image tag using the &lt;strong&gt;&lt;code&gt;:TAG&lt;/code&gt;&lt;/strong&gt;syntax or the image digest using the &lt;strong&gt;&lt;code&gt;@DIGEST&lt;/code&gt;&lt;/strong&gt;syntax.&lt;/p&gt;

&lt;p&gt;For example, to download the latest version of the &lt;strong&gt;&lt;code&gt;nginx&lt;/code&gt;&lt;/strong&gt;image from Docker Hub, you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="nx"&gt;nginx&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To download a specific version of the &lt;strong&gt;&lt;code&gt;nginx&lt;/code&gt;&lt;/strong&gt;image, you can specify the image tag as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="nx"&gt;nginx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;1.20&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will download version 1.20.1 of the &lt;strong&gt;&lt;code&gt;nginx&lt;/code&gt;&lt;/strong&gt;image.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;OPTIONS&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Here are some common options for &lt;code&gt;docker pull&lt;/code&gt; command:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-all-tags&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;a&lt;/code&gt;&lt;/strong&gt;: By default, &lt;strong&gt;&lt;code&gt;docker pull&lt;/code&gt;&lt;/strong&gt; pulls only the latest image. This option pulls all the available tags for the specified image repository.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-disable-content-trust&lt;/code&gt;&lt;/strong&gt;: By default, Docker verifies the authenticity of the image with Notary. This option disables this check.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-platform&lt;/code&gt;&lt;/strong&gt;: This option specifies the platform for the pulled image. This is useful for multi-architecture images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-quiet&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;q&lt;/code&gt;&lt;/strong&gt;: This option only displays the image ID of the pulled image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-registry-mirror&lt;/code&gt;&lt;/strong&gt;: This option specifies a registry mirror to use for pulling the image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-no-trunc&lt;/code&gt;&lt;/strong&gt;: By default, &lt;strong&gt;&lt;code&gt;docker pull&lt;/code&gt;&lt;/strong&gt; truncates the image ID to the first 12 characters. This option displays the full length of the image ID.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Pull&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;latest&lt;/span&gt; &lt;span class="nx"&gt;version&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;an&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;
&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;latest&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Pull&lt;/span&gt; &lt;span class="nx"&gt;all&lt;/span&gt; &lt;span class="nx"&gt;available&lt;/span&gt; &lt;span class="nx"&gt;tags&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;an&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt; &lt;span class="nx"&gt;repository&lt;/span&gt;
&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;tags&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Disable&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="nx"&gt;trust&lt;/span&gt; &lt;span class="nx"&gt;verification&lt;/span&gt;
&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;disable&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;trust&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Pull&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;specific&lt;/span&gt; &lt;span class="nx"&gt;platform&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;multi&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;architecture&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;
&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;platform&lt;/span&gt; &lt;span class="nx"&gt;linux&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;amd64&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Only&lt;/span&gt; &lt;span class="nx"&gt;display&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt; &lt;span class="nx"&gt;ID&lt;/span&gt;
&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;quiet&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Use&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;registry&lt;/span&gt; &lt;span class="nx"&gt;mirror&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;
&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;registry&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;mirror&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//myregistrymirror.com myimage&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Display&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;full&lt;/span&gt; &lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt; &lt;span class="nx"&gt;ID&lt;/span&gt;
&lt;span class="nx"&gt;docker&lt;/span&gt; &lt;span class="nx"&gt;pull&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;no&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;trunc&lt;/span&gt; &lt;span class="nx"&gt;myimage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;A &lt;strong&gt;Docker registry mirror&lt;/strong&gt; is a server that caches and serves Docker images from a remote registry such as &lt;strong&gt;Docker Hub&lt;/strong&gt;. When you pull an image, Docker first checks the local registry mirror for the image, and if it's not found, it pulls the image from the remote registry. This can improve the speed of pulling images, especially in situations where multiple users or systems need to pull the same images repeatedly. This only applies if you are using &lt;code&gt;--registry-mirror&lt;/code&gt; option to pull the image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Notary&lt;/strong&gt; is an open-source project that provides a trust framework for signing and verifying container images. It helps ensure the integrity and authenticity of container images by allowing you to digitally sign them and store the signature in a trusted registry.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>webdev</category>
      <category>devops</category>
      <category>docker</category>
      <category>kubernetes</category>
    </item>
    <item>
      <title>Lerna: Simplifying Multi-Package JavaScript Projects with Monorepo</title>
      <dc:creator>Prasenjeet Kumar</dc:creator>
      <pubDate>Fri, 28 Apr 2023 03:43:55 +0000</pubDate>
      <link>https://dev.to/prasenjeetsymon/lerna-simplifying-multi-package-javascript-projects-with-monorepo-4a97</link>
      <guid>https://dev.to/prasenjeetsymon/lerna-simplifying-multi-package-javascript-projects-with-monorepo-4a97</guid>
      <description>&lt;p&gt;Lerna is a popular tool used for managing multi-package JavaScript projects. It simplifies the development process, improves code sharing and collaboration, and makes it easier to maintain and manage large codebases. In this article, we'll explore the benefits of using a monorepo and how Lerna can help you manage your multi-package JavaScript projects more effectively.&lt;/p&gt;

&lt;h1&gt;
  
  
  Monorepo
&lt;/h1&gt;

&lt;p&gt;A monorepo, short for "monolithic repository", is a software development approach where multiple projects are stored in a single repository. This means that instead of having separate repositories for each project, they are all kept together in a single repository, with version control applied at the repository level.&lt;/p&gt;

&lt;p&gt;In a monorepo, all the projects share the same codebase, dependencies, and configuration. This allows for better code reuse, easier cross-project refactoring, and simplified dependency management. Monorepos are commonly used by large software companies such as Google, Facebook, and Microsoft, as well as by many open-source projects.&lt;/p&gt;

&lt;p&gt;There are several reasons why one might choose a monorepo for their software development:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Code sharing&lt;/strong&gt;: By keeping all code in a single repository, it becomes easier to share code between different projects. This is especially useful when there are common components or libraries that are used across multiple projects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consistency&lt;/strong&gt;: With a monorepo, all projects share the same codebase and dependencies. This ensures consistency across projects and reduces the risk of incompatibilities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplified development process&lt;/strong&gt;: With all code in a single repository, developers can work on multiple projects simultaneously without having to switch between different repositories.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easier maintenance&lt;/strong&gt;: Since all code is in a single repository, it's easier to maintain and manage. This makes it simpler to update dependencies, apply security patches, and perform other maintenance tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved collaboration&lt;/strong&gt;: By using a monorepo, teams can collaborate more easily since all code is in one place. This can help reduce communication overhead and speed up development.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Overall, a monorepo can simplify the development process and improve code sharing and collaboration, making it a popular choice for many development teams.&lt;/p&gt;

&lt;p&gt;Lerna is a popular tool used for managing multi-package JavaScript projects. Some of the most used Lerna commands are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;lerna init&lt;/code&gt;: Initializes a new Lerna repository in the current directory.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lerna create&lt;/code&gt;: Creates a new package in the Lerna repository.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lerna bootstrap:&lt;/code&gt; Installs package dependencies and links local packages together.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lerna add&lt;/code&gt;: Adds a package as a dependency to another package in the Lerna repository.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lerna exec&lt;/code&gt;: Runs an arbitrary command in each package of the Lerna repository.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lerna changed&lt;/code&gt;: Lists all packages that have changed since the last release.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lerna diff&lt;/code&gt;: Shows the difference between the current working tree and the last release of a package.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lerna publish&lt;/code&gt;: Publishes new package versions to a package registry, such as npm.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lerna run&lt;/code&gt;: Runs an npm script in each package of the Lerna repository.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lerna clean&lt;/code&gt;: Removes the node_modules directory from all packages in the Lerna repository.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;lerna init&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;lerna init&lt;/code&gt;&lt;/strong&gt; is a command used to initialize a new Lerna repository with a basic directory structure and a default &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt; file. It creates a new Git repository and adds an initial commit.&lt;/p&gt;

&lt;p&gt;To use &lt;strong&gt;&lt;code&gt;lerna init&lt;/code&gt;&lt;/strong&gt;, you need to have Lerna installed globally on your machine. Once installed, you can navigate to the directory where you want to create the Lerna repository and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;lerna&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create the following directory structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;my-lerna-repo/&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nx"&gt;packages/&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;package.json&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nx"&gt;lerna.json&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;code&gt;packages&lt;/code&gt;&lt;/strong&gt; directory is where you will create your individual packages. Each package should have its own directory within the &lt;strong&gt;&lt;code&gt;packages&lt;/code&gt;&lt;/strong&gt; directory. The &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt; file is the main &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt; file for the entire repository. The &lt;strong&gt;&lt;code&gt;lerna.json&lt;/code&gt;&lt;/strong&gt; file contains configuration options for Lerna.&lt;/p&gt;

&lt;p&gt;After running &lt;strong&gt;&lt;code&gt;lerna init&lt;/code&gt;&lt;/strong&gt;, you can add packages to the repository using the &lt;strong&gt;&lt;code&gt;lerna create&lt;/code&gt;&lt;/strong&gt; command.&lt;/p&gt;

&lt;p&gt;💡 To install the lerna globally on your machine use the command &lt;code&gt;npm i -g lerna&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Lerna Create
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;lerna create&lt;/code&gt;&lt;/strong&gt; command is used to create a new package in a Lerna-managed monorepo. It initializes the package with a basic directory structure and a &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;Here's the basic syntax of the &lt;strong&gt;&lt;code&gt;lerna create&lt;/code&gt;&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;lerna&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;name&amp;gt;&lt;/code&gt;&lt;/strong&gt;: The name of the package to be created&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;[loc]&lt;/code&gt;&lt;/strong&gt;: Optional. The location where the package should be created. If not specified, the package will be created in the default packages directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, to create a new package called &lt;strong&gt;&lt;code&gt;my-package&lt;/code&gt;&lt;/strong&gt; in the default packages directory, you can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kr"&gt;package&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new directory called &lt;strong&gt;&lt;code&gt;packages/my-package&lt;/code&gt;&lt;/strong&gt;, along with a basic &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
 file. You can then add your source code, tests, and other files to this directory as needed. Once you've added your code, you can use &lt;strong&gt;&lt;code&gt;lerna add&lt;/code&gt;&lt;/strong&gt;to add the new package as a dependency of other packages in your monorepo.&lt;/p&gt;


&lt;h2&gt;
  
  
  lerna bootstrap
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;lerna bootstrap&lt;/code&gt;&lt;/strong&gt; is a command provided by Lerna that enables you to set up a development environment where packages depend on each other within a Lerna monorepo.&lt;/p&gt;

&lt;p&gt;When you run &lt;strong&gt;&lt;code&gt;lerna bootstrap&lt;/code&gt;&lt;/strong&gt;, Lerna installs all external dependencies of each package, hoisting dependencies to the root level where possible. It also symlinks packages that are dependencies of each other.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use &lt;strong&gt;&lt;code&gt;lerna bootstrap&lt;/code&gt;&lt;/strong&gt; in a Lerna monorepo:&lt;/p&gt;

&lt;p&gt;Let's say you have a Lerna monorepo with two packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;monorepo&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;lerna&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside &lt;strong&gt;&lt;code&gt;lerna.json&lt;/code&gt;&lt;/strong&gt;, you can define which packages should be linked and how:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;packages&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;packages/*&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;version&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.0.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, you can run &lt;strong&gt;&lt;code&gt;lerna bootstrap&lt;/code&gt;&lt;/strong&gt;to install all external dependencies of each package and create symlinks for interdependent packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;bootstrap&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will execute the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install all external dependencies for each package&lt;/li&gt;
&lt;li&gt;Symlink packages that depend on each other&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After running &lt;strong&gt;&lt;code&gt;lerna bootstrap&lt;/code&gt;&lt;/strong&gt;, you should have the following directory structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;monorepo&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="nx"&gt;node_modules&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="nx"&gt;node_modules&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;symlinked&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;node_modules&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="nx"&gt;node_modules&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;symlinked&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;node_modules&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;├──&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt;   &lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;span class="err"&gt;└──&lt;/span&gt; &lt;span class="nx"&gt;lerna&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can develop your packages as usual and Lerna will take care of the dependencies.&lt;/p&gt;

&lt;p&gt;💡 Hoisting is the process of moving common dependencies from packages to the root node_modules folder to reduce duplication and improve performance.&lt;/p&gt;




&lt;h2&gt;
  
  
  lerna add
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;lerna add&lt;/code&gt;&lt;/strong&gt; is a command provided by Lerna that allows you to add a dependency to one or more packages in your Lerna-managed monorepo. It automates the process of manually editing the &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt; files of each package to add the dependency.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use &lt;strong&gt;&lt;code&gt;lerna add&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Suppose you have a Lerna-managed monorepo with two packages: &lt;strong&gt;&lt;code&gt;package-a&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;package-b&lt;/code&gt;&lt;/strong&gt;. You want to add the &lt;strong&gt;&lt;code&gt;lodash&lt;/code&gt;&lt;/strong&gt; library as a dependency to &lt;strong&gt;&lt;code&gt;package-a&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your terminal and navigate to the root of your Lerna-managed monorepo.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the following command:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="nx"&gt;lodash&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This command adds the &lt;strong&gt;&lt;code&gt;lodash&lt;/code&gt;&lt;/strong&gt; library as a dependency to &lt;strong&gt;&lt;code&gt;package-a&lt;/code&gt;&lt;/strong&gt; and updates the &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt; file of &lt;strong&gt;&lt;code&gt;package-a&lt;/code&gt;&lt;/strong&gt;. It also installs &lt;strong&gt;&lt;code&gt;lodash&lt;/code&gt;&lt;/strong&gt; in &lt;strong&gt;&lt;code&gt;package-a&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you want to add &lt;strong&gt;&lt;code&gt;lodash&lt;/code&gt;&lt;/strong&gt; as a dev dependency, you can use the &lt;strong&gt;&lt;code&gt;--dev&lt;/code&gt;&lt;/strong&gt; flag:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="nx"&gt;lodash&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This will add &lt;strong&gt;&lt;code&gt;lodash&lt;/code&gt;&lt;/strong&gt; as a dev dependency to &lt;strong&gt;&lt;code&gt;package-a&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you want to add the same dependency to multiple packages at once, you can specify a comma-separated list of scopes:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="nx"&gt;lodash&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This will add &lt;strong&gt;&lt;code&gt;lodash&lt;/code&gt;&lt;/strong&gt; as a dependency to both &lt;strong&gt;&lt;code&gt;package-a&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;package-b&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;


💡 A "dev dependency" is a package or library that is only required for development and testing purposes, rather than for the production version of the software.

&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  lerna exec
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;lerna exec&lt;/code&gt;&lt;/strong&gt; is a command provided by Lerna that allows you to execute a command in each of the packages in your Lerna repository. This can be useful for running tests or other commands that need to be executed across all packages.&lt;/p&gt;

&lt;p&gt;The basic syntax for &lt;strong&gt;&lt;code&gt;lerna exec&lt;/code&gt;&lt;/strong&gt; is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;command&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will execute the specified command in each package directory. For example, if you wanted to run the &lt;strong&gt;&lt;code&gt;npm test&lt;/code&gt;&lt;/strong&gt;command in each package, you could use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use &lt;strong&gt;&lt;code&gt;{}&lt;/code&gt;&lt;/strong&gt;as a placeholder for the package directory name in the command. For example, if you wanted to create a new directory in each package directory, you could use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mkdir {}/new-directory&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would create a new &lt;strong&gt;&lt;code&gt;new-directory&lt;/code&gt;&lt;/strong&gt; directory in each package directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;lerna exec&lt;/code&gt;&lt;/strong&gt; also supports various options for controlling the behavior of the command. Some commonly used options include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-parallel&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;P&lt;/code&gt;&lt;/strong&gt;: Run the command in all packages in parallel.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-stream&lt;/code&gt;&lt;/strong&gt;: Stream output from each package as it completes instead of buffering it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-scope&lt;/code&gt;&lt;/strong&gt;: Only run the command in packages that match the given scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if you wanted to run the &lt;strong&gt;&lt;code&gt;npm install&lt;/code&gt;&lt;/strong&gt; command in all packages in parallel, you could use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;parallel&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use the &lt;strong&gt;&lt;code&gt;lerna exec&lt;/code&gt;&lt;/strong&gt;command with the &lt;strong&gt;&lt;code&gt;--scope&lt;/code&gt;&lt;/strong&gt;flag and a wildcard expression:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;package-*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will execute the &lt;strong&gt;&lt;code&gt;npm run test&lt;/code&gt;&lt;/strong&gt;command on all the packages that have a name starting with "package-". The &lt;strong&gt;&lt;code&gt;--scope&lt;/code&gt;&lt;/strong&gt;flag allows us to specify which packages we want to run the command on.&lt;/p&gt;

&lt;p&gt;Overall, &lt;strong&gt;&lt;code&gt;lerna exec&lt;/code&gt;&lt;/strong&gt;is a powerful command that can help you automate tasks across multiple packages in your Lerna repository.&lt;/p&gt;




&lt;h2&gt;
  
  
  lerna changed
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;lerna changed&lt;/code&gt;&lt;/strong&gt; command is used to identify which packages in a Lerna-managed repository have changed since the last tagged release, in preparation for publishing new versions of those packages. It compares the current state of the repository with the latest tagged release and identifies packages that have been modified or added.&lt;/p&gt;

&lt;p&gt;Here is an example of using &lt;strong&gt;&lt;code&gt;lerna changed&lt;/code&gt;&lt;/strong&gt; command:&lt;/p&gt;

&lt;p&gt;Suppose you have a Lerna-managed repository with two packages named &lt;strong&gt;&lt;code&gt;package-1&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;package-2&lt;/code&gt;&lt;/strong&gt;. You have made some changes to &lt;strong&gt;&lt;code&gt;package-1&lt;/code&gt;&lt;/strong&gt; since the last tagged release, and you want to see which packages have changed in the repository. You can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;changed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output a list of packages that have changed since the last tagged release, in this case &lt;strong&gt;&lt;code&gt;package-1&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can also use the &lt;strong&gt;&lt;code&gt;--json&lt;/code&gt;&lt;/strong&gt; flag to output the result as a JSON object, which can be used by other tools:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;lerna&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;changed&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;--json&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output a JSON object with the list of packages that have changed.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;&lt;code&gt;lerna changed&lt;/code&gt;&lt;/strong&gt;can be used in a Continuous Integration (CI) or Continuous Deployment (CD) pipeline to trigger builds or deployments only for the packages that have changed, instead of rebuilding or redeploying everything.&lt;/p&gt;




&lt;h2&gt;
  
  
  lerna diff
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;lerna diff&lt;/code&gt;&lt;/strong&gt; is a Lerna command that shows the difference between the current repository state and the last release of the package(s).&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;lerna diff&lt;/code&gt;&lt;/strong&gt; command outputs a list of the packages that have changed since the last release, along with the type of change that has been made. The types of changes include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;changed&lt;/code&gt;&lt;/strong&gt;: the package has changed but it doesn't include changes to its dependencies&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;new&lt;/code&gt;&lt;/strong&gt;: a new package has been added&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;upgraded&lt;/code&gt;&lt;/strong&gt;: the package has upgraded one of its dependencies&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;downgraded&lt;/code&gt;&lt;/strong&gt;: the package has downgraded one of its dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if you run &lt;strong&gt;&lt;code&gt;lerna diff&lt;/code&gt;&lt;/strong&gt;, and there have been changes made to two packages since the last release, the output might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;lerna notice cli v3.22.1
lerna info Looking &lt;span class="k"&gt;for &lt;/span&gt;changed packages since v1.0.0
lerna info found 2 packages to publish
lerna info diffing packages
lerna info comparing to previous version v1.0.0
my-package-1             CHANGED
my-package-2             UPGRADED &lt;span class="o"&gt;(&lt;/span&gt;from 1.0.0 to 1.1.0&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This indicates that there have been changes made to &lt;strong&gt;&lt;code&gt;my-package-1&lt;/code&gt;&lt;/strong&gt;, and an upgrade made to&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;my-package-2&lt;/code&gt;&lt;/strong&gt;since the last release.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;&lt;code&gt;lerna changed&lt;/code&gt;&lt;/strong&gt;is used to identify which packages have changed, while &lt;strong&gt;&lt;code&gt;lerna diff&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
 is used to see the specific changes made to each package.&lt;/p&gt;




&lt;h2&gt;
  
  
  lerna publish
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;lerna publish&lt;/code&gt;&lt;/strong&gt; is a command in Lerna that helps you publish new versions of packages to the npm registry. It automates many of the tasks associated with publishing multiple packages, including updating version numbers, creating and pushing git tags, and publishing to the npm registry.&lt;/p&gt;

&lt;p&gt;Here are the steps involved in using &lt;strong&gt;&lt;code&gt;lerna publish&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, run &lt;strong&gt;&lt;code&gt;lerna changed&lt;/code&gt;&lt;/strong&gt; to see which packages have changed since the last release.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;&lt;code&gt;lerna version&lt;/code&gt;&lt;/strong&gt; to bump the version number of each changed package, update the changelog, create a git tag, and commit the changes.&lt;/li&gt;
&lt;li&gt;Finally, run &lt;strong&gt;&lt;code&gt;lerna publish&lt;/code&gt;&lt;/strong&gt; to publish the new versions of the packages to the npm registry.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;During the &lt;strong&gt;&lt;code&gt;lerna publish&lt;/code&gt;&lt;/strong&gt;process, you will be prompted to confirm the versions you want to publish, and you will have the option to choose a custom tag for the release (such as "next" or "beta").&lt;/p&gt;

&lt;h3&gt;
  
  
  What is &lt;strong&gt;conventional-commits ?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Conventional commit messages are a standardized way of structuring commit messages that provide more context and semantic meaning.&lt;/p&gt;

&lt;p&gt;The basic structure of a conventional commit message is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;[optional scope]: &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;description&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

[optional body]

[optional footer]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, a commit message for a feature implementation might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;feat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;login&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="nx"&gt;forgot&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="nx"&gt;feature&lt;/span&gt;

&lt;span class="nx"&gt;Add&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;forgot password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;link&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;login&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;allowing&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;reset&lt;/span&gt; &lt;span class="nx"&gt;their&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;they&lt;/span&gt; &lt;span class="nx"&gt;forget&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using conventional commits, you can generate a changelog automatically based on the commit messages, making it easier to track changes and releases.&lt;/p&gt;

&lt;p&gt;In conventional commits, &lt;strong&gt;&lt;code&gt;&amp;lt;type&amp;gt;&lt;/code&gt;&lt;/strong&gt; represents the nature of the changes made in the commit. This can be one of the following types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;feat&lt;/code&gt;&lt;/strong&gt;: A new feature.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;fix&lt;/code&gt;&lt;/strong&gt;: A bug fix.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;docs&lt;/code&gt;&lt;/strong&gt;: Documentation changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;style&lt;/code&gt;&lt;/strong&gt;: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;refactor&lt;/code&gt;&lt;/strong&gt;: A code change that neither fixes a bug nor adds a feature.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;perf&lt;/code&gt;&lt;/strong&gt;: A code change that improves performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;test&lt;/code&gt;&lt;/strong&gt;: Adding missing tests or correcting existing tests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;build&lt;/code&gt;&lt;/strong&gt;: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ci&lt;/code&gt;&lt;/strong&gt;: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;chore&lt;/code&gt;&lt;/strong&gt;: Other changes that don't modify src or test files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;revert&lt;/code&gt;&lt;/strong&gt;: Reverts a previous commit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;[optional scope]&lt;/code&gt;&lt;/strong&gt; is used to provide additional context about the changes being made, such as the affected component or module. It is enclosed in square brackets and is optional.&lt;/p&gt;

&lt;p&gt;There is a awesome library know as &lt;strong&gt;&lt;code&gt;commitizen&lt;/code&gt;&lt;/strong&gt; that will help you generate the conventional commit. Please do check that out !&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;lerna publish&lt;/code&gt;&lt;/strong&gt; supports many other options as well, such as publishing only to a specific registry, skipping certain packages, and specifying a custom git commit message.&lt;/p&gt;

&lt;p&gt;Here are all the options available for &lt;strong&gt;&lt;code&gt;lerna publish&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-canary&lt;/code&gt;&lt;/strong&gt;: creates a canary release, which means that it will publish a release with a version suffix such as &lt;strong&gt;&lt;code&gt;alpha.X&lt;/code&gt;&lt;/strong&gt;. This is useful when you want to test a release before publishing it to the public registry.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-conventional-commits&lt;/code&gt;&lt;/strong&gt;: uses the conventional commits specification to determine the new version number based on the commit messages since the last release.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-force-publish&lt;/code&gt;&lt;/strong&gt;: publishes all packages, even if they haven't changed since the last release. You can specify which packages to force publish by providing a comma-separated list of package names.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-dist-tag&lt;/code&gt;&lt;/strong&gt;: the tag that will be used to identify the package when installing it. The default value is "latest".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-exact&lt;/code&gt;&lt;/strong&gt;: forces the package version to be published exactly as it appears in the &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt; file, without resolving the version to the latest semver-compatible version.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-skip-git&lt;/code&gt;&lt;/strong&gt;: skips the Git commit and tag creation process, meaning that no Git operations will be performed during the release.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-skip-npm&lt;/code&gt;&lt;/strong&gt;: skips the actual npm publish step, meaning that no packages will be published to the npm registry.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-preid&lt;/code&gt;&lt;/strong&gt;: specifies the pre-release identifier for the version. For example, if you specify &lt;strong&gt;&lt;code&gt;-preid alpha&lt;/code&gt;&lt;/strong&gt;, the version will be something like &lt;strong&gt;&lt;code&gt;1.0.0-alpha.0&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-registry&lt;/code&gt;&lt;/strong&gt;: the URL of the npm registry to use for publishing the packages. By default, it uses the public npm registry at &lt;strong&gt;&lt;a href="https://registry.npmjs.org/"&gt;https://registry.npmjs.org/&lt;/a&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-message&lt;/code&gt;&lt;/strong&gt;: the message to use when creating the Git tag for the release. By default, it uses the release type and version number.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example that uses several of these options:&lt;/p&gt;

&lt;p&gt;These options can be used in combination with each other to achieve the desired result. For example, you can use &lt;strong&gt;&lt;code&gt;--skip-git --skip-npm --skip-changelog&lt;/code&gt;&lt;/strong&gt; to quickly generate a new version number without committing any changes or publishing to npm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;publish&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;canary&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;conventional&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;commits&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;skip&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;git&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;skip&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;registry&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//my-registry.com --dist-tag next&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will create a canary release with a version suffix, using the conventional commits specification to determine the new version number. It will skip the Git commit and tag creation process, as well as the actual npm publish step. It will publish the package to a private registry at &lt;strong&gt;&lt;a href="https://my-registry.com/"&gt;https://my-registry.com&lt;/a&gt;&lt;/strong&gt;, and use the "next" dist-tag to identify the package when installing it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Package release lifecycle ?
&lt;/h3&gt;

&lt;p&gt;The package release lifecycle refers to the various stages that a software package goes through from its initial development to its final release. The lifecycle can be broken down into several phases, each of which serves a specific purpose. The phases typically include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Development:&lt;/strong&gt; This is the phase where the software package is being actively developed. During this phase, developers write code, test it, and make changes as needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alpha testing:&lt;/strong&gt; Once the initial development is complete, the package may be released to a limited group of testers for alpha testing. This testing is designed to catch any major bugs or issues before the package is released to a wider audience.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Beta testing:&lt;/strong&gt; After alpha testing is complete, the package may be released to a larger group of beta testers. Beta testing is designed to catch any remaining bugs and to gather feedback from users.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Release candidate:&lt;/strong&gt; Once beta testing is complete and all major bugs have been fixed, the package may be designated as a release candidate. This version is considered to be stable and is intended to be the final release unless any critical issues are found.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Final release:&lt;/strong&gt; After the release candidate has been thoroughly tested and any remaining issues have been fixed, the package is released to the public as a final, stable version.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Throughout the release lifecycle, developers may also release patches or minor updates to the software package to address any bugs or issues that are found. These updates typically follow the same lifecycle process as the initial release, with alpha and beta testing before the final release.&lt;/p&gt;

&lt;p&gt;Here are the steps to use Lerna to publish in different release scenarios:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Alpha release:&lt;/strong&gt; If you want to publish an alpha release, you can use the &lt;strong&gt;&lt;code&gt;--dist-tag&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
flag to specify a &lt;strong&gt;&lt;code&gt;alpha&lt;/code&gt;&lt;/strong&gt;tag for your package. This will let users install your package with the &lt;strong&gt;&lt;code&gt;@your-package-name@alpha&lt;/code&gt;&lt;/strong&gt;tag.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;publish&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;dist&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="nx"&gt;alpha&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Beta release:&lt;/strong&gt; Similarly, if you want to publish a beta release, you can use the &lt;strong&gt;&lt;code&gt;--dist-tag&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
flag to specify a &lt;strong&gt;&lt;code&gt;beta&lt;/code&gt;&lt;/strong&gt;tag for your package. This will let users install your package with the &lt;strong&gt;&lt;code&gt;@your-package-name@beta&lt;/code&gt;&lt;/strong&gt;tag.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;publish&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;dist&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="nx"&gt;beta&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Release candidate (RC) release:&lt;/strong&gt; To publish an RC release, you can use the &lt;strong&gt;&lt;code&gt;--dist-tag&lt;/code&gt;&lt;/strong&gt;flag to specify a &lt;strong&gt;&lt;code&gt;rc&lt;/code&gt;&lt;/strong&gt;tag for your package. This will let users install your package with the &lt;strong&gt;&lt;code&gt;@your-package-name@rc&lt;/code&gt;&lt;/strong&gt;tag.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;publish&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;dist&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="nx"&gt;rc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Production (stable) release:&lt;/strong&gt; Finally, when you are ready to publish a stable release of your package, you can simply run &lt;strong&gt;&lt;code&gt;lerna publish&lt;/code&gt;&lt;/strong&gt;without any flags. This will publish the latest version of your package to the default &lt;strong&gt;&lt;code&gt;latest&lt;/code&gt;&lt;/strong&gt;tag. This will let users install your package with the &lt;strong&gt;&lt;code&gt;@your-package-name@latest&lt;/code&gt;&lt;/strong&gt;tag or &lt;strong&gt;&lt;code&gt;@your-package-name&lt;/code&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;publish&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



💡 If you also want to suffix your version number with release tag then use the flag **`-preid`**

&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  lerna run
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;lerna run&lt;/code&gt;&lt;/strong&gt; is a command provided by Lerna that allows running npm scripts across packages in the monorepo. It enables running scripts in a specific package, running scripts for all packages, or running scripts in a subset of packages that match a specific glob pattern.&lt;/p&gt;

&lt;p&gt;The basic syntax of &lt;strong&gt;&lt;code&gt;lerna run&lt;/code&gt;&lt;/strong&gt; command is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;packageGlob&lt;/span&gt;&lt;span class="p"&gt;...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;strong&gt;&lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;&lt;/strong&gt; is the name of the npm script to run, &lt;strong&gt;&lt;code&gt;--flags&lt;/code&gt;&lt;/strong&gt; are any flags passed to the npm command, and &lt;strong&gt;&lt;code&gt;packageGlob...&lt;/code&gt;&lt;/strong&gt; is an optional list of glob patterns that filter the packages where the script should be executed.&lt;/p&gt;

&lt;p&gt;Here's an example of using &lt;strong&gt;&lt;code&gt;lerna run&lt;/code&gt;&lt;/strong&gt; to run the &lt;strong&gt;&lt;code&gt;build&lt;/code&gt;&lt;/strong&gt; script in all packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="nx"&gt;build&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will run the &lt;strong&gt;&lt;code&gt;build&lt;/code&gt;&lt;/strong&gt;script in all packages in the monorepo. You can also pass additional flags to the npm command, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="nx"&gt;build&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;prod&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will pass the &lt;strong&gt;&lt;code&gt;--prod&lt;/code&gt;&lt;/strong&gt; flag to the npm command, which could be used by the build script to create a production build.&lt;/p&gt;

&lt;p&gt;You can also use glob patterns to run the script only in certain packages, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="nx"&gt;build&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kr"&gt;package&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;prod&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will run the &lt;strong&gt;&lt;code&gt;build&lt;/code&gt;&lt;/strong&gt;script for only the &lt;strong&gt;&lt;code&gt;my-package&lt;/code&gt;&lt;/strong&gt;package and pass the &lt;strong&gt;&lt;code&gt;--prod&lt;/code&gt;&lt;/strong&gt;flag to the script.&lt;/p&gt;

&lt;p&gt;In addition to the &lt;strong&gt;&lt;code&gt;--scope&lt;/code&gt;&lt;/strong&gt; flag, &lt;strong&gt;&lt;code&gt;lerna run&lt;/code&gt;&lt;/strong&gt; supports other options such as &lt;strong&gt;&lt;code&gt;--parallel&lt;/code&gt;&lt;/strong&gt; to run scripts in parallel, &lt;strong&gt;&lt;code&gt;--stream&lt;/code&gt;&lt;/strong&gt; to stream output from multiple processes, and &lt;strong&gt;&lt;code&gt;--no-bail&lt;/code&gt;&lt;/strong&gt; to continue running scripts even if one fails.&lt;/p&gt;

&lt;p&gt;Here's an example of running the &lt;strong&gt;&lt;code&gt;test&lt;/code&gt;&lt;/strong&gt; script in all packages in parallel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;parallel&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will run the &lt;strong&gt;&lt;code&gt;test&lt;/code&gt;&lt;/strong&gt;script in all packages at the same time, which can speed up the build process if the packages are independent of each other.&lt;/p&gt;




&lt;h2&gt;
  
  
  lerna clean
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;lerna clean&lt;/code&gt;&lt;/strong&gt; is a command provided by Lerna that removes the &lt;strong&gt;&lt;code&gt;node_modules&lt;/code&gt;&lt;/strong&gt; directory from all packages in the Lerna monorepo. It is useful when you want to clean up the dependencies and start fresh.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;lerna clean&lt;/code&gt;&lt;/strong&gt; command has the following options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-yes&lt;/code&gt;&lt;/strong&gt;: skips the confirmation prompt and cleans the packages without confirmation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-ignore&lt;/code&gt;&lt;/strong&gt;: a comma-separated list of package names that should be ignored during the cleaning process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-nohoist&lt;/code&gt;&lt;/strong&gt;: prevents Lerna from hoisting the dependencies while cleaning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example:&lt;/p&gt;

&lt;p&gt;Suppose you have a Lerna monorepo with two packages named &lt;strong&gt;&lt;code&gt;package-a&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;package-b&lt;/code&gt;&lt;/strong&gt;, and you want to clean them up.&lt;/p&gt;

&lt;p&gt;To clean up all the packages in the monorepo, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;clean&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to clean up only &lt;strong&gt;&lt;code&gt;package-a&lt;/code&gt;&lt;/strong&gt;and ignore &lt;strong&gt;&lt;code&gt;package-b&lt;/code&gt;&lt;/strong&gt;, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;clean&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;ignore&lt;/span&gt; &lt;span class="kr"&gt;package&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to skip the confirmation prompt and clean up all packages in the monorepo, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;clean&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;yes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to prevent Lerna from hoisting the dependencies while cleaning, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;lerna&lt;/span&gt; &lt;span class="nx"&gt;clean&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;nohoist&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Hoisting is a technique used by package managers like npm and Yarn to optimize the installation process by reducing the number of duplicated packages. It involves moving packages that are used by multiple dependencies to a higher level in the dependency tree, so they can be shared by all packages that need them, rather than being duplicated in each package's node_modules folder. This helps to reduce the overall size of the node_modules folder and improve installation times.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Follow me
&lt;/h2&gt;

&lt;p&gt;You can follow me on &lt;a href="https://twitter.com/Prasenjeetsymon"&gt;Twitter&lt;/a&gt;, &lt;a href="https://github.com/prasenjeet-symon"&gt;GitHub&lt;/a&gt;, and &lt;a href="https://www.instagram.com/prasenjeetkr5/"&gt;Instagram&lt;/a&gt; to stay updated on my latest projects and musings.&lt;/p&gt;

&lt;p&gt;Let's connect and share our passion for tech and development!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
