Skip to content

Commit cd4220a

Browse files
authored
launch ssh server in docker (#99)
* launch ssh server in docker * fix: ci
1 parent b5c8162 commit cd4220a

File tree

9 files changed

+121
-11
lines changed

9 files changed

+121
-11
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ docker-compose.yaml
77
Dockerfile
88
Pipfile
99
Pipfile.lock
10+
.env.example
11+
.env

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
AZURE_STORAGE_CONNECTION_STRING=
22
AZURE_STORAGE_CONTAINER=
33
AZ_ROOT_FILE_PATH=
4-
LOCAL_DOWNLOAD_PATH=
4+
LOCAL_DOWNLOAD_PATH=
5+
SSH_USERS=

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
run: |
6060
# Change owner of workspace to ubuntu user
6161
sudo chown -R 1000:1000 ${{ github.workspace }}
62-
docker run --rm -v ${{ github.workspace }}:/app -w /app ${{ steps.image_tag.outputs.TAG }} make test
62+
docker run --rm -v ${{ github.workspace }}:/app -w /app --entrypoint /bin/bash ${{ steps.image_tag.outputs.TAG }} -c "make test"
6363
6464
deploy-acr:
6565
name: Build and deploy to Azure Container Registry

Dockerfile

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
# Use the GDAL image as the base
22
FROM ghcr.io/osgeo/gdal:ubuntu-full-3.10.0
33

4+
ARG GROUPNAME="cbsurge"
5+
46
# Install necessary tools and Python packages
57
RUN apt-get update && \
6-
apt-get install -y python3-pip pipenv gcc cmake libgeos-dev && \
8+
apt-get install -y python3-pip pipenv gcc cmake libgeos-dev openssh-server && \
79
apt-get clean && \
810
rm -rf /var/lib/apt/lists/*
911

1012
# install azure-cli
1113
RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash
1214

15+
RUN mkdir /var/run/sshd && \
16+
echo 'PermitRootLogin no' >> /etc/ssh/sshd_config && \
17+
echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config
18+
1319
WORKDIR /app
1420

1521
COPY . .
22+
23+
# Create a group and set permissions for /app
24+
RUN groupadd ${GROUPNAME} && \
25+
usermod -aG ${GROUPNAME} root && \
26+
mkdir -p /app && \
27+
chown -R :${GROUPNAME} /app && \
28+
chmod -R g+rwx /app
29+
30+
RUN chmod +x /app/create_user.sh
31+
RUN chmod +x /app/entrypoint.sh
32+
33+
# install package
1634
RUN pipenv --python 3 && pipenv run pip install -e .
1735

18-
CMD [ "pipenv", "run", "rapida", "--help"]
36+
EXPOSE 22
37+
38+
ENTRYPOINT ["/app/entrypoint.sh"]

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ shell:
1313
@echo "------------------------------------------------------------------"
1414
@echo "Shelling in dev mode"
1515
@echo "------------------------------------------------------------------"
16-
docker compose -f docker-compose.yaml run cbsurge /bin/bash
16+
docker compose -f docker-compose.yaml run --entrypoint /bin/bash cbsurge
1717

1818

1919
test:
@@ -30,6 +30,13 @@ build:
3030
@echo "------------------------------------------------------------------"
3131
docker compose -f docker-compose.yaml build
3232

33+
up:
34+
@echo
35+
@echo "------------------------------------------------------------------"
36+
@echo "Launch docker containers"
37+
@echo "------------------------------------------------------------------"
38+
docker compose -f docker-compose.yaml up
39+
3340
down:
3441
@echo
3542
@echo "------------------------------------------------------------------"

README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,50 @@ before running the above command, please use `devcontainer` or `make shell` to e
7272

7373
## Using docker
7474

75-
- build docker-image
75+
### build docker-image
7676

7777
```shell
7878
make build
7979
```
8080

81-
- destroy docker container
81+
### Launch SSH server
82+
83+
- set users
84+
85+
```
86+
cp .env.example .env
87+
vi .env
88+
```
89+
90+
SSH_USERS can have multiple users (username:password) for SSH login
91+
92+
```shell
93+
SSH_USERS=docker:docker user:user
94+
```
95+
96+
- launch docker container
97+
98+
```shell
99+
make up
100+
```
101+
102+
The below command is connecting to `localhost` with user `docker` through port `2222`.
103+
104+
```shell
105+
ssh docker@localhost -p 2222
106+
107+
# make sure installing the package first
108+
cd /app
109+
pipenv run pip install -e .
110+
```
111+
112+
### destroy docker container
82113

83114
```shell
84115
make down
85116
```
86117

87-
- enter to Docker container
118+
### enter to Docker container
88119

89120
```shell
90121
make shell

create_user.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
USERNAME=$1
4+
PASSWORD=$2
5+
GROUPNAME=cbsurge
6+
7+
# skip if user already exists
8+
if id "$USERNAME" &>/dev/null; then
9+
echo "User $USERNAME already exists."
10+
else
11+
# create new user
12+
useradd -m -s /bin/bash "$USERNAME"
13+
echo "$USERNAME:$PASSWORD" | chpasswd
14+
echo "User $USERNAME created."
15+
16+
# Add the user to the group
17+
usermod -aG $GROUPNAME "$USERNAME"
18+
echo "User $USERNAME added to $GROUPNAME group."
19+
20+
# Grant sudo access (optional)
21+
usermod -aG sudo "$USERNAME"
22+
echo "User $USERNAME granted sudo privileges."
23+
fi
24+
25+
# Set ownership of /app folder to the user
26+
chown -R "$USERNAME:$USERNAME" /app
27+
echo "Ownership of /app granted to $USERNAME."

docker-compose.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ services:
44
build:
55
context: .
66
dockerfile: ./Dockerfile
7-
# default command to show help menu
8-
command: "pipenv run python -m cbsurge.cli --help"
97
volumes:
8+
- ./create_user.sh:/app/create_user.sh
9+
- ./entrypoint.sh:/app/entrypoint.sh
1010
- ./Makefile:/app/Makefile
1111
- ./cbsurge:/app/cbsurge # mount app folder to container
12-
- ./tests:/app/tests
12+
- ./tests:/app/tests
13+
entrypoint: "/app/entrypoint.sh"
14+
ports:
15+
- 2222:22
16+
environment:
17+
- SSH_USERS=${SSH_USERS:-''}

entrypoint.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
# Create multiple users from environment variable SSH_USERS
4+
# Format: SSH_USERS="user1:password1 user2:password2 user3:password3"
5+
if [ ! -z "$SSH_USERS" ]; then
6+
for user_info in $SSH_USERS; do
7+
IFS=':' read -r username password <<< "$user_info"
8+
if [ ! -z "$username" ] && [ ! -z "$password" ]; then
9+
/app/create_user.sh "$username" "$password"
10+
else
11+
echo "Invalid user format: $user_info"
12+
fi
13+
done
14+
fi
15+
16+
# launch ssh server
17+
/usr/sbin/sshd -D

0 commit comments

Comments
 (0)