Skip to content

Instantly share code, notes, and snippets.

@danhyun
Last active June 11, 2018 09:39
Show Gist options
  • Save danhyun/650644ecc7da56737d7a to your computer and use it in GitHub Desktop.
Save danhyun/650644ecc7da56737d7a to your computer and use it in GitHub Desktop.
Configure `Dockermachine` and Windows to share folders
function mysqlUrl {
local ip=`docker-machine ip default`
local port=`docker inspect --format='{{(index (index .NetworkSettings.Ports "3306/tcp") 0).HostPort}}' mysql`
echo "$ip:$port"
}

Sharing folders between Dockermachine/Docker and Windows

Add shared folders and configure docker-machine

Add shared folders
docker-machine stop default

/c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe  sharedfolder add default --name apps --hostpath 'c:\apps' --automount
/c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe  sharedfolder add default --name mysql-data --hostpath 'C:\mysql\mysql-5.6.21-winx64\data' --automount

docker-machine start default
ssh into Dockermachine and mount your folders
sudo mkdir -p /apps
sudo mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` apps /apps

sudo mkdir -p /opt/data
sudo mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` mysql-data /opt/data

{\
  echo 'mkdir -p /apps'; \
  echo 'mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` apps /apps'; \
  echo 'mkdir -p /opt/data'; \
  echo 'mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` mysql-data /opt/data'; \
} | sudo tee /mnt/sda1/var/lib/boot2docker/bootlocal.sh

Verify docker-machine can see mounted folders

Test that mounting from Dockermachine works
docker run -it -v //opt/data:/apps --rm ubuntu ls -ltra //apps

Build MySQL container and use existing data directory as datadir

Dockerfile
FROM mysql:5.6

RUN {\
  echo 'max_allowed_packet=64M'; \
  echo 'innodb_log_file_size=256M'; \
} >> /etc/mysql/my.cnf
Build from Dockerfile
docker build -t msvc/mysql .
Start container from Dockerfile
docker run --name mysql -v //opt/data:/var/lib/mysql -d -p 3306:3306 msvc/mysql
Verify you can connect to mysql
mysql -u root -p -h $(docker-machine ip default)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment