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”

services:
pihole:
container_name: pihole
image: pihole/pihole:latest
ports:
- “192.168.1.198:53:53/tcp”
- “192.168.1.198:53:53/udp”
- “192.168.1.198:67:67/udp” # Only required if you are using Pi-hole as your DHCP server
- “192.168.1.198:80:80/tcp”
networks:
- ph_network
- ph_bridge
environment:
TZ: ‘Asia/Kolkata’
WEBPASSWORD: ‘*********’
DNSMASQ_LISTENING: local
# Volumes store your data between container upgrades
volumes:
- ‘/volume1/docker/pihole/pihole:/etc/pihole’
- ‘/volume1/docker/pihole/dnsmasq.d:/etc/dnsmasq.d’
cap_add:
- NET_ADMIN # Required if you are using Pi-hole as your DHCP server, else not needed
restart: unless-stopped
networks:
ph_bridge:
driver: bridge
ipam:
config:
- subnet: 192.168.10.0/24
gateway: 192.168.10.1
ip_range: 192.168.10.2/32
ph_network:
name: ad_blocker
external: true

Check the ports section :slight_smile: