Skip to main content

在windows上使用docker

  1. 安裝docker for window

  2. 啟動wsl,並安裝ubuntu18.04版本(如果安裝不同版本,在後續的docker的設定上會有錯誤出現)

  3. 依照這個網頁來實做

    1. Install Docker

      # Update the apt package list.
      sudo apt-get update -y

      # Install Docker's package dependencies.
      sudo apt-get install -y \
      apt-transport-https \
      ca-certificates \
      curl \
      software-properties-common

      # Download and add Docker's official public PGP key.
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

      # Verify the fingerprint.
      sudo apt-key fingerprint 0EBFCD88

      # Add the `stable` channel's Docker upstream repository.
      #
      # If you want to live on the edge, you can change "stable" below to "test" or
      # "nightly". I highly recommend sticking with stable!
      sudo add-apt-repository \
      "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
      $(lsb_release -cs) \
      stable"

      # Update the apt package list (for the new apt repo).
      sudo apt-get update -y

      # Install the latest version of Docker CE.
      sudo apt-get install -y docker-ce

      # Allow your user to access the Docker CLI without needing root access.
      sudo usermod -aG docker $USER
      1. Install Docker Compose

        # Install Python 3 and PIP.
        sudo apt-get install -y python3 python3-pip

        # Install Docker Compose into your user's home directory.
        pip3 install --user docker-compose
      2. $HOME/.local/bin is set on your WSL $PATH

        sudo vi ~/.profile
        # 在最後一行新增
        export PATH="$PATH:$HOME/.local/bin"
        # 存檔
        source ~/.profile #使設定生效
      3. 讓wsl連到window上的docker

        echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc && source ~/.bashrc
      4. docker在window上預設不能掛載subfolder,只能掛載內建預設的volume,所以如果需要複製檔案進container,要在dockerfile用cpoy的方式進行

      5. 如果要使用subfolder,要進行以下設定(版本為ubuntu18.04)

        sudo vi /etc/wsl.conf
        # Now make it look like this and save the file when you're done:
        [automount]
        root = /

        存檔後logout或是restart即可

  4. 如果有在windows中編輯*.sh檔,要注意,檔案的製作需要在linux的環境下創立,而且最後不能有enter

    ,不然容易會有結尾符號linux不認識,導致container crash(如果在window下編輯,更新完後需要透過wsl用vi/nano重新複製一份)

  5. 將需要的dockerfile以及docker-compose.yaml準備好

  6. 啟動的方式建議不要使用快取

    docker-compose build --no-cache
    docker-compose up --force-recreate #這行為強制重新建立
    docker-compose up -d --force-recreate #這行為背景運作
  7. 關閉的方法

    docker-compose down
    docker volume ls # 查看volume的使用
    docker volume rm volumeName # 刪除volume內容

參考網頁

setup docker on wsl