Running containers inside a docker container with port forwarding to make them accessible through ssh

I want to learn docker and how i can manage the containers inside the docker environment such that they can be used as dev environments.

I am a complete beginner while using Docker. I have a remote machine running ubuntu. I connect to that using ssh from my windows machine to use it as a dev environment. What i want to do is I want to create two other containers inside that remote machine, one of kali linux and another of centos9. I want to be able to use those containers as dev environments seperately. Just like i have connected to the remote machine using ssh i want those two containers to be accessible through ssh from my vscode remote explorer extension.(similar to making an ssh connection).

My thought on how one can do this is by following these steps:

  1. Install docker in the remote ubuntu machine by connecting through ssh to that machine from my windows machine.
  2. After this pull the kali and centos images from docker hub.(the os pulled may be any)
  3. Start the containers with this command
    docker run -itd --network=host -p 2222:localhost:22 --name kali_container kalilinux/kali-rolling
    docker run -itd --network=host -p 2223:localhost:22 --name centos_container centos

and allow those ports 2222 and 2223 to be mapped to any two ports in the host machine like 1020,1021,1022 because i still have to access the ubuntu container that is running which works as my host machine(port 1020) and 1021 and 1022 ports are for the two new containers inside those containers.

This is a thought and i want to try this out. I want help in this and a complete procedure on how i can do this. My main purpose is to create a versioning system based on this such that the host machine will keep images of the two containers running at certain interval of time and if something breaks in the two containers when i am using them as dev environments, i can rollback to previous images.

Please help me with this.

So what is stopping you or what is not working?

I tried running the commands that i mentioned above and after starting those containers like this and using docker ps -a to see the status of the running containers , no ports are shown there. If you could tell me if the solution i have presented with is correct or not and if there is another better way to do it, it would be very helpful.

Maybe the containers don’t have a openssh-server installed and running. So simply no SSH connection possible.

You can go inside the container with docker exec -it centos_container sh.

Note: using --network=host and (corrected) -p 2222:22 does not make sense, as you don’t have to publish ports from inside the container to the outside, when using host network directly.