ca9163d9
ca9163d9

Reputation: 29209

How to publish .Net core docker application from Windows to Linux machine?

I created a .Net core application with Linux docker support using Visual Studio 2017 on a Windows 10 PC with Docker for Windows installed. I can use the following command to run it (a console application)

docker run MyApp

I have another Linux machine with Docker installed. How to publish the .Net core application to the Linux machine? I need to publish and run the dockerized application on the Linux machine.

The linux has the following docker packages installed.

$ sudo yum list installed "*docker*"
Loaded plugins: amazon-id, rhui-lb, search-disabled-repos
Installed Packages
docker-engine.x86_64                                17.05.0.ce-1.el7.centos                         @dockerrepo
docker-engine-selinux.noarch                        17.05.0.ce-1.el7.centos                         @dockerrepo

Upvotes: 0

Views: 2683

Answers (1)

rekiem87
rekiem87

Reputation: 1573

There are many ways to do this, just search for any tool for CI/CD.

The easiest way to do it is manually, connect to your Linux server, make a git pull of the code and then run the same commands that you run locally.

Other option is to do a push of your docker image to a container registry, then do a pull in you docker server and you are ready to go

Edit:

You should really take a look to some CI service, for example, in our environment, we use GitLab, when we do a push to master there is a gitlab.yml that builds the project, then do a push:

image: docker:latest
services:
- docker:dind

stages:
- build

api:
  variables:
    IMAGE_NAME: git.lagersoft.com:4567/gumbo/vtae/api:${CI_BUILD_REF}
  stage: build
  only:
    - master
  script:
    - docker build -t ${IMAGE_NAME} -f vtae.api/Dockerfile .
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN ${IMAGE_NAME}
    - docker push ${IMAGE_NAME}

With this we only need to do a pull in our server with the latest version.

It's worth noticing that docker by itself does not handle the publication part, so you need to do it manually or with some tool (any CI tool like gitlab, jenkins, circleci, amazon code pipeline...) if you are starting learning I would recommend to start manually and then integrate some CI tool.

Edit 2

About the Visual Studio tool, I would not recommend to use it for anything else than local development, since yeah, it only works in windows and it only works in visual studio (Rider has integrated just very recently), so, to do the deploy in a linux environment we use our own docker and docker compose files, they are based in the defaults anyway, they are something like this:

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY lagersoft.common/lagersoft.common.csproj lagersoft.common/
COPY vtae.redirect/vtae.redirect.csproj vtae.redirect/
COPY vtae.data/vtae.data.csproj vtae.data/
COPY vtae.common/vtae.common.csproj vtae.common/
RUN dotnet restore vtae.redirect/vtae.redirect.csproj
COPY . .
WORKDIR /src/vtae.redirect
RUN dotnet build vtae.redirect.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish vtae.redirect.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "vtae.redirect.dll"]

This docker file copy all the related projects (I hate the copying part, but is the same as Microsoft do their default file), the do the build and publish the app, on the other hand we have a docker-compose to add some services (this files must be in the solution folder to access all the related projects):

version: '3.4'

services:  
  vtae.redirect.redis:
    image: redis
    volumes:
      - "./volumes/redirect/redis/data:/data"
    container_name: vtae.redirect.redis

  vtae.redirect:
    image: vtae.redirect
    depends_on:
      - vtae.redirect.redis
    build:
      context: .
      dockerfile: vtae.redirect/Dockerfile
    ports: 
      - "8080:80"
    volumes:
      - "./volumes/redirect/data:/data"
    container_name: vtae.redirect
    entrypoint: dotnet /app/vtae.redirect.dll

With this parts there is only left to do a commit, then a pull in the server and run the docker-compose up command to run our app (you could do it from the docker file directly, but it is easier and more manageable with docker compose.

Edit 3

To make the deployment in the server we use two tools.

  • First the gitlab ci is run after the commit is done
  • It makes the build specified in the docker file and pushes it to our Gitlab container registry, same if it was the container registry of amazon, google, azure... etc...
  • Then it makes a post request to the server in production, this server is running a special tool in a separate port
  • The server receive the post request and validates it, for this we use this tool (a friend is the repo owner)
  • The script receive the request, check the login, and if it is valid, then it simply does the pull from our gitlab container registry and run docker-compose up

Notes

The tool is not perfect, we are moving from just docker to use kubernetes, were you can connect to your cluster directly from your machine or some CI integration and do the deploys directly, no matter what solution do you choose, i recommend you that start to see how kubernetes can help you, sadly is one more layer to learn, but it is very promising, were you will be able to publish to almos any cloud or metal painless, with fallbacks, scaling and other stuff.

Also If you do not want or can not use the container registry (I strongly recommend this way), you can use the same tool, in the .sh that executes it, just do a git pull and then a docker build or docker compose. The most simple scenario could be to create an script yourself where you do ssh to the server, upload the files as zip and then run it in the server, remember, Ubuntu is in the microsoft store and could run this script, but the other solutions are more "independient" and scalable, so, make your choose!

Upvotes: 6

Related Questions