summaryrefslogtreecommitdiff
path: root/devdocs/docker/compose%2Fprofiles%2Findex.html
blob: 04f317b44479077c9561cfae3122f81fbf8357d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<h1>Using profiles with Compose</h1>

<p>Profiles allow adjusting the Compose application model for various usages and environments by selectively enabling services. This is achieved by assigning each service to zero or more profiles. If unassigned, the service is <em>always</em> started but if assigned, it is only started if the profile is activated.</p> <p>This allows one to define additional services in a single <code class="language-plaintext highlighter-rouge">docker-compose.yml</code> file that should only be started in specific scenarios, e.g. for debugging or development tasks.</p> <h2 id="assigning-profiles-to-services">Assigning profiles to services</h2> <p>Services are associated with profiles through the <a href="../compose-file/compose-file-v3/index#profiles"><code class="language-plaintext highlighter-rouge">profiles</code> attribute</a> which takes an array of profile names:</p> <div class="highlight"><pre class="highlight" data-language="">version: "3.9"
services:
  frontend:
    image: frontend
    profiles: ["frontend"]

  phpmyadmin:
    image: phpmyadmin
    depends_on:
      - db
    profiles:
      - debug

  backend:
    image: backend

  db:
    image: mysql
</pre></div> <p>Here the services <code class="language-plaintext highlighter-rouge">frontend</code> and <code class="language-plaintext highlighter-rouge">phpmyadmin</code> are assigned to the profiles <code class="language-plaintext highlighter-rouge">frontend</code> and <code class="language-plaintext highlighter-rouge">debug</code> respectively and as such are only started when their respective profiles are enabled.</p> <p>Services without a <code class="language-plaintext highlighter-rouge">profiles</code> attribute will <em>always</em> be enabled, i.e. in this case running <code class="language-plaintext highlighter-rouge">docker-compose up</code> would only start <code class="language-plaintext highlighter-rouge">backend</code> and <code class="language-plaintext highlighter-rouge">db</code>.</p> <p>Valid profile names follow the regex format of <code class="language-plaintext highlighter-rouge">[a-zA-Z0-9][a-zA-Z0-9_.-]+</code>.</p> <blockquote> <p><strong>Note</strong></p> <p>The core services of your application should not be assigned <code class="language-plaintext highlighter-rouge">profiles</code> so they will always be enabled and automatically started.</p> </blockquote> <h2 id="enabling-profiles">Enabling profiles</h2> <p>To enable a profile supply the <code class="language-plaintext highlighter-rouge">--profile</code> <a href="../reference/index">command-line option</a> or use the <a href="../reference/envvars/index#compose_profiles"><code class="language-plaintext highlighter-rouge">COMPOSE_PROFILES</code> environment variable</a>:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker-compose --profile debug up
$ COMPOSE_PROFILES=debug docker-compose up
</pre></div> <p>The above command would both start your application with the <code class="language-plaintext highlighter-rouge">debug</code> profile enabled. Using the <code class="language-plaintext highlighter-rouge">docker-compose.yml</code> file above, this would start the services <code class="language-plaintext highlighter-rouge">backend</code>, <code class="language-plaintext highlighter-rouge">db</code> and <code class="language-plaintext highlighter-rouge">phpmyadmin</code>.</p> <p>Multiple profiles can be specified by passing multiple <code class="language-plaintext highlighter-rouge">--profile</code> flags or a comma-separated list for the <code class="language-plaintext highlighter-rouge">COMPOSE_PROFILES</code> environment variable:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker-compose --profile frontend --profile debug up
$ COMPOSE_PROFILES=frontend,debug docker-compose up
</pre></div> <h2 id="auto-enabling-profiles-and-dependency-resolution">Auto-enabling profiles and dependency resolution</h2> <p>When a service with assigned <code class="language-plaintext highlighter-rouge">profiles</code> is explicitly targeted on the command line its profiles will be enabled automatically so you don’t need to enable them manually. This can be used for one-off services and debugging tools. As an example consider this configuration:</p> <div class="highlight"><pre class="highlight" data-language="">version: "3.9"
services:
  backend:
    image: backend

  db:
    image: mysql

  db-migrations:
    image: backend
    command: myapp migrate
    depends_on:
      - db
    profiles:
      - tools
