In this demo we will instantiate a docker image with the latest SQL Server 2017
docker pull mcr.microsoft.com/mssql/server:2017-latest
Execute the container exposing port 14333 and connecting the local volue d:/ inside the container. This is very interesting when you want to get data from your local storage outside the container
docker run -p 14333:1433 -it -v d:/:/data -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=PaSSw0rd' --name my-container-sql2017 mcr.microsoft.com/mssql/server:2017-latest
-d parameter will open the container and return the execution to the bash runtime
sqlcmd -S laptop,14333 -U sa -P 'PaSSw0rd' -Q "select @@version"
Try to restore a database from your local path:
sqlcmd -S laptop,14333 -U sa -P 'PaSSw0rd' -Q " USE [master]; RESTORE DATABASE [pubs] FROM DISK = N'/data/Git_ecb/mssql-server-samplesdb/Backups/Pubs.bak' WITH FILE = 1, MOVE N'pubs' TO N'/var/opt/mssql/data/pubs.mdf', MOVE N'pubs_log' TO N'/var/opt/mssql/data/pubs_log.ldf', NOUNLOAD, STATS = 5"
or try to do the same and use SQL Server Management studio
By pressyng ctrl+z
docker run -p 14333:1433 -it -v d:/:/data -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=PaSSw0rd' --name my-container-sql2017 mcr.microsoft.com/mssql/server:2017-latest
This time it will throw you an error:
In this demo we will instantiate a docker image with the latest SQL Server 2017
docker pull mcr.microsoft.com/mssql/server:2017-latest
NOTE: valid tags: https://hub.docker.com/_/microsoft-mssql-server?tab=description
Execute the container as SQL Server Express, exposing port 14333 and 4Gb of RAM.
docker run --memory=4g -p 14333:1433 -it -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=PaSSw0rd' -e 'MSSQL_PID=Express' --name my-container-sql2017 mcr.microsoft.com/mssql/server:2017-CU20-ubuntu-16.04
NOTE: Resource tags https://docs.docker.com/config/containers/resource_constraints/
docker container list -a
sqlcmd -S localhost,14333 -U sa -P 'PaSSw0rd' -Q "select @@version"
USE [master]
go
create database test
go
use test
go
create table mytable(a int, b varchar(100))
go
insert into mytable(a,b) values(1,'hola'),(2,'mundo')
go
select * from mytable
go
By pressyng ctrl+c
docker run --memory=4g -p 14333:1433 -it -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=PaSSw0rd' -e 'MSSQL_PID=Express' --name my-container-sql2017 mcr.microsoft.com/mssql/server:2017-CU20-ubuntu-16.04
This time it will throw you an error:
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/my-container-sql2017" is already in use by container "70de3443a42ad37f55de9e8db4177a7a846b7067c39faf16db3410ad330dc50a". You have to remove (or rename) that container to be able to reuse that name. See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
So list the containers with
docker container list -a
CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES |
---|---|---|---|---|---|---|
70de3443a42a | mcr.microsoft.com/mssql/server:2017-CU20-ubuntu-16.04 | "/opt/mssql/bin/sqls…" | 9 minutes ago | Exited (0) About a minute ago | my-container-sql2017 | |
and execute the container with the name you gave it:
docker start -i my-container-sql2017
NOTE: The data still exists within the container...until you delete it
With the following execution, you will delete all exited containers
docker rm my-container-sql2017
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/my-container-sql2017" is already in use by container "70de3443a42ad37f55de9e8db4177a7a846b7067c39faf16db3410ad330dc50a". You have to remove (or rename) that container to be able to reuse that name. See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
So list the containers with
docker container list -a
CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES |
---|---|---|---|---|---|---|
70de3443a42a | mcr.microsoft.com/mssql/server:2017-latest | "/opt/mssql/bin/sqls…" | 9 minutes ago | Exited (0) About a minute ago | my-container-sql2017 | |
and execute the container with the name you gave it:
docker start -i my-container-sql2017
With the following execution, you will delete all exited containers
docker rm $(docker ps -q -f status=exited)