docker-compose以root身份执行卷脚本

时间:2018-12-19 07:42:53

标签: docker centos docker-compose percona

我有这样的docker-compose.yml文件:

version: '3'

services:
  db:
    #build: db
    image: percona:5.7.24-centos
    ports:
      - '3306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: pass
      MYSQL_DATABASE: bc
      MYSQL_PASSWORD: pass
    volumes:
      - ./db:/docker-entrypoint-initdb.d

并且脚本是f.e。:

mkdir /home/workdirectory/

该图像中没有sudo

默认用户为mysql

初始位置仅为/

那么我该如何在./db内以脚本根目录的身份执行脚本?

2 个答案:

答案 0 :(得分:0)

您可以从percona:5.7.24-centos继承自己的docker映像并切换用户或安装sudo。或者只是在Dockerfile中创建必要的目录。

答案 1 :(得分:0)

I'd suggest you use the following assuming your script is a bash script and it's placed inside the db folder:

script.sh

#!/bin/bash
mkdir -p /home/workdirectory/some-sub-folder/

then make sure your container is up and running by executing docker-compose up -d

then use the following command to execute some script from inside the container:

docker exec db /docker-entrypoint-initdb.d/script.sh

Link:

https://docs.docker.com/engine/reference/commandline/exec/