Configuring network for SQL-Server as a docker image on Ubuntu guest under Windows 10 host

3 min read 22-10-2024
Configuring network for SQL-Server as a docker image on Ubuntu guest under Windows 10 host

Configuring SQL Server in a Docker container on an Ubuntu virtual machine (VM) running on a Windows 10 host can enhance your development experience by providing a lightweight and portable database solution. This article will guide you through the process step by step, including the necessary setup and configurations.

Prerequisites

Before we dive into the setup, ensure you have the following:

  1. Windows 10 Host: You should be using a version that supports Hyper-V or WSL2.
  2. Ubuntu Guest: An Ubuntu VM running on Windows using either Hyper-V or VirtualBox.
  3. Docker: Installed on your Ubuntu VM.
  4. SQL Server Docker Image: Access to the Microsoft SQL Server Docker image.

Step 1: Setting up Docker on Ubuntu

To run SQL Server as a Docker image, you need to have Docker installed on your Ubuntu guest. Here are the commands to install Docker:

sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql17
sudo apt-get install -y docker.io

Step 2: Pulling the SQL Server Docker Image

Once Docker is installed, you can pull the SQL Server image. The command below pulls the latest SQL Server image:

sudo docker pull mcr.microsoft.com/mssql/server

Step 3: Running the SQL Server Container

Now that you have the SQL Server image, it's time to run the container. Replace your_password with a strong password of your choice:

sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=your_password' -p 1433:1433 --name sql1 -d mcr.microsoft.com/mssql/server

This command does several things:

  • Accepts the SQL Server EULA.
  • Sets the SA (System Administrator) password.
  • Maps port 1433 of the container to port 1433 on your host.
  • Names the container sql1 and runs it in detached mode.

Step 4: Configuring Networking

Accessing SQL Server

To access SQL Server from your Windows 10 host machine, you will need to ensure that the network settings for your VM are correctly configured.

  1. Ensure Port Forwarding: Make sure your Ubuntu VM is set to forward traffic from port 1433 on the host to port 1433 on the VM.

  2. Use the Correct IP Address: You can find your VM’s IP address by running:

    hostname -I
    
  3. Connect to SQL Server: Using SQL Server Management Studio (SSMS) on Windows, connect to the VM's IP address, using SA as the login and the password you set earlier.

Step 5: Testing the Connection

To ensure that everything is working properly, you can test the connection by executing the following command in your terminal:

sqlcmd -S localhost -U SA -P 'your_password'

This command connects to the SQL Server running in your Docker container. If successful, you will be presented with a SQL prompt.

Additional Considerations

  • Data Persistence: By default, any data created in the SQL Server container will be lost when the container is stopped. To prevent data loss, consider mounting a volume:
sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=your_password' -p 1433:1433 --name sql1 -v /your/local/dir:/var/opt/mssql -d mcr.microsoft.com/mssql/server
  • Performance: Running SQL Server in a Docker container may have some performance overhead compared to running it natively. However, the advantages of isolation and ease of deployment generally outweigh this concern for development purposes.

Conclusion

Using Docker to run SQL Server on an Ubuntu guest under a Windows 10 host is a straightforward process that facilitates a portable development environment. The steps outlined above should help you get SQL Server up and running quickly. With this setup, you can begin developing applications that rely on SQL Server without the complexities of managing a full-blown database server installation.

Useful Resources

By following these steps, you should now have a fully functional SQL Server setup running in a Docker container on your Ubuntu guest, all while using your Windows 10 machine as the host. Enjoy your development!