1+ # TODO: Set the UID and GID To match the user who is going to use this container. Due to security, the UID and GID
2+ # *must* be baked in to the image in order for sshd to obtain the correct permissions to enable ssh security.
3+ UID ?= 1060
4+ GID ?= 100
5+
6+ # NOTE: SSH_PORT is only for local testing purposes. In Rabbit User Containers, the port is assigned dynamically via the
7+ # NNF_CONTAINER_PORTS environment variable.
8+ SSH_PORT ?= 5123
9+
10+ # SSH key files - these are generated locally and used in the container. The mpiuser_key is used for ssh authentication
11+ # to connect to the container. Do not lose the client keys.
12+ SSH_HOST_KEY_DIR = ./ssh_host_keys
13+ SSH_HOST_RSA_KEY = $(SSH_HOST_KEY_DIR ) /ssh_host_rsa_key
14+ SSH_CLIENT_KEY_DIR = ./ssh_client_keys
15+ SSH_CLIENT_KEY = $(SSH_CLIENT_KEY_DIR ) /mpiuser_key
16+
17+ # Image name and tag
18+ IMAGE_NAME ?= ssh-container
19+ TAG ?= latest
20+
21+ # CONTAINER_TOOL defines the container tool to be used for building images.
22+ # Be aware that the target commands are only tested with Docker which is
23+ # scaffolded by default. However, you might want to replace it to use other
24+ # tools. (i.e. podman)
25+ CONTAINER_TOOL ?= docker
26+
27+ # Container name for easier management
28+ CONTAINER_NAME = ssh-container-$(UID ) -$(GID )
29+
30+ .PHONY : all docker-build clean ssh-keys client-keys keys run stop connect status help
31+
32+ all : docker-build
33+
34+ help : # # Show this help message
35+ @echo " Available targets:"
36+ @awk ' BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " %-15s %s\n", $$1, $$2}' $(MAKEFILE_LIST )
37+
38+ # Generate SSH host keys
39+ ssh-keys : # # Generate SSH host keys for the container
40+ @echo " Generating SSH host keys..."
41+ @mkdir -p $(SSH_HOST_KEY_DIR )
42+ @if [ ! -f $( SSH_HOST_RSA_KEY) ]; then \
43+ ssh-keygen -t rsa -b 4096 -f $(SSH_HOST_RSA_KEY ) -N ' ' -C " SSH host RSA key" ; \
44+ fi
45+ @echo " SSH host keys generated successfully"
46+
47+ # Generate SSH client keys for authentication
48+ client-keys : # # Generate SSH client keys for authentication
49+ @echo " Generating SSH client keys..."
50+ @mkdir -p $(SSH_CLIENT_KEY_DIR )
51+ @if [ ! -f $( SSH_CLIENT_KEY) ]; then \
52+ ssh-keygen -t rsa -b 4096 -f $(SSH_CLIENT_KEY ) -N ' ' -C " mpiuser@ssh-container" ; \
53+ echo " " ; \
54+ echo " Client key generated: $( SSH_CLIENT_KEY) " ; \
55+ echo " Public key: $( SSH_CLIENT_KEY) .pub" ; \
56+ echo " " ; \
57+ echo " To connect without password:" ; \
58+ echo " ssh -i $( SSH_CLIENT_KEY) -p $( SSH_PORT) mpiuser@localhost" ; \
59+ echo " " ; \
60+ fi
61+
62+ # Generate both SSH host and client keys
63+ keys : ssh-keys client-keys # # Generate both SSH host and client keys
64+ @echo " SSH keys generated successfully"
65+ @echo " Host keys in: $( SSH_HOST_KEY_DIR) "
66+ @echo " Client keys in: $( SSH_CLIENT_KEY_DIR) "
67+
68+ # Build the container image
69+ docker-build :
70+ @echo " Building $( CONTAINER_TOOL) image with UID=$( UID) , GID=$( GID) ..."
71+ $(CONTAINER_TOOL ) build \
72+ --platform linux/amd64 \
73+ --build-arg USER_UID=$(UID ) \
74+ --build-arg USER_GID=$(GID ) \
75+ -t $(IMAGE_NAME ) :$(TAG ) \
76+ -t $(IMAGE_NAME ) :uid$(UID ) -gid$(GID ) \
77+ .
78+ @echo " $( CONTAINER_TOOL) image built successfully"
79+ @echo " Image tags:"
80+ @echo " - $( IMAGE_NAME) :$( TAG) "
81+ @echo " - $( IMAGE_NAME) :uid$( UID) -gid$( GID) "
82+ @echo " "
83+ @echo " To inspect UID/GID/PORT labels:"
84+ @echo " $( CONTAINER_TOOL) inspect $( IMAGE_NAME) :$( TAG) | grep -A 10 Labels"
85+
86+ # Run the container
87+ run : docker-build # # Run the container with SSH service (as UID 1060 and GID 100)
88+ $(CONTAINER_TOOL ) run -d \
89+ --platform linux/amd64 \
90+ --name $(CONTAINER_NAME ) \
91+ --user $(UID ) :$(GID ) \
92+ -p $(SSH_PORT ) :$(SSH_PORT ) \
93+ -e NNF_CONTAINER_PORTS=$(SSH_PORT ) \
94+ $(IMAGE_NAME ) :$(TAG )
95+ @echo " Container started with SSH on port $( SSH_PORT) (accessible via host port $( SSH_PORT) )"
96+ @echo " Connect with: make connect"
97+
98+ # Stop and remove container
99+ stop : # # Stop and remove the container
100+ -$(CONTAINER_TOOL ) stop $(CONTAINER_NAME )
101+ -$(CONTAINER_TOOL ) rm $(CONTAINER_NAME )
102+
103+ # Check container status
104+ status : # # Check if the container is running
105+ @if $(CONTAINER_TOOL ) ps | grep -q $(CONTAINER_NAME ) ; then \
106+ echo " Container $( CONTAINER_NAME) is running" ; \
107+ $(CONTAINER_TOOL ) ps | grep $(CONTAINER_NAME ) ; \
108+ else \
109+ echo " Container $( CONTAINER_NAME) is not running" ; \
110+ fi
111+
112+ # Connect to the running container using SSH keys
113+ connect : # # Connect to the container via SSH
114+ @if [ ! -f $( SSH_CLIENT_KEY) ]; then \
115+ echo " Client key not found. Run 'make client-keys' first." ; \
116+ exit 1; \
117+ fi
118+ @if ! $(CONTAINER_TOOL ) ps | grep -q $(CONTAINER_NAME ) ; then \
119+ echo " Container $( CONTAINER_NAME) is not running." ; \
120+ echo " Run 'make run' to start the container first." ; \
121+ echo " Or run 'make status' to check container status." ; \
122+ exit 1; \
123+ fi
124+ @echo " Connecting to SSH container..."
125+ ssh -i $(SSH_CLIENT_KEY ) -p $(SSH_PORT ) -o StrictHostKeyChecking=no mpiuser@localhost
126+
127+ # Clean up generated files and containers
128+ clean : stop # # Clean up generated files and containers
129+ rm -rf $(SSH_HOST_KEY_DIR )
130+ rm -rf $(SSH_CLIENT_KEY_DIR )
131+ -$(CONTAINER_TOOL ) rmi $(IMAGE_NAME ) :$(TAG )
132+ -$(CONTAINER_TOOL ) rmi $(IMAGE_NAME ) :uid$(UID ) -gid$(GID )
0 commit comments