Docker Cheatsheet

Docker Cheatsheet

January 13, 2020
Cheatsheets
★ Star me on GitHub

Created by Cyanhall.

# List docker image
docker images
# Remove docker iamge
docker rmi [IMAGE ID / REPOSITORY]
# Remove all <none> images
docker rmi $(docker images -f "dangling=true" -q)
# List all docker container
docker ps -a
# Remove docker container
docker rm [CONTAINER ID]

# Inspect container
docker inspect [CONTAINER ID]
# Inspect container ip
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' [CONTAINER ID]

# Login to container
docker exec -it [CONTAINER ID or CONTAINER NAME] bash
# Execute Shell inside container
docker exec [CONTAINER ID or CONTAINER NAME] [Shell Command]

# Copying files from Docker container to host
docker cp [CONTAINER ID]:/file/path/within/container /host/path/target

Build Docker Image:

FROM alpine:latest as certs
RUN apk update && apk upgrade && apk add --no-cache ca-certificates
RUN update-ca-certificates

FROM scratch
# fix x509: certificate signed by unknown authority
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

WORKDIR $GOPATH/src/github.com/Cyanhall/Cyanhall
COPY . $GOPATH/src/github.com/Cyanhall/Cyanhall

EXPOSE 8080
ENTRYPOINT ["./Cyanhall"]
# Build from the source code at current path
# -t: Repository name (and optionally a tag) for the image
docker build -t [IMAGE NAME] .

Run:

# --name: Name of the container
# -e: Environment variables
# -d: Run in background
# -v: Bind mount a volume
# -p: Publish a container's port(s) to the host
# Postgres environment variables: 
#   POSTGRES_PASSWORD,
#   POSTGRES_USER,
#   read more: https://hub.docker.com/_/postgres
docker run -d --name cyanhall-postgres -v /my/own/datadir:/var/lib/postgresql/data -p 5432:5432 -e POSTGRES_PASSWORD=[password] -e POSTGRES_USER=root postgres

docker run -d -p 8080:8080 --name cyanhall-app --link=cyanhall-postgres:cyanhall-db [IMAGE NAME]
# Inside cyanhall-app container
# env | grep CYANHALL_DB
CYANHALL_DB_ENV_PG_MAJOR=12
CYANHALL_DB_ENV_PGDATA=/var/lib/postgresql/data
CYANHALL_DB_ENV_POSTGRES_USER=root
CYANHALL_DB_PORT_5432_TCP_ADDR=172.17.0.2
CYANHALL_DB_ENV_POSTGRES_PASSWORD=password
CYANHALL_DB_PORT_5432_TCP_PORT=5432
CYANHALL_DB_PORT=tcp://172.17.0.2:5432
CYANHALL_DB_PORT_5432_TCP=tcp://172.17.0.2:5432
CYANHALL_DB_ENV_LANG=en_US.utf8
CYANHALL_DB_PORT_5432_TCP_PROTO=tcp
CYANHALL_DB_ENV_GOSU_VERSION=1.12
CYANHALL_DB_NAME=/cyanhall-postgres/cyanhall-db
CYANHALL_DB_ENV_PG_VERSION=12.3-1.pgdg100+1