-
Couldn't load subscription status.
- Fork 6
Executing commands against a container
When investigating issues with SQL running in a container or just exploring, executing commands against a running container can be really handy.
And it's easy to do with the docker container exec command.
Let's run a SQL container: -
docker container run -d `
--publish 15789:1433 `
--env ACCEPT_EULA=Y `
--env SA_PASSWORD=Testing1122 `
--name sqlcontainer1 `
mcr.microsoft.com/mssql/server:2019-CU5-ubuntu-18.04
Confirm that the container is running: -
docker container ls -a

Ok, let's run some commands against that container. First, let's list the files in the master database location: -
docker container exec sqlcontainer1 ls -al /var/opt/mssql/data

So the format of the container exec command is: -
docker container exec CONTAINERNAME COMMAND
Let's see the processes running within the container: -
docker container exec sqlcontainer1 ps aux

By default, we run commands as the user of the process running in the container: -
docker container exec sqlcontainer1 whoami

But this can be changed by using the u (user) flag: -
docker container exec -u 0 sqlcontainer1 whoami

By setting the user to 0, we can execute commands as root within the container!
We can also jump into the container itself using: -
docker container exec -it sqlcontainer1 bash
The i flag gives us an interactive session and the t flag opens a psuedo terminal, then we run bash to get a shell in the container.
Using this, we can dig around in the container, run our commands and then exit out: -
ls /var/opt/mssql/data
ps aux
exit
