Home > Enterprise >  Running SQL Server as a docker container. command line to start the container after system reboot
Running SQL Server as a docker container. command line to start the container after system reboot

Time:10-30

I have downloaded the SQL Server docker container onto my mac.

I downloaded the container using the following command from terminal

docker pull mcr.microsoft.com/mssql/server:2019-latest

I was then able to run the container using the following command

docker run -e ‘ACCEPT_EULA=Y’ -e ‘SA_PASSWORD=MyStrongPassword’ -p 1433:1433 –name sql2019_latest -d mcr.microsoft.com/mssql/server:2019-latest

However I am finding that after taking my mac out of sleep/reboot, the image is always stopped

If I run

docker images

I get the following

REPOSITORY                                   TAG           IMAGE ID       CREATED        SIZE
mcr.microsoft.com/mssql/server               2019-latest   80bdc8efc889   4 weeks ago    1.55GB

And when I run

docker ps

I then get nothing returned

docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

I am trying to start the image, and I can start the image if I do it via the Docker Desktop, but I want to know what I should do to get the image started from a command line

I tried

docker run mcr.microsoft.com/mssql/server:2019-latest

and I get this in response

SQL Server 2019 will run as non-root by default.
This container is running as user mssql.
To learn more visit https://go.microsoft.com/fwlink/?linkid=2099216.
The SQL Server End-User License Agreement (EULA) must be accepted before SQL
Server can start. The license terms for this product can be downloaded from
http://go.microsoft.com/fwlink/?LinkId=746388.

You can accept the EULA by specifying the --accept-eula command line option,
setting the ACCEPT_EULA environment variable, or using the mssql-conf tool.

I then tried

docker run mcr.microsoft.com/mssql/server:2019-latest --accept-eula

and I get this response:

SQL Server 2019 will run as non-root by default.
This container is running as user mssql.
To learn more visit https://go.microsoft.com/fwlink/?linkid=2099216.
/opt/mssql/bin/permissions_check.sh: line 59: exec: --: invalid option
exec: usage: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]

What am I doing wrong to start my container from starting from the command line

*** UPDATE ****

Following the reply by Jamie F I did the following:

I ran the docker ps -a command

docker ps -a
CONTAINER ID   IMAGE                                        COMMAND                  CREATED          STATUS                      PORTS                                       NAMES
cac05db1aac7   mcr.microsoft.com/mssql/server:2019-latest   "/opt/mssql/bin/perm…"   38 minutes ago   Exited (2) 38 minutes ago                                               agitated_cohen
0327940b7393   mcr.microsoft.com/mssql/server:2019-latest   "/opt/mssql/bin/perm…"   39 minutes ago   Exited (2) 39 minutes ago                                               focused_mclean
ad077064a63a   mcr.microsoft.com/mssql/server:2019-latest   "/opt/mssql/bin/perm…"   41 minutes ago   Exited (1) 41 minutes ago                                               objective_torvalds
c9809150f9d1   mcr.microsoft.com/mssql/server:2019-latest   "/opt/mssql/bin/perm…"   2 days ago       Exited (255) 44 hours ago   0.0.0.0:1433->1433/tcp, :::1433->1433/tcp   mssql2019

So I can see that there are 4 stopped containers, three with random names, but one with the name mssql2019

when I ran

docker start mssql2019 

Then my image started

This is different from the name I thought I was giving the container, but in my instance I have been able to do what I required

I wanted to clean up the containers so I ran the following commands

docker rm cac05db1aac7
docker rm 0327940b7393
docker rm ad077064a63a

now when I run docker ps I can see my running image

docker ps
CONTAINER ID   IMAGE                                        COMMAND                  CREATED      STATUS         PORTS                                       NAMES
c9809150f9d1   mcr.microsoft.com/mssql/server:2019-latest   "/opt/mssql/bin/perm…"   3 days ago   Up 4 minutes   0.0.0.0:1433->1433/tcp, :::1433->1433/tcp   mssql2019

and when I run docker ps -a I can see only one instance of the container

docker ps -a
CONTAINER ID   IMAGE                                        COMMAND                  CREATED      STATUS         PORTS                                       NAMES
c9809150f9d1   mcr.microsoft.com/mssql/server:2019-latest   "/opt/mssql/bin/perm…"   3 days ago   Up 3 minutes   0.0.0.0:1433->1433/tcp, :::1433->1433/tcp   mssql2019

CodePudding user response:

When you first ran this container, you downloaded the image from the docker registry. Multiple instances of the container can be run from that image. docker ps shows running containers by default. You will see your stopped container if you run docker ps -a

To start a stopped container, you can run the command docker start <container_name or ID> so in your case:

docker start sql2019_latest

(See where you assigned that name when you first ran the container?) docker stop sql2019_latest will stop it as well.

In your case, the container (sql2019_latest) and the container image (mcr.microsoft.com/mssql/server:2019-latest) have very similar names. But that is not always the case. If you don't provide a name when you run the container, docker will create one for you, by combining a couple of odd words. Knowing when you are working with the image and when you are working with the container will help you a bunch.

CodePudding user response:

Specify a --restart parameter on the docker run command to restart the container after a reboot. The example below will automatically restart it unless it was stopped before the reboot.

docker run -e ‘ACCEPT_EULA=Y’ -e ‘SA_PASSWORD=MyStrongPassword’ -p 1433:1433 –name sql2019_latest -d --restart unless-stopped mcr.microsoft.com/mssql/server:2019-latest
  • Related