Docker Cheatsheet
March 02, 2018
Images
Build
docker build -t img_x .- By using
.
it will look forDockerfile
in thepwd
, override this by specifying your ownDockerfile
- By using
Remove
docker image rm img_xList
docker image ls
Define Dockerfile
Extend
alpine
imageFROM alpineRUN apk add --update --no-cache -q \bash \curlExtend
debian
imageFROM debianRUN apt-get update && apt-get install -y \bash \curlMulti-stage builds
Use tooling to build an artifact
Copy that artifact into a minimal runtime
FROM golang:1.7.3WORKDIR /go/src/github.com/alexellis/href-counter/RUN go get -d -v golang.org/x/net/htmlCOPY app.go .RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffimg_x cgo -o app .FROM alpine:latestRUN apk --no-cache add ca-certificatesWORKDIR /root/COPY --from=0 /go/src/github.com/alexellis/href-counter/app .CMD ["./app"]
Containers
Run
docker run -d --name cntr_x tag img_xExecute
docker run img_x echo "hello world!"# interactive session for debuggingdocker run -it img_x /bin/bashList
docker psLogs
docker logs -f cntr_xRemove
docker rm -f cntr_x
docker-compose
A useful tool for defining how to build and orchestrate multiple containers.
Build
# alldocker-compose build# single servicedocker-compose build svc_xRun
# alldocker-compose up -d# single servicedocker-compose run svc_xStop
docker-compose down# remove volumes (e.g. wipe database)docker-compose down --volumesExecute
docker-compose run svc_x echo "hello world!"# interactive session for debuggingdocker-compose run -it svc_x /bin/bashLogs
# alldocker-compose logs -f# single servicedocker-compose logs -f svc_xRun commands against compose file with different name
docker-compose run -f ../../pew.yml
Define docker-compose.yml
Build from
Dockerfile
version: '3'services:svc_x:build:context: .dockerfile: svc_x.DockerfileDependent containers
version: '3'services:pg:image: postgres:12-alpineenvironment:POSTGRES_PASSWORD: postgresnetworks:- integrationpg_migration:image: flyway/flywaycommand: -url=jdbc:postgresql://pg:5432/postgres -user=postgres -password=postgres -connectRetries=60 migratevolumes:- ./flyway/sql:/flyway/sqlnetworks:- integrationdepends_on:- pg