<p>I want to set initial data on MySQL of container.
In docker-compose.yml, such code can create initial data when running container.</p>

<pre><code>volumes:
  - db:/var/lib/mysql
  - "./docker/mysql/conf.d:/etc/mysql/conf.d"
  - "./docker/mysql/init.d:/docker-entrypoint-initdb.d"
</code></pre>

<p>However, how can I create initial data on Kubernetes when running?</p>

<p>According to the MySQL Docker image <a href="https://hub.docker.com/_/mysql/" rel="noreferrer">README</a>, the part that is relevant to data initialization on container start-up is to ensure all your initialization files are mount to the container's <code>/docker-entrypoint-initdb.d</code> folder.</p>
<p>You can define your initial data in a <code>ConfigMap</code>, and mount the corresponding volume in your pod like this:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: mysql
spec:
  containers:
  - name: mysql
    image: mysql        
    ports:
      - containerPort: 3306
    volumeMounts:
      - name: mysql-initdb
        mountPath: /docker-entrypoint-initdb.d
  volumes:
    - name: mysql-initdb
      configMap:
        name: mysql-initdb-config
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-initdb-config
data:
  initdb.sql: |
    CREATE TABLE friends (id INT, name VARCHAR(256), age INT, gender VARCHAR(3));
    INSERT INTO friends VALUES (1, 'John Smith', 32, 'm');
    INSERT INTO friends VALUES (2, 'Lilian Worksmith', 29, 'f');
    INSERT INTO friends VALUES (3, 'Michael Rupert', 27, 'm');
</code></pre>
