0%

(Linux)(Ubuntu) Docker

環境

Ubuntu 20.04 (64 Bits)

安裝

  • 執行下列指令
    1
    2
    3
    4
    5
    6
    7
    8
    9
    sudo apt-get remove docker docker-engine docker.io containerd runc
    sudo apt-get update
    sudo apt-get install ca-certificates curl gnupg lsb-release
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io

基本操作

Image操作

  • 安裝 (範例:ubuntu image)

    1
    2
    docker search ubuntu -f is-official=true
    docker pull ubuntu
  • 移除

    1
    2
      docker rmi <Image name/Image ID>
    * 操作內容

    docker run -it ubuntu /bin/bash

    1
    2
    3
    * 自訂Image
    * By Interaction
    1. 操作內容
      docker run -it ubuntu /bin/bash 
      
    1
    2
    3
    4
    5
    6
    2. 手動安裝環境
    3. 暫時離開container

    ctrl+p, ctrl+q

    4. 查詢container名稱
    docker container ls -a
    1
    5. 建立Image
    docker commit -m <message> -a <author> <container ID> <tag>
    1
    2
    * Dockerfile方法
    1. 編寫Dockerfile (範例: Hexo)
    FROM ubuntu MAINTAINER Neil ARG DEBIAN_FRONTEND=noninteractive ENV TZ=Asia/Taipei RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN apt-get update RUN apt-get --yes install git RUN apt-get --yes install curl dirmngr apt-transport-https lsb-release ca-certificates RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - RUN apt-get update RUN apt-get --yes install nodejs RUN npm install hexo-cli -g
    1
    2. 建立Image
    docker build -t <image name> -f <docker-file> <path>
  • Export Image

    1
    docker export <.tar>
  • Import Image

    1
    docker import <.tar>

    Container操作

  • 列出所有container狀態

    1
    2
    docker ps -a
    docker container ls -a
  • 移除所有停止的container

    1
    docker container prune
  • Detaching Without Stopping container

    ctrl + p, ctrl + q

  • Reattaching Container

    1
    docker attach <container>
  • 使用root身份登入

    1
    docker exec -u 0 -it <container ID> /bin/bash
  • Docker volume & network (host)

    1
    docker run --net=host -it -v <host path>:/<container path> <image ID> /bin/bash

    問題

    Container中apt install自動補全

    安裝bash-completion

    1
    apt install bash-completion

    編輯/bin/bash.bashrc

    1
    2
    3
    4
    5
    6
    7
    if ! shopt -oq posix; then
    if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
    elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
    fi
    fi

    移除docker.clean

    1
    2
    rm /etc/apt/apt.conf.d/docker.clean
    apt-get update

更改Docker Image & Container預設安裝位置

方法一

停止docker daemon

1
sudo service docker stop

編輯/etc/docker/daemon.json,指定新位置

1
2
3
{
"data-root": "/path/to/your/docker"
}

複製現有docker images/ containers到新位置

1
sudo rsync -aP /var/lib/docker/ /path/to/your/docker

重起docker daemon

1
sudo service docker start

方法二

Stop docker daemon.

1
/etc/init.d/docker stop
1
ps aux|grep docker

Move the contents of /var/lib/docker to your new location.

1
mv /var/lib/docker /home/
1
ln -s /home/docker/ /var/lib/docker

Start docker daemon.

1
/etc/init.d/docker start