</pre></div> <div class="highlight"><pre class="highlight" data-language=""># will only start backend and db
$ docker-compose up -d

# this will run db-migrations (and - if necessary - start db)
# by implicitly enabling profile `tools`
$ docker-compose run db-migrations
</pre></div> <p>But keep in mind that <code class="language-plaintext highlighter-rouge">docker-compose</code> will only automatically enable the profiles of the services on the command line and not of any dependencies. This means that all services the targeted service <code class="language-plaintext highlighter-rouge">depends_on</code> must have a common profile with it, be always enabled (by omitting <code class="language-plaintext highlighter-rouge">profiles</code>) or have a matching profile enabled explicitly:</p> <div class="highlight"><pre class="highlight" data-language="">version: "3.9"
services:
  web:
    image: web

  mock-backend:
    image: backend
    profiles: ["dev"]
    depends_on:
      - db

  db:
    image: mysql
    profiles: ["dev"]

  phpmyadmin:
    image: phpmyadmin
    profiles: ["debug"]
    depends_on:
      - db
</pre></div> <div class="highlight"><pre class="highlight" data-language=""># will only start "web"
$ docker-compose up -d

# this will start mock-backend (and - if necessary - db)
# by implicitly enabling profile `dev`
$ docker-compose up -d mock-backend

# this will fail because profile "dev" is disabled
$ docker-compose up phpmyadmin
</pre></div> <p>Although targeting <code class="language-plaintext highlighter-rouge">phpmyadmin</code> will automatically enable its profiles - i.e. <code class="language-plaintext highlighter-rouge">debug</code> - it will not automatically enable the profile(s) required by <code class="language-plaintext highlighter-rouge">db</code> - i.e. <code class="language-plaintext highlighter-rouge">dev</code>. To fix this you either have to add the <code class="language-plaintext highlighter-rouge">debug</code> profile to the <code class="language-plaintext highlighter-rouge">db</code> service:</p> <div class="highlight"><pre class="highlight" data-language="">db:
  image: mysql
  profiles: ["debug", "dev"]
</pre></div> <p>or enable a profile of <code class="language-plaintext highlighter-rouge">db</code> explicitly:</p> <div class="highlight"><pre class="highlight" data-language=""># profile "debug" is enabled automatically by targeting phpmyadmin
$ docker-compose --profile dev up phpmyadmin
$ COMPOSE_PROFILES=dev docker-compose up phpmyadmin
</pre></div> <h2 id="compose-documentation">Compose documentation</h2> <ul> <li><a href="../index">User guide</a></li> <li><a href="../install/index">Installing Compose</a></li> <li><a href="../gettingstarted/index">Getting Started</a></li> <li><a href="../reference/index">Command line reference</a></li> <li><a href="../compose-file/index">Compose file reference</a></li> <li><a href="../samples-for-compose/index">Sample apps with Compose</a></li> </ul> 
<p><a href="https://docs.docker.com/search/?q=cli">cli</a>, <a href="https://docs.docker.com/search/?q=compose">compose</a>, <a href="https://docs.docker.com/search/?q=profile">profile</a>, <a href="https://docs.docker.com/search/?q=profiles%20reference">profiles reference</a></p>
<div class="_attribution">
  <p class="_attribution-p">
    &copy; 2019 Docker, Inc.<br>Licensed under the Apache License, Version 2.0.<br>Docker and the Docker logo are trademarks or registered trademarks of Docker, Inc. in the United States and/or other countries.<br>Docker, Inc. and other parties may also have trademark rights in other terms used herein.<br>
    <a href="https://docs.docker.com/compose/profiles/" class="_attribution-link">https://docs.docker.com/compose/profiles/</a>
  </p>
</div>