Enable ssh into a live docker container

I am new to docker.I am running a Pi hole server in a docker container (called container manager in synology DSM). I was trying to run gravity vm to sync 2 pi holes which needs ssh. But the docker container does not have ssh enabled, how can i enable ssh in a running docker container

To use SSH directly with a container, you need to install a SSH server inside the container.

But you can usually create a shell inside a container from host with docker exec -it <c-id> sh.

ok good idea i installed ssh server by opening shell (apt install -y openssh-server) in docker but while i try to ssh it still says connection refused
ssh: connect to host 192...* port 22: Connection refused.

You also need to open the port on host to be forwarded into container.

docker run -p 2000:22 …

Probably not 22 on host, as thatbis already used by the hosts SSH, but a different one on the outside.

Note: opening shell and installing packages is not the way to do it with containers, as that is lost upon container re-create or upgrade. If you really need to install an additional package, you would usually create your own image with your own Dockerfile.

Thats a valid point i did know that docker updates worked that way. here is my current docker compose file for pi hole . Can you let me know what should i add in this docker file to enable ssh and port forward 22.

version: “3”

container_name: pihole
image: pihole/pihole:latest
- “”
- “”
- “” # Only required if you are using Pi-hole as your DHCP server
- “”
- ph_network
- ph_bridge
TZ: ‘Asia/Kolkata’
WEBPASSWORD: ‘*********’
# Volumes store your data between container upgrades
- ‘/volume1/docker/pihole/pihole:/etc/pihole’
- ‘/volume1/docker/pihole/dnsmasq.d:/etc/dnsmasq.d’
- NET_ADMIN # Required if you are using Pi-hole as your DHCP server, else not needed
restart: unless-stopped
driver: bridge
- subnet:
name: ad_blocker
external: true

Check the ports section :slight_smile: