Skip to content

Commit 8971b33

Browse files
committed
Add recipe: 'create a docker image'
1 parent d4cf9ce commit 8971b33

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

docs/recipes/build/docker-image.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# How do I build with docker?
2+
3+
Using [Docker](https://www.docker.com/) makes it possible to deploy your application as a docker container or release an image on docker hub. This recipe walks you through creating a `Dockerfile` and automating the build and test process with [Docker Hub](https://hub.docker.com/).
4+
5+
#### 1. Create a .dockerignore file
6+
7+
Create a `.dockerignore` file with the same contents as `.gitignore`
8+
9+
##### Linux
10+
```bash
11+
cp .gitignore .dockerignore
12+
```
13+
##### Windows
14+
```bash
15+
copy .gitignore .dockerignore
16+
```
17+
18+
Now, add the following lines to the `.dockerignore` file:
19+
20+
```
21+
.git
22+
```
23+
24+
#### 2. Create the dockerfile
25+
26+
Create a `Dockerfile` with the following contents:
27+
28+
```dockerfile
29+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
30+
31+
# Install node
32+
ARG NODE_MAJOR=20
33+
RUN apt-get update
34+
RUN apt-get install -y ca-certificates curl gnupg
35+
RUN mkdir -p /etc/apt/keyrings
36+
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
37+
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
38+
RUN apt-get update && apt-get install nodejs -y
39+
40+
WORKDIR /workspace
41+
COPY . .
42+
RUN dotnet tool restore
43+
RUN dotnet run Bundle
44+
45+
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine
46+
COPY --from=build /workspace/deploy /app
47+
WORKDIR /app
48+
EXPOSE 5000
49+
ENTRYPOINT [ "dotnet", "Server.dll" ]
50+
```
51+
52+
This uses [multistage builds](https://docs.docker.com/develop/develop-images/multistage-build/) to keep the final image small.
53+
54+
#### 3. Building and running with docker locally
55+
56+
1. Build the image `docker build -t my-safe-app .`
57+
2. Run the container `docker run -it -p 8080:8080 my-safe-app`
58+
3. Open the page in browser at [http://localhost:8080](http://localhost:8080)
59+
60+
61+
62+
> Because the build is done entirely in docker, Docker Hub [automated builds](https://docs.docker.com/docker-hub/builds/) can be setup to automatically build and push the docker image.
63+
64+
#### 4. Testing the server
65+
Create a `docker-compose.server.test.yml` file with the following contents:
66+
67+
```yml
68+
version: '3.4'
69+
services:
70+
sut:
71+
build:
72+
target: build
73+
context: .
74+
working_dir: /workspace/tests/Server
75+
command: dotnet run
76+
```
77+
To run the tests execute the command `docker-compose -f docker-compose.server.test.yml up --build`
78+
79+
> The template is not currently setup for automating the client tests in ci.
80+
81+
> Docker Hub can also run [automated tests](https://docs.docker.com/docker-hub/builds/automated-testing/) for you.
82+
83+
> Follow [the instructions to enable Autotest](https://docs.docker.com/docker-hub/builds/automated-testing/#enable-automated-tests-on-a-repository) on docker hub.
84+
85+
#### 5. Making the docker build faster
86+
87+
> Not recommended for most applications
88+
89+
If you often build with docker locally, you may wish to make the build faster by optimising the Dockerfile for caching. For example, it is not necessary to download all paket and npm dependencies on every build unless there have been changes to the dependencies.
90+
91+
Furthermore, the client and server can be built in separate build stages so that they are cached independently. Enable [Docker BuildKit](https://docs.docker.com/develop/develop-images/build_enhancements/) to build them concurrently.
92+
93+
This comes at the expense of making the dockerfile more complex; if any changes are made to the build such as adding new projects or migrating package managers, the dockerfile must be updated accordingly.
94+
95+
The following should be a good starting point but is not guaranteed to work.
96+
97+
```dockerfile
98+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
99+
100+
# Install node
101+
ARG NODE_MAJOR=20
102+
RUN apt-get update
103+
RUN apt-get install -y ca-certificates curl gnupg
104+
RUN mkdir -p /etc/apt/keyrings
105+
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
106+
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
107+
RUN apt-get update && apt-get install nodejs -y
108+
109+
WORKDIR /workspace
110+
COPY .config .config
111+
RUN dotnet tool restore
112+
COPY .paket .paket
113+
COPY paket.dependencies paket.lock ./
114+
115+
FROM build as server-build
116+
COPY src/Shared src/Shared
117+
COPY src/Server src/Server
118+
RUN cd src/Server && dotnet publish -c release -o ../../deploy
119+
120+
FROM build as client-build
121+
COPY package.json package-lock.json ./
122+
RUN npm install
123+
COPY src/Shared src/Shared
124+
COPY src/Client src/Client
125+
RUN dotnet fable src/Client --run npx vite build
126+
127+
128+
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine
129+
COPY --from=server-build /workspace/deploy /app
130+
COPY --from=client-build /workspace/deploy /app
131+
WORKDIR /app
132+
EXPOSE 5000
133+
ENTRYPOINT [ "dotnet", "Server.dll" ]
134+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ nav:
7070
- Upgrade from V4 to V5: "recipes/upgrading/v4-to-v5.md"
7171
- Create a new Recipe: "recipes/template.md"
7272
- Build:
73+
- Create a docker image: "recipes/build/docker-image.md"
7374
- Remove FAKE: "recipes/build/remove-fake.md"
7475
- Package my SAFE app for deployment: "recipes/build/bundle-app.md"
7576
- UI:

0 commit comments

Comments
 (0)