Why docker with SQL Server disappeared? - sql-server

I run SQL Server in container
docker run --network=bridge --name sql29 -h sql29 -it --rm -v h:/sql219data:/var/opt/mssql -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=sQL_19[pwd]" -p 12433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
as described here:
https://learn.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container?view=sql-server-ver15
I see the active docker
docker ps
But if I try to create the new folder :
docker exec -it sql29 mkdir /var/opt/mssql/bkp22
then the docker disappeared.
docker ps
.........
How to understand: why the docker disappeared? Maybe the volume was mapped incorrectly?

As #Zeitounator commented, the tutorial you linked has a note saying that bind mounts don't work on Windows with the /var/opt/mssql directory:
Host volume mapping for Docker on Windows does not currently support mapping the complete /var/opt/mssql directory. However, you can map a subdirectory, such as /var/opt/mssql/data to your host machine.
You commented that your goal was to keep or restore databases between docker runs. You don't need a bind mount to do that, this is the primary purpose of a volume.
You can create a volume using docker volume create:
docker volume create sql219data
Then run your container using this volume:
docker run -v sql219data:/var/opt/mssql # ...
For debugging purposes you can remove the --rm option from your docker run command so the container won't be removed when stopped. You will then be able to read the logs of the container (even if it stopped):
docker logs sql29
# Then remove it to run the same `docker run` command again
docker rm sql29
docker run # ...

Related

Mongodb running on Docker is wiping the collection after restart

I have to build a small application that reads data from MongoDB running on docker and uses it for further processes.
The problem is that after I close docker, the local instance of the database is also getting deleted. How can I stop it?
The MONGODB_URI is mongodb://localhost:27017 and what are the attributes that I should add in the docker command to avoid it. should I avoid using localhost? docker-compose seems confusing to me so I use Dockerfile.
So, what exactly can be the docker run command to avoid it? is it one of these?
Commands: docker run -d --name mongo-on-docker -p 27017:27017 mongo
docker run -d --name sample --link mongo-on-docker web app
Also to permanently save what data directory should I use?
Docker container are dead before quiting. For store data you should mount named volume, folder or file to the container.
In MongoDB case try:
docker run --rm -ti -v mongo_data:/data/db mongo bash
Where mongo_data is a special Docker entity, that can be mounted as a folder into container. Including in different containers at the same time.
Not new:
How to set docker mongo data volume

Unable to bind mount a Windows folder in a Linux SQL Server docker image

I'm trying to run the docker image mcr.microsoft.com/mssql/server:2019-latest on a windows host.
When running this command line, everything works fine:
docker run --rm -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=My_Password" -p 1433:1433 mcr.microsoft.com/mssql/server:2019-latest
When I try to bind mount the /var/opt/mssql/data folder to access it's content from my windows host, I get the following error:
PS C:\Docker> docker run --rm -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=My_Password" -v C:\SQL\data:/var/opt/mssql/data -p 1433:1433 mcr.microsoft.com/mssql/server:2019-latest
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.
2020-05-13 20:39:01.91 Server Setup step is copying system data file 'C:\templatedata\master.mdf' to '/var/opt/mssql/data/master.mdf'.
2020-05-13 20:39:01.99 Server ERROR: Setup FAILED copying system data file 'C:\templatedata\master.mdf' to '/var/opt/mssql/data/master.mdf': 31(A device attached to the system is not functioning.)
ERROR: BootstrapSystemDataDirectories() failure (HRESULT 0x8007001f)
It looks like this issue: https://support.microsoft.com/en-us/help/4212960/fix-sql-server-2017-won-t-start-with-linux-container-image-on-docker
Which is supposed to be fixed. But I tried with several version of the docker image, including mcr.microsoft.com/mssql/server:2017-CU7-ubuntu, which is supposed to be the version in which that issue was fixed.
It seems to be an issue in the new Docker for Windows version. I did rollback from 2.3.0.2 to 2.2.0.5, and now the bind mount works as expected.
An issue has been raised about it in the mssql-docker github: https://github.com/microsoft/mssql-docker/issues/600

Problem Optimizing Docker Container Start With SqlServer on MacBook OSX

