Containers | QuickStart Docker Compose for SQL Server
Tags: docker, sqlserver, tutorial, dockercompose
Published: 2020-01-23 18:05:56

---
title: Containers | QuickStart Docker Compose for SQL Server
published: true
date: 2020-01-21 00:00:00 UTC
tags: docker, sqlserver, tutorial, dockercompose
canonical_url: 
---

> ‘Visual Studio Code + Compose File + Docker Plugin = Amazing’

In my previous post, [introduction to docker]( https://dev.to/kgeorge314/containers-quick-start-sql-server-containers-41c3) we looked at how to get, start, stop and remove containers by running the various `docker` commands. Today, we will look at how to use `docker-compose`, followed by how easy it is to use with [Visual Studio Code](https://code.visualstudio.com/) + [vscode-docker extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker).

#### Introduction to `docker-compose`

Dockers documentation on [docker-compose](https://docs.docker.com/compose/).

> Compose is a tool for defining and running multi-container Docker applications.

Note: `docker-compose` will only work with **Linux** containers. (At the moment).

Let us now break down the `docker run` command into a compose file.

`docker run -d -p 14333:1433 --name myDocker --hostname myDocker -e sa_password='$3cureP@ssw0rd' -e ACCEPT_EULA=Y microsoft/mssql-server-linux`

Recapping, we have a `Microsoft/mssql-server-linux` container named `myDocker`, running SQL Server on port `14333` with the `SA` password as `$3cureP@ssw0rd`.


{% gist https://gist.github.com/kgeorge314/2e36e2cdb2c98d185841a8c3dee6a8b6 file=single-sql-server.yml %}

Let me explain what each line means, in a simplistic way.

| Code | Notes |
| --- | --- |
| `version: '3'` | Version of Docker-Compose |
| `services:` | A list Services in the container |
| ` ____ db:` | A container named `db` |
| ` ________ image: microsoft/mssql-server-linux` | The image that this services will use |
| ` ________ environment:` | A list of environment variables |
| ` ____________ SA_PASSWORD: $3cureP@ssw0rd` | The environment variable `SA_PASSWORD` with its value as `$3cureP@ssw0rd` |
| ` ____________ ACCEPT_EULA: Y` | The environment variable `ACCEPT_EULA` with its value as `Y` |
| ` ________ ports:` | A list of ports exposed by this container |
| ` ____________ - '14333:1433'` | The mapping of container-port:1433 to 14333 |

\_Spaces are important in `YML` files which is why I have used `_` to represent them here.

Now that we have an understanding of the basics, how do we run a compose file?

#### Running a `docker-compose` file

To run a docker-compose file

1. To run `docker-compose -f "path\to\docker-compose.yml" up -d --build`
2. To tear-down `docker-compose -f "path\to\docker-compose.yml" down`
3. to restart, well it is `down` and `up` :)

And now a screencast of how to run a `docker-compose` file in VS Code

![docker-compose-up](https://datastories.aletasystems.com/assets/images/Posts/2020-01-21-Introduction-to-docker-compose/docker-compose-up.gif)[mp4 version](https://datastories.aletasystems.com/assets/images/Posts/2020-01-21-Introduction-to-docker-compose/docker-compose-up.mp4)(Ps: _I used [screentogif](https://www.screentogif.com/) to make these_)

#### Environment Variables in `docker-compose` file

If you recall, in the above screencast there was an error `Invalid interpolation format for "environment" option in service "db": "$3cureP@ssw0rd"`. With a compose file you can use variables in the format of `${variable_name}`.

**Scenario:** You don’t want to hard-code the password into your Docker-Compose file but would like it to be taken from a `.env` file (_ideally it is not part of source-control_).

In your `docker-compose` file you replace the _hard-code_ password with a variable `${SQL_SERVER_PASSWORD}`

{% gist https://gist.github.com/kgeorge314/2e36e2cdb2c98d185841a8c3dee6a8b6 file=single-sql-server-with-env.yml %}

Then you create a file name `.env` in the same folder as the `docker-compose` file.

{% gist https://gist.github.com/kgeorge314/2e36e2cdb2c98d185841a8c3dee6a8b6 file=single-sql-server-with-env.env %}

Now when you run `docker-compose up` it will automatically substitute the value of `$3cureP@ssw0rd` into `${SQL_SERVER_PASSWORD}` and as a bonus docker will automatically escape the `$` and change to `$$`

A snippet to run this code-example.

{% gist https://gist.github.com/kgeorge314/2e36e2cdb2c98d185841a8c3dee6a8b6 file=DockerCompose-with-env-var.ps1 %}