Skip to content

Commit 131efb1

Browse files
committed
LOCAL_PORT is optional, licence, ssh cmd, readme w/o docker run block
1 parent c5caad4 commit 131efb1

File tree

5 files changed

+130
-14
lines changed

5 files changed

+130
-14
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
### Changed
10+
11+
- No changes yet.
12+
13+
[Unreleased]: https://github.com/symfony-doge/docker-ssh-tunnel/compare/0.1.0...0.x
14+
[0.2.0]: https://github.com/symfony-doge/docker-ssh-tunnel/compare/0.1.0..0.2.0
15+
[0.1.0]: https://github.com/symfony-doge/docker-ssh-tunnel/releases/tag/0.1.0

Dockerfile

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@ FROM alpine:3.11
33

44
LABEL maintainer="Pavel Petrov <[email protected]>"
55
LABEL version="0.1.0"
6-
LABEL description="Docker SSH tunnel"
6+
LABEL description="A simple SSH tunnel for port forwarding within Docker environment"
77

8-
RUN apk add --update openssh-client && rm -rf /var/cache/apk/*
8+
ENV LOCAL_HOST *
9+
ENV LOCAL_PORT _
10+
11+
ENV REMOTE_HOST 127.0.0.1
12+
ENV REMOTE_PORT _
913

10-
ARG LOCAL_HOST=*
11-
ARG LOCAL_PORT
14+
ENV SSH_TUNNEL_HOST _
15+
ENV SSH_TUNNEL_PORT 22
16+
ENV SSH_TUNNEL_USER root
1217

13-
ARG REMOTE_HOST=127.0.0.1
14-
ARG REMOTE_PORT
18+
RUN apk add --update openssh-client && rm -rf /var/cache/apk/*
1519

16-
ARG SSH_TUNNEL_HOST
17-
ARG SSH_TUNNEL_PORT=22
18-
ARG SSH_TUNNEL_USER=root
20+
COPY docker-ssh-tunnel-cmd.sh /usr/local/bin/docker-ssh-tunnel-cmd.sh
1921

20-
CMD ssh \
21-
-p $SSH_TUNNEL_PORT \
22-
-L $LOCAL_HOST:$LOCAL_PORT:$REMOTE_HOST:$REMOTE_PORT \
23-
$SSH_TUNNEL_USER@$SSH_TUNNEL_HOST \
24-
-N
22+
CMD ["docker-ssh-tunnel-cmd.sh"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Pavel Petrov <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
# Docker SSH tunnel
3+
4+
This docker image can be used in case when you need
5+
an access, for example, to some service on your production server,
6+
which doesn't expose it's ports to the worldwide.
7+
Additionally, you don't want to touch ports on your local machine,
8+
because some of them can be busy or just reserved.
9+
10+
## Usage
11+
12+
### docker run
13+
14+
### docker-compose.yml
15+
16+
The minimal configuration will look like:
17+
18+
```
19+
services:
20+
ssh-tunnel-example:
21+
image: symfonydoge/docker-ssh-tunnel:latest
22+
volumes:
23+
- $HOME/.ssh/id_rsa:/root/ssh-import/id_rsa:ro
24+
environment:
25+
SSH_TUNNEL_HOST: corp.domain.ltd
26+
REMOTE_PORT: 6379
27+
```
28+
29+
where `$HOME/.ssh/id_rsa` is a path to your private SSH key,
30+
`corp.domain.ltd` is a domain or IP address of your server
31+
and `6379` is the remote port to forward
32+
(local port will be the same if not specified explicitly,
33+
see full list of environment variables below).
34+
35+
Now each container in the compose project can access
36+
tunnelled resource through `ssh-tunnel-example:6379`.
37+
38+
### Environment Variables
39+
40+
| Name | Default value | Description |
41+
| :---------------- | :------------ | :-------------------------------------------------------------------------------- |
42+
| `LOCAL_HOST` | * | IP address or domain to present remote resource in the tunnel container |
43+
| `LOCAL_PORT` | equal to `REMOTE_PORT` | Port number to present remote resource in the tunnel container |
44+
| `REMOTE_HOST` | 127.0.0.1 | IP address or domain of resource on the remote server by which it can be accessed |
45+
| `REMOTE_PORT` | | Port number of resource on the remote server by which it can be accessed |
46+
| `SSH_TUNNEL_HOST` | | Host of the remote server to create an SSH tunnel |
47+
| `SSH_TUNNEL_PORT` | 22 | SSH daemon port on the remote server |
48+
| `SSH_TUNNEL_USER` | root | User login for establishing an SSH connection |
49+
50+
### Hints
51+
52+
- You can expose a port only for local access
53+
in your docker-compose project, just specify localhost
54+
in the mapping: `127.0.0.1:6379:6379`. See [documentation](https://docs.docker.com/config/containers/container-networking/).
55+
56+
## Changelog
57+
All notable changes to this project will be documented in [CHANGELOG.md](CHANGELOG.md).

docker-ssh-tunnel-cmd.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env sh
2+
3+
if [ $LOCAL_PORT == "_" ]; then
4+
LOCAL_PORT=$REMOTE_PORT
5+
fi
6+
7+
SSH_TUNNEL_CMD="
8+
ssh \
9+
-v \
10+
-o StrictHostKeyChecking=no \
11+
-o ForwardAgent=yes \
12+
-o TCPKeepAlive=yes \
13+
-o ConnectTimeout=5 \
14+
-o ServerAliveCountMax=10 \
15+
-o ServerAliveInterval=15 \
16+
-i /root/ssh-import/id_rsa
17+
-p $SSH_TUNNEL_PORT \
18+
-L $LOCAL_HOST:$LOCAL_PORT:$REMOTE_HOST:$REMOTE_PORT \
19+
$SSH_TUNNEL_USER@$SSH_TUNNEL_HOST \
20+
-N
21+
"
22+
23+
echo "Running:" ${SSH_TUNNEL_CMD}
24+
25+
${SSH_TUNNEL_CMD}

0 commit comments

Comments
 (0)