Step-by-step example to scale an ASP.NET dotnet core app with Docker Swarm.
--- title: "Step-by-step: Scale ASP.NET Core with Docker Swarm" subtitle: author: Carlos Mendible date: October 30, 2016 source: https://carlos.mendible.com/2016/10/30/step-by-step-scale-asp-net-core-with-docker-swarm/ --- A few weeks ago I posted [Step by step: ASP.NET Core on Docker](https://carlos.mendible.com/2016/09/26/step-by-step-asp-net-core-on-docker/) were I showed how to build and run a [Docker](https://www.docker.com/) image with an ASP.NET Core application. Today I bring you: **Step by step: Scale ASP.NET Core with Docker Swarm** so you can scale out or in the same [application](https://github.com/cmendible/dotnetcore.samples/tree/master/docker.helloworld). Assuming you have [Docker 1.12 or later](https://www.docker.com/) installed and running, follow this steps: ## 1. Create the Dockerfile On your Docker box create a `Dockerfile` with the following contents: ```bash # We use the microsoft/dotnet image as a starting point. FROM microsoft/dotnet # Install git RUN apt-get install git -y # Create a folder to clone our source code RUN mkdir repositories # Set our working folder WORKDIR repositories # Clone the source code RUN git clone https://github.com/cmendible/aspnet-core-helloworld.git # Set our working folder WORKDIR aspnet-core-helloworld/src/dotnetstarter # Expose port 5000 for the application. EXPOSE 5000 # Restore nuget packages RUN dotnet restore # Start the application using dotnet!!! ENTRYPOINT dotnet run ``` ## 2. Create a Docker image With the `Dockerfile` in place run the following command: ```bash sudo docker build -t hello_world . ``` Now you have an image named `hello_world` with all the dependencies and code needed to run the sample. ## 3. Initialize a Swarm Initialize [Docker Swarm](https://docs.docker.com/swarm/): ```bash docker swarm init ``` ## 4. Create a Docker Service Now that you have setup everything use the following command to create a service named `hello_service` based on the `hello_world` image and start it: ```bash docker service create --name hello_service --publish 5000:5000 hello_world ``` Wait a few seconds and navigate to <http://localhost:5000> and you should be able to reach the web application. If you want to learn more about Docker services, start here: [Service Create/](https://docs.docker.com/engine/reference/commandline/service_create/) ## 5. Scale up your application Let's scale your service up to 3 instances with the following command: ```bash docker service scale hello_service=3 ``` If you want to see how many replicas your service is running issue the following command: ```bash docker service ls ``` > Note that it takes some seconds before all new replicas start. ## 6. Scale down your application ------------------------------------------------------------------------ Let's scale your service down to 1 instance with the following command: ```bash docker service scale hello_service=1 ``` ## 7. Optional: Remove the service If you are don't want the service anymore, remove it from the Swarm with the following command: ```bash docker service rm hello_service ``` You can get a copy of the [docker file here](https://github.com/cmendible/dotnetcore.samples/tree/master/docker.helloworld). Hope it helps!