代码改变世界

Docker——MacOS上安装运行docker

2018-03-18 20:29  signjing  阅读(2346)  评论(0编辑  收藏  举报

近几年来,Docker越来越流行,使用场景也越来越广泛。为了能尽快跟上时代步伐、学习并应用到实际工作中,我也开始了Docker之旅。

Docker版本

Docker共有两种版本:

  • 社区版(Community Edition,简称CE)
    • stable更新渠道:提供可靠的功能,每季度一次;
    • edge/beta更新渠道:新功能版本,每个月一次;
  • 企业版(Enterprise Edition,简称EE)
    • 企业基础版;
    • 企业标准版;
    • 企业高级版;

Docker社区版适用于开发者和小型团队,而企业版是为企业开发和IT团队设计。所以,它们对各种功能的支持程度也有所差异。我的学习阶段自然要使用社区版。

支持的平台

Docker CE和EE均支持运行在多种平台、云服务器和本机环境上。

我使用的是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,使用如下命令查看其版本号:

$ 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来测试安装包是否可以运行。
$ 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'.

  1. 运行一个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
  1. 在浏览器里运行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.
  1. 在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
  1. 在删除容器之前需要停止容器,否则会报错:
$ 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/