Reconnecting PC termial to a running ubuntu container

I am using my PC terminal to start an new ubuntu interactive container using the command

docker run -it ubuntu

Where I am using this to do some command line revision and practice, when I close the terminal window the container still exists in the Windows Desktop.

However when I return to terminal my connection to the docker desktop has been dropped and I am presented with a fresh PowerShell prompt.

Is there some way to use my terminal to re attach to the existing container?

I am using Docker version 27.2.0
Windows 11

Yes, but not to this specific container as you have created it

A container runs a process, when that process ends, so does the container’s purpose, and lifecycle

When you run docker run -it ubuntu, you create an ubuntu container who’s main process is a bash session, then you attach to it,
When you close your terminal, the bash session ends, and the container is done with its job.

If you want a container running idle, which you can enter and exit out of, you can do this:

docker run --name containername -d ubuntu tail -f /dev/null
docker exec -it containername bash

The first line creates the container (containername) with tail -f /dev/null being its main process - This watches for changes on the /dev/null file (Which is forever empty); effectively creating an idle container which does nothing and never exits on its own

The second line creates a secondary process in the container, being a bash shell session, and enter it interactively

Exiting that session will end the secondary process, but as the primary one (tail) will still be running, so will the container, and you could docker exec to enter it again as you please

Thankyou this I think this answers a second question, giving a coantainer a speciffic name I am going to have a play with this and see how I get on.

If you’ll be running containers with specific configurations again and again, you may want to look into Docker Compose as well

For example, the commands I specified above can translate to this:

compose.yml

services:
  servicename:
    image: ubuntu
    container_name: containername
    command: tail -f /dev/null

terminal:

docker compose up -d 

docker compose exec -it servicename bash
# or
docker exec -it containername bash

When dealing with many configurations, this will at som point be unavoidable

Nice! that worked for me. I have other questions but Iwilll post them seperately.