I'm a Docker newbie and have managed to create simple steps to create, start and load an image running sqlserver with a database backup. For me, it's three steps now.
docker pull mcr.microsoft.com/mssql/server:2019-latest
docker run --name SQL19c -p 1433:1433 -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=xxxx" -v /Users/useraccount/sql:/sql -d mcr.microsoft.com/mssql/server:2019-latest
docker exec -it SQL19c /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'xxxx' -Q 'RESTORE DATABASE svcodecamp FROM DISK = "/sql/sv-small-2019.bak" WITH MOVE "361684_codecamp08_dat" TO "/var/opt/mssql/ata/codecamp08_dat.mdf", MOVE "361684_codecamp08_log" TO "/var/opt/mssql/data/codecamp08_log.mdf"'
This is on my macbook running OSX which I reboot frequently so I need to do this everytime I need to use SqlServer.
Questions with this:
1) Each time I do this, I have to increment the SQL19c to Sql19d (or next letter of alphabet) because I get error saying name in use. How to re-use same name?
2) If I rm the container, it needs to repull the full image (1gig). I need to just start it and reload the data, not pull the full image
3) Is there a more optimum way to start SqlServer and load the data without using too much of my battery every time I reboot my computer or restart docker?
(notice my backup file is on a docker share so I don't need to recopy that in)
This is because you already have a container with this name. Try to execute the command:
docker container list -a
If you repull the image I think you remove the image NOT the container for remove the container you must run
docker container rm SQL19x
The way is to restart the container, drop database and then restore the database
Run ONCE: This create a SQL19x container
docker pull mcr.microsoft.com/mssql/server:2019-latest
docker run --name SQL19x -p 1433:1433 -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=xxxx" -v /Users/useraccount/sql:/sql -d mcr.microsoft.com/mssql/server:2019-latest
Now each time you restart the machine you must run the command below to start the container and restart the database.
docker container start SQL19x
docker exec -it SQL19x /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'xxxx' -Q 'DROP DATABASE svcodecamp'
docker exec -it SQL19x /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'xxxx' -Q 'RESTORE DATABASE svcodecamp FROM DISK = "/sql/sv-small-2019.bak" WITH MOVE "361684_codecamp08_dat" TO "/var/opt/mssql/ata/codecamp08_dat.mdf", MOVE "361684_codecamp08_log" TO "/var/opt/mssql/data/codecamp08_log.mdf"'
If you want to have a clean shutdown before to poweroff your machine execute
docker container stop SQL19x

Developing react app via docker container

I am trying to develop a simple react app and I am trying to use docker for running a development server, but it is not connecting up in the browser
Here is the Dockerfile.dev
FROM node:alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "run", "start"]
EXPOSE 3000
Here are the two commands to create and run the container
docker build -f Dockerfile.dev .
docker run -p 3000:3000 <image_id>
It's starting a development server as the normal npm start does but it is not running in the browser at localhost:3000
You container will exist if you did not mention -it or -dit (if its not typo in question). The reason being stopped immediately because bash can't find any pseudo terminal to be allocated. You have to specify -it or -dit so that bash or sh can be allocated to a pseudo-terminal.
docker run --name test -p 3000:3000 <image_id>
If you run docker ps | grep test you will see in the output
"/bin/bash" {some} seconds ago Exited (0) {some} seconds ago
Now try to run with
docker run --name test -dit -p 3000:3000 <image_id>
or
docker run --name test -it -p 3000:3000 <image_id>
Good to go localhost:3000
Updated:
For window, docker toolbox follows these steps.
Click the appropriate machine (probably the one labeled "default")
Settings
Network > Adapter 1 > Advanced > Port Forwarding
Click "+" to add a
new Rule Set Host Port 3000 & Guest Port 3000; be sure to leave Host
IP and Guest IP empty
Run the command:
docker run -dit -p 3000:3000 ${image_id}
docker-toolbox-localhost

Docker postgres starting automation can not create database due to error

I want to create a shell script which automates the creation and running and creating database in a postgres database using docker.
I want to use the docker postgres official package for postgres in docker.
The script that I use is as follows:
docker network create --subnet=172.18.0.0/16 shared_network;
docker kill postgres_linker;
docker rm postgres_linker;
docker run --name postgres_linker -e POSTGRES_PASSWORD=blahblahblah -d --net shared_network --ip 172.18.0.2 postgres:10-alpine;
docker exec -it postgres_linker psql -U postgres -c "create database linker;";
But when I run this I get the following output without any database being created:
Error response from daemon: network with name shared_network already exists
postgres_linker
postgres_linker
b2a9fd4d6e25b62d60adb05c8b6b653a1b55ec7a869c4728677d6289f5cddd63
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
The first line of this log is OK, the second and third are too, The problem is that psql does not run the command on the postgres container althogh the command I am trying to run is correct. If I run the last command seperately from my shell script:
docker exec -it postgres_linker psql -U postgres -c "create database linker;";
It does not give me error! and it works!
Why is this behavior happening?
I found the solution to my problem.
Unfortunately I thought that when postgres container is run the server is up and running immediately.
It was not true and it takes some time to come up. so I have needed to add some delay.
So the resulting script file should look like this:
docker network create --subnet=172.18.0.0/16 shared_network;
docker kill postgres_linker;
docker rm postgres_linker;
docker run --name postgres_linker -e POSTGRES_PASSWORD=blahblahblah -d --net shared_network --ip 172.18.0.2 postgres:10-alpine;
sleep 5;
docker exec -it postgres_linker psql -U postgres -c "create database linker;";

Resources