cropse

Docker cheat sheet 2017- 3-24


這是我自己整理的docker cheatsheet,總之就是一邊用一邊更新自己使用過的指令

Useage:

docker [option] {Command}

背景執行請用 -it -d, 這樣後面就可以用logs去查背景輸出

Command:

  • inspect {container_id} looking for detail , json form
  • logs {container_id} inquery process log in background
  • ps inquery running container
    • -a : all container, according terminated
    • -q : only inquery id
  • images check image
    • -q : only id
  • commit {container_id} {repository_name:tag}
  • build build Dockerfile
    -t {Tag name} : add Tag
  • rm delete container
  • login login docker registry to be ready to push
  • rmi delete image
  • run: run docker container from images
    • -i interactive mode
    • -t pseudo TTY mode
    • -d background mode
    • -p {local host port}:{container vps port} porting mapping
    • -v {Absolute local path}:{Absolute container path} mount volume
    • -p {host port}:{container port} port forward
    • -v {host_path}:{container_path} mount volume from host

example:
docker build --tag aiohttp:0.1 .
docker run -it -p 9898:80 -v $(pwd)/example:/run aiohttp:0.1 bash

  • docker exec -it {container} {command}: 繼續執行command
    • ex:docker exec -it test ps aux

Delete all containers
docker rm $(docker ps -a -q)
Delete all images
docker rmi $(docker images -a -q)
Login to running container
docker exec -it {container_id} bash
delete all volume not used
docker volume rm $(docker volume ls -f 'dangling=true' -q)


Dokcer Save&Export

可以打包container,打包後直接丟給別人

先講結論:

  • export: 打包container出來,image會被合併成一包,使用import匯入
  • save: 把container的紀錄給保存下來,會保留image的安裝紀錄,安裝也是批次安裝,使用load匯入

使用方法:

export

  • docker export {container_id} > export.tar
  • cat export.tar | sudo docker import - {repository_name:tag}

save

  • docker save {container_id} > save.tar
  • docker load < save.tar

Dockerfile

FROM {OS}
RUN {shell_command} command on build image
CMD {shell_command} command after container build
COPY {localfile_path} {container_path} cp指令
ADD 類似wget之類的透過URL下載,可以自動解壓縮,但是非必要建議COPY
ENV {key} {value} set the enveriment
WORKDIR /path/to/workdir set Working dir
EXPOSE {local host port} {container vps port} 需要加-P才會生效
Dockerfile 解說


Docker Compose Commands

docker compose up starts up all the containers.
docker compose ps checks the status of the containers managed by docker compose.
docker compose logs outputs colored and aggregated logs for the compose-managed
containers.
docker compose logs -f outputs appended log when the log grows.
docker compose logs {container name} in the end outputs the logs of a specific
container.
docker compose stop stops all the running containers without removing them.
docker compose rm removes all the containers.
docker compose build rebuilds all the images.


Docker-compose 不需要做link,內部的port根據services名稱方便管理跟開關功能

Note

  • alpine交互指令用bin/sh而不是bin/bash

Vloume

Volume 在dokcerfile無法指定local path
指定local path會把container path的資料清空,需要注意一下

深入理解Docker Volume(一)
Docker volume 簡單用法

Tag: docker