Skip to content
This repository was archived by the owner on May 31, 2019. It is now read-only.

Commit 1ec9f89

Browse files
authored
Update the README to demonstrate multi-stage builds (#264)
[ci skip]
1 parent df6cd7a commit 1ec9f89

File tree

1 file changed

+51
-23
lines changed

1 file changed

+51
-23
lines changed

README.aspnetcore-build.md

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,50 +45,78 @@ The CI Image (`1.0-1.1`) contains both the 1.0 and 1.1 pre-restored packages. It
4545

4646
## Example Usage
4747

48-
### Build an app with `docker run`
49-
50-
You can use this container to compile your application when it runs. If you use the [Visual Studio tooling](https://blogs.msdn.microsoft.com/webdev/2016/11/16/new-docker-tools-for-visual-studio/) to setup CI/CD to Azure Container Service then this method of using the build container is used.
51-
52-
Run the build container, mounting your code and output directory, and publish your app:
48+
### Build an app with `docker build`
5349

54-
```
55-
docker run -it -v $(PWD):/app --workdir /app microsoft/aspnetcore-build bash -c "dotnet restore && dotnet publish -c Release -o ./bin/Release/PublishOutput"
56-
```
50+
With this technique your application is compiled in two stages when you run `docker build`. The requires Docker 17.05 or newer.
5751

58-
After this has run the application in the current directory will be published to the `bin/Release/PublishOutput` directory.
52+
Stage 1 compiles and publishes the application by using the `microsoft/aspnetcore-build` image. Stage 2 copies the published application
53+
from Stage 1 into the final image leaving behind all of the source code and tooling needed to build.
5954

60-
### Build an app with `docker build`
55+
1. Create a `.dockerignore` file in your project folder and exclude files that shouldn't be copied into the container:
6156

62-
With this technique your application is compiled when you run `docker build` and you then copy the binaries out of the built image.
57+
```
58+
# Sample contents of .dockerignore file
59+
bin/
60+
obj/
61+
node_modules/
62+
```
6363
64-
1. Create a Dockerfile to build your application (`Dockerfile.build` is a common name used).
64+
1. Create a `Dockerfile` in your project:
6565
6666
```Dockerfile
67-
FROM microsoft/aspnetcore-build
68-
WORKDIR /app
67+
# Sample contents of Dockerfile
68+
# Stage 1
69+
FROM microsoft/aspnetcore-build AS builder
70+
WORKDIR /source
6971
72+
# caches restore result by copying csproj file separately
7073
COPY *.csproj .
7174
RUN dotnet restore
7275
76+
# copies the rest of your code
7377
COPY . .
74-
RUN dotnet publish --output /out/ --configuration Release
78+
RUN dotnet publish --output /app/ --configuration Release
79+
80+
# Stage 2
81+
FROM microsoft/aspnetcore
82+
WORKDIR /app
83+
COPY --from=builder /app .
84+
ENTRYPOINT ["dotnet", "myapp.dll"]
7585
```
7686
77-
2. Build your image:
87+
This approach has the advantage of caching the results of `dotnet restore` so that packages are not downloaded unless you change your
88+
project file.
89+
90+
1. Build your image:
91+
92+
```
93+
$ docker build -t myapp .
94+
```
95+
96+
1. (Linux containers) Start a container from your image. This will expose port 5000 so you can browse it locally at <http://locahost:5000>.
7897
7998
```
80-
$ docker build -t build-image -f Dockerfile.build
99+
$ docker run -it -p 5000:80 myapp
81100
```
82101
83-
3. Create a container from your image and copy your built application out.
102+
1. (Windows containers) Start a container from your image, get its assigned IP address, and then open your browser to the IP address
103+
of the container on port 80. To see console output, attach to the running container or use `docker logs`.
84104
85105
```
86-
$ docker create --name build-cont build-image
87-
$ docker cp build-cont:/out ./bin/Release/PublishOutput
106+
PS> docker run --detach --name myapp_container myapp
107+
PS> docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' myapp_container
108+
PS> docker attach myapp_container
88109
```
89110
90-
After this the application in the current directory will be published to the `bin/Release/PublishOutput` directory.
111+
### Build an app with `docker run`
112+
113+
You can use this container to compile your application when it runs. If you use the [Visual Studio tooling](https://blogs.msdn.microsoft.com/webdev/2016/11/16/new-docker-tools-for-visual-studio/) to setup CI/CD to Azure Container Service then this method of using the build container is used.
91114
92-
From here you could construct an optimized runtime image with the `microsoft/aspnetcore` image or just deploy/run the binaries as normal without using Docker at runtime.
115+
Run the build container, mounting your code and output directory, and publish your app:
116+
117+
```
118+
docker run -it -v $(PWD):/app --workdir /app microsoft/aspnetcore-build bash -c "dotnet restore && dotnet publish -c Release -o ./bin/Release/PublishOutput"
119+
```
120+
121+
After this has run the application in the current directory will be published to the `bin/Release/PublishOutput` directory.
93122
94-
This approach has the advantage of caching the results of `dotnet restore` so that packages are not downloaded unless you change your project file.

0 commit comments

Comments
 (0)