Run MSSQL server in docker inside vagrant (ubuntu) VM

Files

Vagrant VM needs at least 2Gb.

## Vagrantfile
Vagrant.configure("2") do |config|
    config.vm.box = "ubuntu/focal64" # v 20.04 LTS
    config.vm.synced_folder "./", "/vagrant"
    config.vm.network "forwarded_port", guest: 1433, host: 1433
    config.vm.provision :shell, path: "vagrant_provision.sh"  
    config.vm.provider "virtualbox" do |vb|
      vb.name = "SqlServer"     
      vb.memory = 2048
    end
  end
## vagrant_provision.sh 
echo "provisioning VM..."
sudo apt update
sudo apt-get update

# install docker and run
sudo apt -y install docker.io
sudo systemctl start docker
sudo systemctl enable docker
docker --version
echo "docker installed and running..."

# create group 'docker' and add user 'vagrant' (the one I connect with) to it
sudo groupadd docker
sudo usermod -aG docker vagrant
echo "docker group created and 'vagrant' user added to it..."

# install docker compose
sudo curl -L https://github.com/docker/compose/releases/download/1.26.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
echo "docker compose installed..."

echo "done provisioning VM."
# docker-compose.yaml
version: '3.7'
services:
  sqlserver:
    image: mcr.microsoft.com/mssql/server:2017-latest
    container_name: mssql
    ports:
      - '1433:1433'
    volumes:
      - mssqldata:/var/opt/mssql
    environment:
      - ACCEPT_EULA=Yes
      - SA_PASSWORD=StrongP@ssword!
      - MSSQL_PID=Developer
volumes:
  mssqldata:

Run SQL server and connect

I’m using volumes, since binding data folder fails. ¿¡!?

vagrant up
vagrant ssh
docker-compose up -d

The alternative command would be:

docker run --name mssql
 -e ACCEPT_EULA=Y \
        -e MSSQL_PID="Developer" \
        -e MSSQL_SA_PASSWORD="StrongP@ssword!" \
        -v mssqldata:/var/opt/mssql \
        -p 1433:1433 \
        -d mcr.microsoft.com/mssql/server:2017-latest

The admin user is ´sa´ with password specified.

Also, to connect to the server from the terminal inside vagrant VM:

docker exec -it mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P StrongP@ssword!