这样的 DOCKER你总说不会,现在就在你面前,你还不赶快努力起来

 

开始安装:


由于国内的源比较卡,所以我们推荐使用daocloud的方式,注册安装即可:

[root@localhost ~]# curl -sSL https://get.daocloud.io/docker | sh
  • 1

为了快速拉取镜像,还要接daocloud.因为在国内通常拉取Docker hub的镜像都是被墙的。

[root@localhost ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://681a96df.m.daocloud.io
docker version >= 1.12
{"registry-mirrors": ["http://681a96df.m.daocloud.io"],
    "live-restore": true
}
Success.
You need to restart docker to take effect: sudo systemctl restart docker
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

实际上是相关配置写入了,

[root@localhost ~]# cat /etc/docker/daemon.json
{"registry-mirrors": ["http://681a96df.m.daocloud.io"],
    "live-restore": true
}
  • 1
  • 2
  • 3
  • 4

指定去这个仓库下,因为默认是去官方的Docker hub下载镜像的。 
然后重启docker:

systemctl restart docker
  • 1
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375  ##tcp
  • 1

2、我们也可以看一下到底docker 读取的是哪个配置文件,不同版本可能不一样:

[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since 六 2018-08-18 11:46:11 CST; 42s ago
     Docs: https://docs.docker.com
 Main PID: 40649 (dockerd)
    Tasks: 29
   Memory: 47.4M
   CGroup: /system.slice/docker.service
           ├─40649 /usr/bin/dockerd
           └─40657 docker-containerd --config /var/run/docker/containerd/containerd.toml

8月 18 11:46:10 localhost.localdomain dockerd[40649]: time="2018-08-18T11:46:10.860628488+08:00" level=info msg="pickfirstBalancer:...e=grpc
8月 18 11:46:10 localhost.localdomain dockerd[40649]: time="2018-08-18T11:46:10.860794413+08:00" level=info msg="pickfirstBalancer:...e=grpc
8月 18 11:46:10 localhost.localdomain dockerd[40649]: time="2018-08-18T11:46:10.860811302+08:00" level=info msg="Loading containers: start."
8月 18 11:46:11 localhost.localdomain dockerd[40649]: time="2018-08-18T11:46:11.113070064+08:00" level=info msg="Default bridge (do...dress"
8月 18 11:46:11 localhost.localdomain dockerd[40649]: time="2018-08-18T11:46:11.164857941+08:00" level=info msg="Loading containers: done."
8月 18 11:46:11 localhost.localdomain dockerd[40649]: time="2018-08-18T11:46:11.181541949+08:00" level=info msg="Docker daemon" com...6.0-ce
8月 18 11:46:11 localhost.localdomain dockerd[40649]: time="2018-08-18T11:46:11.181666174+08:00" level=info msg="Daemon has complet...ation"
8月 18 11:46:11 localhost.localdomain dockerd[40649]: time="2018-08-18T11:46:11.189756196+08:00" level=warning msg="Could not regis...$PATH"
8月 18 11:46:11 localhost.localdomain dockerd[40649]: time="2018-08-18T11:46:11.195308654+08:00" level=info msg="API listen on /var....sock"
8月 18 11:46:11 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

这里我们知道centos读取的是/usr/lib/systemd/system/docker.service这个文件。启动的相关参数,后面再慢慢开始

Tcp: 
Vim /usr/lib/systemd/system/docker.service

[root@master ~]# docker -H 192.168.63.200:2375 images

三、开始基本操作:

1、查看有没有镜像:

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
upstram_nginx       latest              f3c5dcc79825        8 days ago          669 MB
lnmp/nginx          0.2                 9fe8cdcb3e43        8 days ago          669 MB
  • 1
  • 2
  • 3
  • 4

一个镜像的标识由仓库信息+tag构成 
比方说我们启动可以用Image ID来标识,或者lnmp/nginx:0.2这样标识,不写后面默认是lastest

2、有镜像之后我们可以启动一个容器:

[root@localhost ~]# docker run -dit -P nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
be8881be8156: Pull complete 
32d9726baeef: Pull complete 
87e5e6f71297: Pull complete 
Digest: sha256:4ffd9758ea9ea360fd87d0cee7a2d1cf9dba630bb57ca36b3108dcd3708dc189
Status: Downloaded newer image for nginx:latest
cf05d1459d716965c21e8c30fff3dede10744029cbc46db4620e532789762448
[root@localhost ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

docker run 后面常用选项:

--name:指定容器名称
-P:随机映射端口
-v:实现文件或者目录挂载
-i:交互
-t:tty
  • 1
  • 2
  • 3
  • 4
  • 5

我们启动一个容器,默认没有指定后面的tag标签就是变成:nginx:latest,然后我们可以看到本地没有镜像的话就会去远端的Docker hub下载。

3、查看一下docker 当前运行有哪些镜像:

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
cf05d1459d71        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes        0.0.0.0:32768->80/tcp   serene_boyd
[root@localhost ~]#
  • 1
  • 2
  • 3
  • 4

当容器停止之后,我们需要用docker ps –a来查看所有镜像。

可以看到当前运行一个nginx的镜像,名称因为我们没有指定是随机起的叫gifted_aryabhata。然后nginx 的80端口映射到本机的32768端口。

4、测试一下:

[root@localhost ~]# curl 127.0.0.1:32768
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

可以看到是能够访问的。

5、我们可以查看相关容器的日志:

[root@localhost ~]# docker logs cf05d1459d71
172.17.0.1 - - [18/Aug/2018:04:00:05 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@localhost ~]#
  • 1
  • 2
  • 3

6、当然我们也可以登录容器:

有需要的联系我2317384986

[root@localhost ~]# docker exec -it cf05d1459d71 /bin/bash
root@cf05d1459d71:/#

猜你喜欢

转载自blog.csdn.net/qq_42851004/article/details/81837352
今日推荐