1、第一个Docker镜像和容器——HelloWorldnginx
1.学习几条命令docker
2.案例:咱们从远程pull一个hello-word镜像ubuntu
(1)拉取镜像,能够看到不加[:TAG] 会默认拉取最新的浏览器
docker pull hello-word Using default tag: latest latest: Pulling from library/hello-world 5b0f327be733: Pull complete Digest: sha256:175735360662078abd70dacb73c5518d5b3ae7c1ed069d22def5da57c3e917d6 Status: Downloaded newer image for hello-world:latest
(2)查看本地镜像,REPOSITORY镜像名字,TAG镜像版本,IMAGE ID 64位字符串(这里只显示了12位,被截取了),惟一标识镜像,CREATED最后修改时间,SIZE镜像大小。bash
C:\WINDOWS\system32>docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 05a3bd381fc2 7 weeks ago 1.84kB
注意:镜像的名称为何是hello-world,这个是能够到远程镜像仓库中心能够查询到的,而pull时只提供镜像的名称,而没有仓库地址信息,则默认到Docker的镜像仓库中心http://hub.docker.com获取镜像。网络
(3)运行hello-world镜像,若是成功的显示以下,表示成功运行,且你的Docker安装是没有问题的。app
C:\WINDOWS\system32>docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 05a3bd381fc2 7 weeks ago 1.84kB C:\WINDOWS\system32>docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. 为了生成下面的消息,Docker花费了如下步骤: To generate this message, Docker took the following steps: 1. Docker 客户端链接了daemon 1. The Docker client contacted the Docker daemon. 2. Docker daemon 从Docker仓库拉取了hello-world镜像 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. Docker daemon 从这个镜像中建立了一个容器,这个容器运行了一个可执行文件,可执行文件能够产生你当前看到的输出 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. Docker daemon 把输出变成输出流给到Docker客户端,Docker客户端将它发送到了你的终端 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://cloud.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/
3.图解Docker从拉取镜像到运行镜像整个流程:tcp
2、Docker运行Nginx镜像ide
1.Nginx镜像学习
2.运行Nginx镜像步骤
(1)前往网易蜂巢镜像中心https://c.163.com/hub#/m/home/查找Nginx镜像,须要登陆!而后搜索Nginx,能够看到以下,有两个Nginx镜像,一个是library/nginx,一个是public/nginx,经过图标也能够区别,第一个也就是带有鲸鱼图标的是从Docker官网的镜像仓库同步过来的,和Dokcer的是一摸同样的,咱们使用Docker的镜像,咱们点开它,而后复制下载地址http://docker pull hub.c.163.com/library/nginx:latest。
(2)而后到终端上将它pull下来,再查看镜像,能够看到Nginx镜像成功拉取下来了!
C:\WINDOWS\system32>docker pull hub.c.163.com/library/nginx: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 C:\WINDOWS\system32>docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 05a3bd381fc2 7 weeks ago 1.84kB hub.c.163.com/library/nginx latest 46102226f2fd 6 months ago 109MB
(3)咱们使用docker run hub.c.163.com/library/nginx运行Nginx镜像
docker run hub.c.163.com/library/nginx
运行以后,你会发现没有任何反应,咱们再开一个终端查看下当前运行的容器,你能够看到Nginx其实已经运行起来了,说明这是使用前台启动的方式,咱们使用Ctrl+C把它停了,再次docker ps,发现已经中止运行了。
C:\WINDOWS\system32>docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f756bb16d3cc hub.c.163.com/library/nginx "nginx -g 'daemon ..." About a minute ago Up About a minute 80/tcp modest_poincare
其实最好的运行方式是后台运行,使用docker run -d代表运行程序在后台,并打印容器的ID (若是不知道用什么参数,可使用docker run --help查看帮助),再经过查看docker ps是否成功运行。
C:\WINDOWS\system32>docker run -d hub.c.163.com/library/nginx 6f80393759d2ac6884faa5a5ff78588e7e57838e613c054cb960513c730af981
(4)进入容器内容 docker exec -it 6f80 bash
C:\WINDOWS\system32>docker exec -it 6f80 bash root@6f80393759d2:/# ls bin dev home lib32 libx32 mnt proc run srv tmp var boot etc lib lib64 media opt root sbin sys usr root@6f80393759d2:/# which nginx /usr/sbin/nginx root@6f80393759d2:/# exit exit
3.Docker网络
(1)网络类型
Docker的隔离性,网络也是隔离性的一部分,Linux用了namespace(即命名空间)来进行资源的隔离,好比pid namespace就是用来隔离进程的,network namespace就是用来隔离网络的,每个network namespace都提供了一个独立的网络环境,包括像网卡、路由、iptable规则等都是与其余的network namespace隔离的。
(2)端口映射:在使用Bridge模式的时候,就涉及到如何让容器中的端口能够在主机上访问到(由于它有独立的namespace),这个时候就须要用到端口映射技术。Docker能够指定,你想把容器内的某一个端口和容器所在主机上的端口作一个映射,当你访问主机的端口时,其实就是访问容器里的一个端口。
咱们将Nginx容器的端口和主机的端口作一个端口映射,这样访问主机的指定端口时就能够访问Nginx容器中的指定端口了:
(1)使用docker run -d -p 8080:80 hub.c.163.com/library/nginx命令
C:\WINDOWS\system32>docker run -d -p 8080:80 hub.c.163.com/library/nginx b3d5a8b4472b0e072abdc496c2a0748a5a341a3c260ba932dcc40a0216aea7f1 C:\WINDOWS\system32>docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b3d5a8b4472b hub.c.163.com/library/nginx "nginx -g 'daemon ..." 8 seconds ago Up 7 seconds 0.0.0.0:8080->80/tcp practical_torvalds
咱们上面能够看到咱们将本机的8080端口映射到了Nginx容器中的80端口,而且成功启动,咱们在浏览器访问本机的8080端口,看是否能够访问到Nginx容器的80端口(当看到下图是,代表成功的访问到了Nginx的欢迎页):
(2)使用docker run -d -P hub.c.163.com/library/nginx命令,表示主机的全部端口能够和容器作端口映射,而且会随机分配一个端口32768映射到80端口,咱们再次使用浏览器访问本机8080端口已经没法打开了,此次须要访问分配的随机端口32768进行访问!
C:\WINDOWS\system32>docker run -d -P hub.c.163.com/library/nginx 8e539fc8a0cd3a357bc466e7cbdfb7386430ce53aa008338a2c5f21f790c014c C:\WINDOWS\system32>docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e539fc8a0cd hub.c.163.com/library/nginx "nginx -g 'daemon ..." 8 seconds ago Up 8 seconds 0.0.0.0:32768->80/tcp frosty_darwin
本文为原创文章,若是对你有一点点的帮助,别忘了点赞哦!比心!如需转载,请注明出处,谢谢!