Docker 學(xué)習(xí)-安裝docker并創(chuàng)建鏡像

docker介紹

Docker 是一個開源的應(yīng)用容器引擎,讓開發(fā)者可以打包他們的應(yīng)用以及依賴包到一個可移植的容器中,然后發(fā)布到任何流行的 Linux 機器上,也可以實現(xiàn)虛擬化。容器是完全使用沙箱機制,相互之間不會有任何接口。

安裝過程主要轉(zhuǎn)載自官網(wǎng)

1. Build an OS

  1. Install a Linux system
    安裝Linux很多方式,你可以裝雙系統(tǒng),也可以在vmware Pro15 上安裝好Linux虛擬機。
    以下給出兩種方式的鏈接
  • 安裝雙系統(tǒng)(Ubuntu ,mint...)安裝過程簡單
  • 在vmware Pro15上安裝Linux虛擬機

2.安裝docker(ubuntu)

安裝Docker CE,使用儲存庫安裝

  1. 設(shè)置存儲庫
    (SET UP THE REPOSITORY)

1) 更新安裝包
(Update the apt package )

$ sudo apt-get update

2) 安裝包以允許apt通過HTTPS使用存儲庫
(Install packages to allow apt to use a repository over HTTPS)

   $ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

3) 添加Docker的官方GPG密鑰
(Add Docker’s official GPG key)

    $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg |  sudo apt-key add -

    $ sudo apt-key fingerprint 0EBFCD88

4) 使用以下命令設(shè)置穩(wěn)定的存儲庫
(Use the following command to set up the stable repository)

注意版本,轉(zhuǎn)載來自官網(wǎng)的提醒(版本選擇)
有道翻譯如下
注意:下面的lsb_release -cs子命令返回Ubuntu發(fā)行版的名稱,比如xenial。有時候,在Linux Mint這樣的發(fā)行版中,您可能需要將$(lsb_release -cs)更改為您的父Ubuntu發(fā)行版。例如,如果您正在使用Linux Mint Tessa,您可以使用bionic。Docker對于未經(jīng)測試和不受支持的Ubuntu發(fā)行版不提供任何保證。

原文
Note: The lsb_release -cs sub-command below returns the name of your Ubuntu distribution, such as xenial. Sometimes, in a distribution like Linux Mint, you might need to change $(lsb_release -cs) to your parent Ubuntu distribution. For example, if you are using Linux Mint Tessa, you could use bionic. Docker does not offer any guarantees on untested and unsupported Ubuntu distributions.

*   x86_64 / amd64

    $ sudo add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) \
       stable"

*   armhf

  $ sudo add-apt-repository \
     "deb [arch=armhf] https://download.docker.com/linux/ubuntu \
     $(lsb_release -cs) \
     stable"

*   arm64

  $ sudo add-apt-repository \
     "deb [arch=arm64] https://download.docker.com/linux/ubuntu \
     $(lsb_release -cs) \
     stable"

*   ppc64le (IBM Power)

  $ sudo add-apt-repository \
     "deb [arch=ppc64el] https://download.docker.com/linux/ubuntu \
     $(lsb_release -cs) \
     stable"

*   s390x (IBM Z)

  $ sudo add-apt-repository \
     "deb [arch=s390x] https://download.docker.com/linux/ubuntu \
     $(lsb_release -cs) \
     stable"
  1. 安裝DOCKER CE
    (INSTALL DOCKER CE)
  1. 更新apt包索引
    (Update the apt package index)
    $ sudo apt-get update
  1. 安裝Docker CE和containerd的最新版本,或者進入下一步安裝特定版本
    (Install the latest version of Docker CE and containerd, or go to the next step to install a specific version)
    $ sudo apt-get install docker-ce docker-ce-cli containerd.io
  1. 要安裝特定版本的Docker CE,請在repo中列出可用版本,然后選擇并安裝
    (To install a specific version of Docker CE, list the available versions in the repo, then select and install)

a. 列出你庫中可用的版本
(List the versions available in your repo)

    $ apt-cache madison docker-ce

  docker-ce | 5:18.09.1~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
  docker-ce | 5:18.09.0~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
  docker-ce | 18.06.1~ce~3-0~ubuntu       | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
  docker-ce | 18.06.0~ce~3-0~ubuntu       | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
  ...

b.要安裝特定版本的Docker CE,請在repo中列出可用的版本,然后選擇并安裝使用第二列中的版本字符串安裝特定版本,例如 5:18.09.13-0ubuntu-xenial
(Install a specific version using the version string from the second column, for example, 5:18.09.13-0ubuntu-xenial.)

   $ sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
  1. 通過運行 hello-world 鏡像來判斷 Docker CE 是否安裝成功
    Verify that Docker CE is installed correctly by running the hello-world image
    $ sudo docker run hello-world  
1.png

3. 創(chuàng)建Docker鏡像并部署

使用 Dokcerfile 自定義鏡像基礎(chǔ)

  1. 創(chuàng)建 docker hub 賬號并登陸
   $ sudo docker login
  1. 創(chuàng)建myDocker文件夾,并進入創(chuàng)建Dockerfile文件
    $ mkdir myDocker 

    $ cd my Docker && touch Dockerfile  
  1. 在Dockerfile文件中寫入如下內(nèi)容
    FROM golang
    MAINTAINER zedididi "zediv1"   
    WORKDIR $GOPATH/src/godocker  
    ADD . $GOPATH/src/godocker   
    RUN go build main.go        
    EXPOSE 8080   
    ENTRYPOINT ["./main"]  
  1. 在myDocker文件夾中,新創(chuàng)main.go文件并編寫內(nèi)容如下
  package main  
  import(   
  "net/http"  
  "fmt"  
  )  
  func main() {  
      http.HandleFunc("/zc",hello)  
      http.ListenAndServe(":8080",nil)  
  }  
  func hello(w http.ResponseWriter, r *http.Request) {  
      fmt.Fprintf(w,"Hello Docker Form Golang!")  
 }  
  1. 構(gòu)建鏡像
  docker build -t zcdocker .
2.png
  1. 查看鏡像
  docker images
3.png
  1. 運行鏡像,并端口映射
   docker run -p 8080:8080 -d zcdocker
4.png
  1. 訪問網(wǎng)址
http://localhost:8080/zc
23.png

參考資料

[1] Get Docker CE for Ubuntu.
https://docs.docker.com/install/linux/docker-ce/ubuntu/
[2] docker國內(nèi)鏡像拉取和鏡像加速registry-mirrors配置修改.
https://blog.csdn.net/u014231523/article/details/61197945
[3] Docker新手入門之三:Docker容器的基本使用.
https://baijiahao.baidu.com/s?id=1592982054546012911&wfr=spider&for=pc
[4] Docker部署Golang
https://www.cnblogs.com/dqh123/p/9481400.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容