Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial docker work #4 #5

Merged
merged 2 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Source/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/bin/*
**/obj/*
**/out/*
**/.vs/*
**/_ReSharper.Caches/*
8 changes: 8 additions & 0 deletions Source/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@
<PackageReference Include="ROOT.Zfs.Core" Version="1.1.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
<Content Update="appsettings.Docker.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
</Project>
28 changes: 27 additions & 1 deletion Source/Api/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

**Prequisites**
# Prequisites

Requires ZFS installed on the server

Expand Down Expand Up @@ -37,3 +37,29 @@ cd /mnt/zfsapi/zfsapi
tr -d '\r' < /mnt/zfsapi/zfsapi/1.2.3/deploy.sh > /mnt/zfsapi/zfsapi/deploy.sh

sudo sh /mnt/zfsapi/zfsapi/deploy.sh 1.2.3 all

# Docker

You can also run the api in a docker container

## SSH keys
Before building the docker image you need to ensure that whatever private/public key you want to use for ssh connections to the zfs server is present in the directory where the Dockerfile is located.

So before building, copy the keys to the same directory as the Dockerfile

## Building the image

Simply open a shell/powershell/cmd whatever up on the Source folder and run the following command to build the image

```bash
cd Source
docker build -t zfs-api:1.1.10 -f './Dockerfile' --build-arg="SSH_PRIVATE_KEY=id_rsa" --build-arg="SSH_PUBLIC_KEY=id_rsa.pub" .
```

Please note that ssh keys needs to be installed into the docker container, so its possible to run passwordless ssh to the server that runs the zfs filesystem.

The ssh keys needs to be present in the same directory as the `Dockerfile` or else the docker build process will not be able to find them, since the docker build process cannot reach file from outside of the root of the build process which is the location of the `Dockerfile`

## Running the docker container
After having built the image, simply do a:
`docker run zfs-api:1.1.10 -p 5000:5000`
17 changes: 17 additions & 0 deletions Source/Api/appsettings.Docker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Remote": {
"UseRemoteConnection": true,
"HostName": "zfsdev.root.dom",
"UserName": "bbs",
"UseSudo": true
},
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"UseLinuxAuth": false
}
55 changes: 55 additions & 0 deletions Source/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# create build environment in /Build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
WORKDIR /Build

# Copy source files
COPY ./Api/* ./Api/

# Build and publish to /Build/out
RUN dotnet build Api/Api.csproj -c Release --framework net6.0 --runtime linux-x64

RUN dotnet publish Api/Api.csproj --no-build -c Release --framework net6.0 --sc --runtime linux-x64 -o out
CMD ls -l
FROM mcr.microsoft.com/dotnet/aspnet:8.0

# install required packages
run apt-get update && apt-get -y install libpam0g-dev ssh

# Create app dir /App and copy publishing output to /App
WORKDIR /App
COPY --from=build-env /Build/out .

# create user to run our app
RUN groupadd -g 10001 dotnet && \
useradd -u 10000 -g dotnet dotnet -rmd /home/dotnet \
&& chown -R dotnet:dotnet /App

ARG SSH_PRIVATE_KEY=id_rsa
ARG SSH_PUBLIC_KEY=id_rsa.pub

# copy ssh keys from build arguments
RUN mkdir -p /home/dotnet/.ssh
COPY ${SSH_PRIVATE_KEY} /home/dotnet/.ssh/id_rsa
COPY ${SSH_PUBLIC_KEY} /home/dotnet/.ssh/id_rsa.pub

# Make dotnet user owner of everything below /home/dotnet and set correct ssh permisssions on files
RUN chown -R dotnet:dotnet /home/dotnet && chmod 700 /home/dotnet/.ssh && chmod 644 /home/dotnet/.ssh/id_rsa.pub && chmod 600 /home/dotnet/.ssh/id_rsa

# Allow docker image to connect to new ssh servers servers without prompting for a ssh key
RUN touch /etc/ssh/ssh_config.d/zfs-api.conf && echo "StrictHostKeyChecking accept-new" >> /etc/ssh/ssh_config.d/zfs-api.conf

MAINTAINER Bjorn Smith <[email protected]>
LABEL maintainer="Bjorn Smith <[email protected]>"
LABEL version="1.1.10"
LABEL description="A .NET REST API that allows you to inspect and manipulate the zfs file system \
\
See github repository for more information and source code: https://github.com/bjornbouetsmith/ROOT.Zfs.Api"

# run as the dotnet user
USER dotnet:dotnet

# Tell docker we will be exposing port 5000
EXPOSE 5000/TCP

# run the api in the asp.net environment Docker, which corresponds to the appsettings.Docker.json
ENTRYPOINT ["dotnet","Api.dll","--environment=Docker"]
2 changes: 2 additions & 0 deletions Source/ROOT.Zfs.Api.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Github", "Github", "{BEDD26
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4912DE71-350A-4036-B203-CC7A1CFD3513}"
ProjectSection(SolutionItems) = preProject
.dockerignore = .dockerignore
Dockerfile = Dockerfile
..\README.md = ..\README.md
EndProjectSection
EndProject
Expand Down
Loading