Solved!! Docker-Compose SQL Server database persist data after host restart


Problem: There is nothing like losing data in the SQL Server Docker Container after it stopped. As a rule of thumb, it's always good practice to back up data to several locations in case you need to role-back the database to a healthy known state or just to restore the database.

This short article, yet to the point will show how docker-compose.yaml file need to be configured in order to persist data after a restart.

Solution: Remember, when you don't have a Dockerfile in the root folder, there is no need to build the image, in this case, we will start our Docker Container by simply typing: sudo docker-compose up #that is it, needed to start the service.

In your docker-compose.yaml file
- define the version of docker-compose # Remember, docker-compose comes as a separate install. You will need to install docker-compose utility in order to utilize the CLI.
- After defining the version, define the services
- under services, define database image: #the database image you will be using in your Docker Container
- most important don't forget to provide the environment variables for a user and the password.
- Under the environment: define a password and Accept the EULA

[NB] The bind section under volumes will automatically sync the files in the Docker Container and the Host, if you have other jobs that back up the data to other location, this is the best place to define as the source.

[Troubleshoot]: Pay attention to the spacing, Yaml files are finicky when it comes to spacing, there is one space between "name definition" or "version:" and the actual version string. 

See the code below:

version: "3.2"
services:
   NameOfYourService:
        image: "NameOfTheImageYouWishToUse"
        container_name: GiveContainerName
        ports:
             - "HostPortToBindToDockerContainer:DockerContainerPort"
        enviroment:
             - USERNAME_PASSWORD=YourPasswordHere
             - ACCEPT_EULA="Y"
        restart: always
        volumes:
             - type: bind
                source: /locationOnHost
                target:  /targetLocationOnDockerContainer

 

 

 






© 2024 - ErnesTech - Privacy
E-Commerce Return Policy