To make an effective Docker image for your application, start by picking the environment you want to run your app in. You built your app with ASP.NET Core 2.1, so you'll need an environment that can run that framework.
Microsoft provides dotnet
Docker images on Docker Hub that you can use as a base. These images give you a minimal Linux environment that's fully prepared to run .NET Core apps.
Many other maintainers provide similar images for their own applications, too. For example, you might use an nginx image to start with a webserver that's ready to go, MySQL for a database, or golang to build apps with the Go programming language.
Take a look at the common tags listed on the dotnet
repository. One of them is 2.1-aspnetcore-runtime
, which is exactly what you'll need to run your ASP.NET Core app. To specify a specific image by its tag, add a colon after the repository name (microsoft/dotnet:2.1-aspnetcore-runtime
). If you don't specify a tag, Docker will automatically try to use the latest
tag.
Now that you've picked microsoft/dotnet:2.1-aspnetcore-runtime
as your base image, you can start preparing a Dockerfile.
A Dockerfile is a set of instructions to prepare a Docker image. It starts from a base image, follows a set of commands, and then takes a snapshot of the environment after the commands are run. The result of this process is the Docker image.
When you run a Docker image, it "wakes up" the snapshot, turns it into a container, and starts running.
To build a Docker image for your app, you'll want to:
- Build your project with Linux as your target runtime.
- Copy the output of your build into a Docker image.
- Configure the image to run your app when it starts up.
The following is an example of a Dockerfile that builds a Python project. Use it as reference to help you build an image for your ASP.NET Core project. The official Dockerfile reference page is also helpful.
# Use an official Python runtime as a base image
FROM python:2.7-slim
# Copy the current directory contents into the container at /pythonapp
ADD . /pythonapp
# Define an environment variable
ENV PYTHON_VERSION 2.7
# Set the working directory to /pythonapp
WORKDIR /pythonapp
# Run the command 'python app.py' when the container launches
ENTRYPOINT ["python", "app.py"]
Add a new file at the root of your project named Dockerfile
. Follow the above example, but use microsoft/dotnet:2.1-aspnetcore-runtime
as a base and build a set of instructions to copy your app into the image and run it.
Note: When your app starts up, it searches the current working directory for appsettings.json
files - not the directory the app lives in. Make sure you set the right working directory in your Dockerfile so your app will know where to look for its configuration files.
Run docker build .
from the folder containing your Dockerfile to build your Docker image. You can (and should) add the --tag
parameter (often shortened to -t
) to name your image.
docker build . --tag mywebapi
When the build starts, you'll see it go through your Dockerfile line by line, displaying each instruction as it progressively builds the image. When it succeeds, you'll see Successfully built xxxxxxxxx
and Successfully tagged mywebapi
in the output.
After you build an image, you can see it (and any other Docker images on your machine) by running docker image ls
. The newest images will show at the top of the list, so the one you just built should be at the very top with the tag you specified displayed in the REPOSITORY
column.
You can also filter the list of images to your specific image by adding the repository name as an extra parameter to the docker image ls
command.
docker image ls mywebapi
- Use
dotnet publish
to generate an executable Linux version of your app- Set the build configuration to
Release
- Set the runtime to
linux-x64
- Set the output directory to
./output
- Set the build configuration to
- Create a Dockerfile that:
- Starts from
microsoft/dotnet:2.1-aspnetcore-runtime
- Copies the content of
./output
into the/app
folder inside the image - Sets the
ASPNETCORE_ENVIRONMENT
environment variable toProduction
- Sets the working directory to
/app
so the app will be able to find its configuration files - Starts running your app, which should be located at
/app/WebAPI
inside the image
- Starts from
- Use the Dockerfile to build an image
- Use the Dockerfile reference page to help make sense of the different Dockerfile instructions.
- Run
docker image ls
and find the image you created. - Make sure the image is tagged as
mywebapi
.