Docker on Windows SSH not detached despite being run with -d flag

18 Views Asked by At

I have a Windows Server running that I ssh into. If I then start Docker Desktop and a Docker container with this command:

docker run -d --name pg-test -e POSTGRES_DB=testdb -e POSTGRES_USER=testuser -e POSTGRES_PASSWORD=test -e PGDATA=/var/lib/postgresql/data/pgdata -e POSTGRES_INITDB_ARGS="--encoding=UTF-8 --lc-collate=C --lc-ctype=C" -v testDbPostGres:/var/lib/postgresql/data -p 5432:5432 postgres:latest

It will automatically shutdown if I close the ssh connection.

Why is this happening?

1

There are 1 best solutions below

1
Allora Jimenez On

This is happening because the Docker container you started is running in the background and is attached to your SSH session. When you close the SSH connection, the session ends and all processes attached to it, including the Docker container, are terminated.

To prevent the Docker container from shutting down when you close the SSH connection, you can use the nohup command to detach the process from the session, or you can use a tool like screen or tmux to create a persistent session that can be detached and reattached later. These methods allow the Docker container to continue running even after you close the SSH connection.