創(chuàng)建一個基于docker的http應用(一)

本教程屬于初學者教程,大神請自動略過。
Docker 是一個開源程序,它可以將應用及其完整的依賴包捆綁到一起,并打包為容器。在云大行其道的今天,虛擬化發(fā)揮了其無限潛能。未來會有更多的服務部署在云端。在實際開發(fā)的過程中,常常會因為現(xiàn)網(wǎng)環(huán)境和開發(fā)環(huán)境不一致導致各種各樣的問題出現(xiàn),但是如果我們利用docker則會完全規(guī)避這個問題。docker的好處這里就不一一列舉了,接下來,讓我們從0開始,創(chuàng)建一個dock的http應用吧。無論之后要開發(fā)多么復雜的應用,這個教程都會使你收益匪淺。


知識點&目錄

1、在windows上安裝docker
2、wsl下安裝docker,配置daemon
3、編寫一個Hello Container的基于http web的應用
4、dockerfile初探
5、構建/運行容器


正文

一、在windows安裝docker應用
如果你是在玩docker toolbx你可以參考這篇文章。
http://www.itdecent.cn/p/aa41f5b77aea
下面我們來看下怎么玩docker for windows:
1、在docker官網(wǎng)下載docker for windows,為什么說坑爹的,現(xiàn)在的docker竟然要求先注冊才能下載。

安裝圖標

點擊安裝:一直next,此處省略10個字
安裝完成后在桌面會出現(xiàn)一個docker的圖標,雙擊即可運行。
運行圖標

狀態(tài)欄右下角出現(xiàn)圖標:

即表示docker服務已經(jīng)成功拉起。
如果你想在wsl編譯運行容器,則需要右擊圖標->settings,有以下界面,選擇Expose daemon on tcp://localhost:2375 without TLS,至于為什么,稍后就會揭曉。
setting

。
以上windows端的docker安裝就完成了,是不是太easy了!

二、wsl端的docker安裝
這里所述的wsl均是基于ubuntu的,其他系統(tǒng)換湯不換藥。
更新源:apt-get update
安裝docker: apt-get install docker
讀條完畢,至此wsl的docker也安裝完成了。是不是更簡單。

**三、測試docker是否安裝成功
鍵入:docker version

Client:
 Version:           18.09.7
 API version:       1.39
 Go version:        go1.10.1
 Git commit:        2d0083d
 Built:             Wed Jul  3 12:13:59 2019
 OS/Arch:           linux/amd64
 Experimental:      false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

出現(xiàn)以上界面,就說明docker已經(jīng)安裝成功了。

但是下面顯示
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
是因為wsl目前不支持docker daemon,需要使用Docker CLI連接到windows通過Docker for Windows或您創(chuàng)建的遠程Docker守護進程。之前已經(jīng)在windows端開啟docker服務的時候已經(jīng)在setting中開啟了Expose daemon on tcp://localhost:2375 without TLS這個選項.
設置一下環(huán)境變量:export DOCKER_HOST=tcp://127.0.0.1:2375

Client:
 Version:           18.09.7
 API version:       1.39
 Go version:        go1.10.1
 Git commit:        2d0083d
 Built:             Wed Jul  3 12:13:59 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.2
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.6
  Git commit:       6247962
  Built:            Sun Feb 10 04:13:06 2019
  OS/Arch:          linux/amd64
  Experimental:     false

可以看到Client 和 Server都起來了。

四、來玩?zhèn)€hello world吧。
輸入:docker pull hello-world來拉取一個hello world的image
查看image: docker image ls
看到hello-world的image已經(jīng)存在:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        6 months ago        1.84kB

運行image container:
輸入:docker run hello-world
顯示如下:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

至此WSL下的docker環(huán)境就已經(jīng)安裝完成,下一篇教程我們來自己編寫一個hello docker的web應用??梢栽跒g覽器顯示的那種。
快速跳轉。
http://www.itdecent.cn/p/ac1a1b4c087d

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

相關閱讀更多精彩內(nèi)容

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