title: Editing docker-compose.yml with PyYAML
body: <p>I have a pretty standard docker-compose.yml and I need to edit the password of the database programmatically.</p>
  
  <p>Since its a YAML file, I thought it would be simple to edit and dump the content.
  So far I tried the PyYAML and it just mess the docker-compose file and I don't know why.</p>
  
  <p>Loading and dumping the same content, it breaks the structure.</p>
  
  <p>Content of the docker-compose.yml:</p>
  
  <pre><code>version: '2'
  services:
    web:
      container_name: xxx
      ports:
       - "80:80"
      volumes:
        - .:/xxx
      depends_on:
        - mysql
      build: .
    mysql:
      ports:
       - "32768:32768"
       - "3306:3306"
      container_name: xxx-mysql
      restart: always
      image: mariadb:latest
      environment:
        MYSQL_ROOT_PASSWORD: 'thiswillbechangeonsetupscript'
        MYSQL_DATABASE: 'xxxdb'
      volumes:
       - ./database:/var/lib/mysql
      ports:
        - "3306:3306"
  </code></pre>
  
  <p>This is how I'm loading and dumping the content:</p>
  
  <pre><code>import yaml
  
  with open("docker-compose.yml", 'r') as ymlfile:
      docker_config = yaml.load(ymlfile)
  
  with open("docker-compose.yml", 'w') as newconf:
      yaml.dump(docker_config, newconf)
  </code></pre>
  
  <p>And this is how the file is saved.</p>
  
  <pre><code>services:
    mysql:
      container_name: xxx-mysql
      environment: {MYSQL_DATABASE: xxxdb, MYSQL_ROOT_PASSWORD: thiswillbechangeonsetupscript}
      image: mariadb:latest
      ports: ['3306:3306']
      restart: always
      volumes: ['./database:/var/lib/mysql']
    web:
      build: .
      container_name: xxx
      depends_on: [mysql]
      ports: ['80:80']
      volumes: ['.:/xxx']
  version: '2'
  </code></pre>
  
  <p>Is there any better way to do this?! What I'm missing?</p>