Containers | QuickStart Docker Compose for SQL Server
Containers, Docker Compose, Local Development, DLM with Containers ·‘Visual Studio Code + Compose File + Docker Plugin = Amazing’
In my previous post, introduction to docker 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 + vscode-docker extension.
Introduction to docker-compose
Dockers documentation on docker-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
.
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
- To run
docker-compose -f "path\to\docker-compose.yml" up -d --build
- To tear-down
docker-compose -f "path\to\docker-compose.yml" down
- to restart, well it is
down
andup
:)
And now a screencast of how to run a docker-compose
file in VS Code
mp4 version (Ps: I used ScreenToGif 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}
Then you create a file name .env
in the same folder as the docker-compose
file.
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.