Docker——MacOS上安裝運行docker

近幾年來,Docker愈來愈流行,使用場景也愈來愈普遍。爲了能儘快跟上時代步伐、學習並應用到實際工做中,我也開始了Docker之旅。linux

Docker版本

Docker共有兩種版本:nginx

  • 社區版(Community Edition,簡稱CE)
    • stable更新渠道:提供可靠的功能,每季度一次;
    • edge/beta更新渠道:新功能版本,每月一次;
  • 企業版(Enterprise Edition,簡稱EE)
    • 企業基礎版;
    • 企業標準版;
    • 企業高級版;

Docker社區版適用於開發者和小型團隊,而企業版是爲企業開發和IT團隊設計。因此,它們對各類功能的支持程度也有所差別。個人學習階段天然要使用社區版。web

支持的平臺

Docker CE和EE均支持運行在多種平臺、雲服務器和本機環境上。docker

我使用的是Mac,選取的版本是CE for Mac的stable版本:
https://download.docker.com/mac/stable/Docker.dmg瀏覽器

安裝和運行Docker for Mac

1) Docker.dmg下載完成後,雙擊進行安裝。
2) 安裝完成後,在「應用」文件夾中雙擊Docker便可運行。
3) Docker運行後,其圖標會出現Mac的狀態欄裏。服務器

Docker for Mac安裝包包含了Docker Engine、 Docker命令行客戶端, Docker Compose、Docker Machine和Kitematic,使用以下命令查看其版本號:app

$ docker --version
Docker version 17.12.0-ce, build c97c6d6
$ docker-compose --version
docker-compose version 1.18.0, build 8dd22a9
$ docker-machine --version
docker-machine version 0.13.0, build 9ba6da9

運行應用

1) 打開終端,經過運行簡易的Docker鏡像hello-world來測試安裝包是否能夠運行。tcp

$ docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
Status: Downloaded newer image for hello-world:latest

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

再次運行上面命令時,則會顯示:學習

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

由上可知,先嚐試從本地啓動,若是不存在,則從Docker Hub上拉取最新版本。測試

另外,這裏的hello-world並非隨意輸入的,它是Docker的一個簡易鏡像,若是隨意輸入一個名詞,則返回以下:

$ docker run hello-everyone
Unable to find image 'hello-everyone:latest' locally
docker: Error response from daemon: pull access denied for hello-everyone, repository does not exist or may require 'docker login'.
See 'docker run --help'.

2) 運行一個Docker的web服務器。和hello-world鏡像同樣,若是本地不存在,則會從Docker Hub上拉取。

$ docker run -d -p 80:80 --name webserver nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
2a72cbf407d6: Pull complete
eccc107d7abd: Pull complete
76aa3935d77c: Pull complete
Digest: sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6
Status: Downloaded newer image for nginx:latest
7944bbef2c428b1a53c0a77e270e0820bb2e7903c7c8d3fc1bd95b48f4be27fe

再一次運行這條命令,則會報錯,緣由是命名爲webserver的服務器已經在使用中,因此能夠從新指定一個新名稱:

$ docker run -d -p 80:80 --name webserver nginx
docker: Error response from daemon: Conflict. The container name "/webserver" is already in use by container "7944bbef2c428b1a53c0a77e270e0820bb2e7903c7c8d3fc1bd95b48f4be27fe". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.

$ docker run -d -p 80:80 --name webserver_new nginx
e57fcb026c02be91929072c4aaa33a6f0420faad8a97f7d8b64f867e59a86772

3) 在瀏覽器裏運行http://localhost/,頁面顯示以下內容:

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

4) 在web服務器運行過程當中,可使用命令docker container lsdocker ps,查看容器內的詳細狀況:

$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
6cc824a7e65b        nginx               "nginx -g 'daemon of…"   24 seconds ago      Up 31 seconds       0.0.0.0:80->80/tcp   webserver

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
6cc824a7e65b        nginx               "nginx -g 'daemon of…"   32 seconds ago      Up 39 seconds       0.0.0.0:80->80/tcp   webserver

5) 在刪除容器以前須要中止容器,不然會報錯:

$ docker container rm webserver
Error response from daemon: You cannot remove a running container 6cc824a7e65b0918d9fb78cfd6b54bd95c004e38a98080a30bec1b4fd7cba511. Stop the container before attempting removal or force remove
$ docker container stop webserver
webserver

再次查看,以前運行的web服務器已不顯示了:

$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

如同linux的ls -a命令,docker container ls -a可以列出全部的容器:

$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
6cc824a7e65b        nginx               "nginx -g 'daemon of…"   4 minutes ago       Exited (0) 8 seconds ago                        webserver
da856b22da04        hello-world         "/hello"                 15 minutes ago      Exited (0) 15 minutes ago                       practical_varahamihira
317006e2577e        hello-world         "/hello"                 2 hours ago         Exited (0) 2 hours ago                          modest_mclean
72b715c6514b        hello-world         "/hello"                 7 weeks ago         Exited (0) 7 weeks ago                          compassionate_benz

刪除webserver容器,使用命令:

$ docker container rm webserver
webserver

再次查看全部容器,原先的webserver容器已經不存在了:

$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
da856b22da04        hello-world         "/hello"            15 minutes ago      Exited (0) 16 minutes ago                       practical_varahamihira
317006e2577e        hello-world         "/hello"            2 hours ago         Exited (0) 2 hours ago                          modest_mclean
72b715c6514b        hello-world         "/hello"            7 weeks ago         Exited (0) 7 weeks ago                          compassionate_benz
$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              73acd1f0cfad        4 days ago          109MB
hello-world         latest              f2a91732366c        3 months ago        1.85kB
$ docker image rm nginx
Untagged: nginx:latest
Untagged: nginx@sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6
Deleted: sha256:73acd1f0cfadf6f56d30351ac633056a4fb50d455fd95b229f564ff0a7adecda
Deleted: sha256:660d894d7e1779b260ce69426dced9f1750deb8a6505f052f61a9876991e73e6
Deleted: sha256:97e86b3c85516c6f3c848ee0df11bebe95154a567046225f1cd3a690fd22687e
Deleted: sha256:3358360aedad76edf49d0022818228d959d20a4cccc55d01c32f8b62e226e2c2

再次查看鏡像:

$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              f2a91732366c        3 months ago        1.85kB

相關連接

https://docs.docker.com/docker-for-mac/install/ https://download.docker.com/mac/stable/Docker.dmg https://docs.docker.com/docker-for-mac/ https://hub.docker.com/_/hello-world/

相關文章
相關標籤/搜索