0%

Mac Docker安装配置

Docker在生产环境中的运用越来越多,已经非常成熟。这篇文章将从入门级操作视角,来讲解如何入门学习Docker。

下载Docker安装软件

Docker官网下载mac版本Docker软件。

目前的链接是:https://download.docker.com/mac/stable/Docker.dmg

双击Docker图标,运行程序。

运行docker version命令,出现如下Client和Server信息,表明安装成功了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
docker version
Client: Docker Engine - Community
Version: 18.09.2
API version: 1.39
Go version: go1.10.8
Git commit: 6247962
Built: Sun Feb 10 04:12:39 2019
OS/Arch: darwin/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

拉取镜像

执行命令docker pull hello-world

1
2
3
4
docker pull hello-world
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request
canceled while waiting forconnection (Client.Timeout exceeded while awaiting headers)

出现上述网络不通的问题,所以要换成国内的镜像源。

我目前使用的阿里云的容器镜像服务。镜像加速器地址为:https://w3344f23.mirror.aliyuncs.com。每个登录用户都不相同。

在mac系统上任何栏点击Docker小图标,Preference -> Daemon -> Registry mirrors添加上述镜像url。重启Docker。

更换国内镜像后再次执行命令,就成功了

1
2
3
4
5
6
docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
Status: Downloaded newer image for hello-world:latest

查看镜像

1
2
3
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 6 months ago 1.84kB

运行镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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/

运行Nginx镜像

拉取镜像

1
2
3
4
5
6
7
8
docker pull hub.c.163.com/library/nginx
Using default tag: latest
latest: Pulling from library/nginx
5de4b4d551f8: Pull complete
d4b36a5e9443: Pull complete
0af1f0713557: Pull complete
Digest: sha256:f84932f738583e0169f94af9b2d5201be2dbacc1578de73b09a6dfaaa07801d6
Status: Downloaded newer image for hub.c.163.com/library/nginx:latest

运行镜像

1
2
docker run -d hub.c.163.com/library/nginx
89979036016d6d09b5cf072e4ad72069e83c0c19929d439233393d0610eefb89

查看运行的容器

1
2
3
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89979036016d hub.c.163.com/library/nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 80/tcp epic_thompson

进入容器

容器就相当于一个虚拟机,我们进入到容器中看看。

1
2
docker exec -it 89979036016d bash
root@89979036016d:/#

执行ps命令

1
2
root@89979036016d:/# ps
bash: ps: command not found

命令找不到。

1
root@89979036016d:/# apt-get update && apt-get install procps

再次执行ps查看nginx进程是否启动成功。

1
2
3
4
5
6
root@89979036016d:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 08:13 ? 00:00:00 nginx: master process nginx -g daemon off;
nginx 7 1 0 08:13 ? 00:00:00 nginx: worker process
root 8 0 0 08:16 pts/0 00:00:00 bash
root 350 8 0 09:07 pts/0 00:00:00 ps -ef

设定端口转发

停止容器

1
docker stop 89979036016d

重新运行镜像

我们把主机8080端口的请求转发到容器内部的80端口,使用-p fromport:toport参数

1
2
docker run -d -p 8080:80 hub.c.163.com/library/nginx
38068e229ea71e29de8cf1992dded3520d2334122bfbdc145958788b48de563a

访问页面

http://localhost:8080

就能成功看到nginx的欢迎页面了。