CyanHall.com 创建于 2020-11-13, 上次更新:2021-04-30。
👉  github shields 如果有用请点赞。

1. 列出 docker 镜像

    docker images
  

2. 移除 docker 镜像

    docker rmi [IMAGE ID / REPOSITORY]
  

3. 移除所有 all <none> 镜像

    docker rmi $(docker images -f "dangling=true" -q)
  

4. 列出所有容器

    docker ps -a
  

5. 移除某个容器

    docker rm [CONTAINER ID]
  

6. 查看容器信息

    docker inspect [CONTAINER ID]
  

7. 查看容器 IP

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

8. 登录进去容器

    docker exec -it [CONTAINER ID or CONTAINER NAME] bash
  

9. 在容器内执行命令

    docker exec [CONTAINER ID or CONTAINER NAME] [Shell Command]
  

10. 从容器内部复制文件到主机

    docker cp [CONTAINER ID]:/file/path/within/container /host/path/target
  

11. 构建 Docker 镜像

    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"]
  

12. 在当前目录根据源代码构建

    # -t: Repository name (and optionally a tag) for the image
docker build -t [IMAGE NAME] .
  

13. 运行 数据库

    # --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
  

14. 运行 应用

    docker run -d -p 8080:8080 --name cyanhall-app --link=cyanhall-postgres:cyanhall-db [IMAGE NAME]
  

15. 容器环境

    # 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
  

1. 列出 docker 镜像

    docker images
  

3. 移除所有 all <none> 镜像

    docker rmi $(docker images -f "dangling=true" -q)
  

5. 移除某个容器

    docker rm [CONTAINER ID]
  

7. 查看容器 IP

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

9. 在容器内执行命令

    docker exec [CONTAINER ID or CONTAINER NAME] [Shell Command]
  

11. 构建 Docker 镜像

    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"]
  

13. 运行 数据库

    # --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
  

15. 容器环境

    # 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
  

2. 移除 docker 镜像

    docker rmi [IMAGE ID / REPOSITORY]
  

4. 列出所有容器

    docker ps -a
  

6. 查看容器信息

    docker inspect [CONTAINER ID]
  

8. 登录进去容器

    docker exec -it [CONTAINER ID or CONTAINER NAME] bash
  

10. 从容器内部复制文件到主机

    docker cp [CONTAINER ID]:/file/path/within/container /host/path/target
  

12. 在当前目录根据源代码构建

    # -t: Repository name (and optionally a tag) for the image
docker build -t [IMAGE NAME] .
  

14. 运行 应用

    docker run -d -p 8080:8080 --name cyanhall-app --link=cyanhall-postgres:cyanhall-db [IMAGE NAME]
  


Maitained by Cyanhall.com, Copy Rights @ CC BY-NC-SA 4.0     ExcelRoadMap