FROM
mcr.microsoft.com/dotnet/aspnet:7.0 AS base
2
WORKDIR
/app
3
EXPOSE
80
4
5
FROM
mcr.microsoft.com/dotnet/sdk:7.0 AS publish
6
WORKDIR
/src
7
COPY
. .
8
RUN
dotnet restore /ignoreprojectextensions:.dcproj
9
WORKDIR
/src/src/Services/Catalog/Catalog.API
10
RUN
dotnet publish Catalog.API.csproj -c Release -o /app
11
12
FROM
base AS final
13
WORKDIR
/app
14
COPY
--from=publish /app .
15
ENTRYPOINT
[
"dotnet"
,
"Catalog.API.dll"
]
Creating your base image from scratch
You can create your own Docker base image from scratch. This scenario is not recommended for
someone who is starting with Docker, but if you want to set the specific bits of your own base image,
you can do so.
Additional resources
•
Multi-arch .NET Core images
.
https://github.com/dotnet/announcements/issues/14
•
Create a base image
. Official Docker documentation.
https://docs.docker.com/develop/develop-images/baseimages/
83
CHAPTER 4 | Development process for Docker-based applications
Step 3. Create your custom Docker images and embed your
application or service in them
For each service in your application, you need to create a related image. If your application is made up
of a single service or web application, you just need a single image.
Note that the Docker images are built automatically for you in Visual Studio. The following steps are
only needed for the editor/CLI workflow and explained for clarity about what happens underneath.
You, as a developer, need to develop and test locally until you push a completed feature or change to
your source control system (for example, to GitHub). This means that you need to create the Docker
images and deploy containers to a local Docker host (Windows or Linux VM) and run, test, and debug
against those local containers.
To create a custom image in your local environment by using Docker CLI and your Dockerfile, you can
use the docker build command, as in Figure 5-5.
Figure 5-5. Creating a custom Docker image
Optionally, instead of directly running docker build from the project folder, you can first generate a
deployable folder with the required .NET libraries and binaries by running
dotnet publish
, and then
use the
docker build
command.
This will create a Docker image with the name
cesardl/netcore-webapi-microservice-
docker:first
. In this case,
:first
is a tag that represents a specific version. You can repeat this step
for each custom image you need to create for your composed Docker application.
When an application is made of multiple containers (that is, it is a multi-container application), you
can also use the
docker-compose up --build
command to build all the related images with a single
command by using the metadata exposed in the related docker-compose.yml files.
You can find the existing images in your local repository by using the docker images command, as
shown in Figure 5-6.
Figure 5-6. Viewing existing images using the docker images command
84
CHAPTER 4 | Development process for Docker-based applications
Dostları ilə paylaş: |