Skip to content
Vojtech Mašek edited this page Aug 19, 2019 · 2 revisions

Docker is used as a container solution @FlowUp.

Resources

Best practices

CLI

  • Remove garbage (untagged images)
    docker image prune
    
  • To remove container after it stops, pass flag --rm to docker run command

Naming

  • Dockerfile
  • in case you need two Dockerfiles in one folder, use something.Dockerfile, IDEs will be able provide intellisense

Single quotes vs double quotes in Dockerfile

I recommend not to use single quotes in Dockerfile, because it leads sometimes to incorrect parsing. As a result, Docker image is invalid and cannot be run, without some specific error.

This relates to commands such as ENTRYPOINT or CMD.

# Use this
ENTRYPOINT ["/bin/bash"]

# Don't use this
ENTRYPOINT ['/bin/bash']

Multi-stage builds

Sometimes, your application has needs different dependencies for build and different for runtime. To prevent packaging of non-necessary packages within Docker image and keep image size minimal, multi-stage builds can be created.

Example

Two-stage build of Go application:

# Use latest 1.11 version of Go
FROM golang:1.11

# Install dependencies
RUN go get ./...

# Static build of application
RUN CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w' -o /go/bin/app ./cmd/app/main.go

# Second stage - Alpine image
FROM alpine:latest

# Install ca-certificates (to be able use HTTPS connection within app)
RUN apk add --no-cache ca-certificates

# Copy statically build application from the previous stage
COPY --from=0 /go/bin/app /bin/app

# Set application as an image entrypoint
ENTRYPOINT ["/bin/app"]
Clone this wiki locally