text
stringlengths
0
13.4k
everything your app needs to run, and spin it up as a container on any
Docker host.
Docker can make scaling your app across multiple servers easier, too.
Once you have an image, using it to create 1 container is the same
process as creating 100 containers.
Before you start, you need the Docker CLI installed on your
development machine. Search for "get docker for (mac/windows/linux)"
and follow the instructions on the official Docker website. You can verify
that it's installed correctly with
docker version
Add a Dockerfile
The first thing you'll need is a Dockerfile, which is like a recipe that tells
Docker what your application needs to build and run.
Create a file called Dockerfile (no extension) in the root, top-level
AspNetCoreTodo folder. Open it in your favorite editor. Write the
following line:
FROM microsoft/dotnet:2.0-sdk AS build
118
Deploy with Docker
This tells Docker to use the microsoft/dotnet:2.0-sdk image as a
starting point. This image is published by Microsoft and contains the
tools and dependencies you need to execute dotnet build and compile
your application. By using this pre-built image as a starting point, Docker
can optimize the image produced for your app and keep it small.
Next, add this line:
COPY AspNetCoreTodo/*.csproj ./app/AspNetCoreTodo/
The COPY command copies the .csproj project file into the image at
the path /app/AspNetCoreTodo/ . Note that none of the actual code ( .cs
files) have been copied into the image yet. You'll see why in a minute.
WORKDIR /app/AspNetCoreTodo
RUN dotnet restore
WORKDIR is the Docker equivalent of cd . This means any commands
executed next will run from inside the /app/AspNetCoreTodo directory
that the COPY command created in the last step.
Running the dotnet restore command restores the NuGet packages
that the application needs, defined in the .csproj file. By restoring
packages inside the image before adding the rest of the code, Docker is
able to cache the restored packages. Then, if you make code changes
(but don't change the packages defined in the project file), rebuilding the
Docker image will be super fast.
Now it's time to copy the rest of the code and compile the application:
COPY AspNetCoreTodo/. ./AspNetCoreTodo/
RUN dotnet publish -o out /p:PublishWithAspNetCoreTargetManifest="
false"
119
Deploy with Docker
The dotnet publish command compiles the project, and the -o out
flag puts the compiled files in a directory called out .
These compiled files will be used to run the application with the final few
commands: