Skip to content

Commit 1f2f872

Browse files
committed
Lab 2, bonus task
1 parent 2ee58c4 commit 1f2f872

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

app_go/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.gitignore

app_go/DOCKER.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Dockerfile for `app_go`
2+
3+
## Best practices employed
4+
5+
- Explicit base image tag version.
6+
7+
- An alpine base image is used, so as to reduce the resulting image
8+
size.
9+
10+
- .dockerignore is used and is symlinked to .gitignore. What's not to
11+
appear in the repo is also not to be included in the image.
12+
13+
- Run instructions in the dockerfile are in the exec form, not shell
14+
form.
15+
16+
- A multi-stage build is used. The final stage consists of just two
17+
layers and takes up about 8MB.
18+
19+
- Root owns the executable file, which is not to be modified.
20+
21+
- A non-root user executes the app.
22+
23+
- The port to be exposed is explicitly mentioned in the Dockerfile.
24+
25+
Not sure what else to say, but just look at it: it's almost perfect!
26+
27+
The only thing one can argue about is the use of a hardcoded UID in the
28+
Dockerfile. But, firstly, there is no native way to add a user in a bare
29+
image, secondly, there is nothing to exchange with between host and container,
30+
so there's no reason to.

app_go/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM golang:1.22.0-alpine3.19 as builder
2+
3+
WORKDIR /usr/src/app/
4+
5+
COPY go.mod *.go /usr/src/app/
6+
# No dependencies yet
7+
#RUN go mod download && go mod verify
8+
9+
RUN ["env", "CGO_ENABLED=0", "go", "build", "-o", "catfact_webapp", "."]
10+
11+
12+
# Like `FROM scratch` but with SSL
13+
FROM damdo/sscratch
14+
15+
EXPOSE 5000
16+
17+
COPY --from=builder /usr/src/app/catfact_webapp /
18+
USER 2004:2004
19+
ENTRYPOINT ["/catfact_webapp"]

app_go/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,35 @@ variable).
1414

1515
In the project directory run `go build .`. This will create an executable file
1616
`catfact_webapp`. Run it to start the web app.
17+
18+
## Docker
19+
20+
To build, in the project directory do:
21+
22+
```bash
23+
docker build . -t IMAGE_TAG_NAME
24+
```
25+
26+
where `IMAGE_TAG_NAME` is the tag you want for the image.
27+
28+
### Pull
29+
30+
You can also run a pre-built version of the image. To pull it:
31+
32+
```bash
33+
docker pull kolay0ne/app_go
34+
```
35+
36+
Use `kolay0ne/app_go` as the image name for future docker operations.
37+
38+
### Run
39+
40+
Run a container based on the image, select options depending on your needs. For
41+
instance, to run it in the background, exposing the port `5000` to a randomly
42+
selected port on the host machine and automatically remove the container ones
43+
it's stopped, do:
44+
```bash
45+
docker run --rm -d -p 5000 kolay0ne/app_go
46+
```
47+
48+
Replace `kolay0ne/app_go` with your image/tag name if you built it manually.

0 commit comments

Comments
 (0)