<p>I have a docker container that maps a user's local directory so that it becomes a persistent volume.  The directory path is customizable via an environment variable "MYSQL_DATA_FOLDER".</p>

<p>This works fine when running the docker container locally, however, when launching the container through minikube/kubernetes, this approach no longer works</p>

<p>When starting this container using only docker, I would use this command:</p>

<pre><code>docker-compose run --service-ports --volume=$MYSQL_DATA_FOLDER:/var/lib/mysql --name db --rm database
</code></pre>

<p>Here's the docker-compose.yml file</p>

<pre><code>version: '3.5'
services:
  database:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: ""
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
    volumes:
      - mydatavolume:${MYSQL_DATA_FOLDER}
    build:
      context: .
      dockerfile: Dockerfile

volumes:
  mydatavolume:

networks:
  default:
    name: dev_network
    driver: bridge
</code></pre>

<p>Here's the environment variable file (env-variables.env):</p>

<pre><code>MYSQL_ALLOW_EMPTY_PASSWORD=yes
MYSQL_ROOT_PASSWORD=
MYSQL_DATA_FOLDER=/Users/foo/mysql/data
</code></pre>

<p>I created a ConfigMap with kubernetes using this command:</p>

<pre><code>kubectl create configmap db-env --from-env-file=env-variables.env 
</code></pre>

<p>Here is the deployment.yml file:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: db-mysql
  labels:
    app: db
spec:
  ports:
    - port: 3306
  selector:
    app: db
    tier: mysql
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: db
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: db-mysql
  labels:
    app: db
spec:
  selector:
    matchLabels:
      app: db
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: db
        tier: mysql
    spec:
      containers:
      - image: mysql
        name: mysql
        envFrom:
          - configMapRef:
              name: db-env
        livenessProbe:
          tcpSocket:
            port: 3306
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: ${MYSQL_DATA_FOLDER}
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
</code></pre>

<p>What I want to do is somehow populate the mountPath value using the environment variable MYSQL_DATA_FOLDER so it can be user configurable.</p>

<p>I want to be able to "git pull" and go without having to do any post/pre-processing on the files.</p>

<p>Thanks for any clues</p>

slashdottir
<p>You can do it like below:</p>

<pre><code>source env.file &amp;&amp; cat deployment.yml  | sed "s/{{MYSQL_DATA_FOLDER}}/$MYSQL_DATA_FOLDER/g" | kubectl apply -f -
</code></pre>

<p>And your yaml file variable like:</p>

<pre><code>...
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: {{MYSQL_DATA_FOLDER}}
...
</code></pre>
