首页 > 其他 > 详细

2,docker的基本操作

时间:2020-08-16 19:04:02      阅读:83      评论:0      收藏:0      [点我收藏+]
Docker组成:Docker client 和 Docker Server
Docker 组件:镜像(image),容器(container),仓库(repository)
搜索镜像:docker search
获取镜像:docker pull
查看镜像:docker images
删除镜像:docker rmi

[root@localhost ~]# docker search centos  ## 它会去docker Hub 上搜索镜像
[root@localhost ~]#rz -y 选择 centos.tar镜像包
[root@localhost ~]# docker load --input centos.tar #导入镜像 
docker load --input centos.tar 或 docker load < centos.tar
[root@localhost ~]# docker images  #查看本地镜像
\REPOSITORY          TAG        IMAGE ID         CREATED     SIZE
docker.io/centos        latest      970633036444    6 weeks ago    196.7MB
[root@localhost ~]#
[root@localhost ~]# docker pull centos #先拉取下来镜像包
[root@localhost ~]# docker save centos -o centos.tar  #将上面的centos镜像保存成一个tar文件


[root@localhost ~]# docker run centos /bin/echo "Hello world"  #起centos镜像,并输出内容。(centos 镜像名称必须在所有参数的最后)
Hello world
[root@localhost ~]# docker ps -a     #查看所有容器


[root@localhost ~]# docker exec mydocker ps       #exec是相当于不进入容器里面,而执行命令
   PID TTY          TIME CMD
    30 ?        00:00:00 ps
[root@localhost ~]#

[root@localhost ~]# docker logs 9baa263c288a  #查看当前访问的日志(只能查看最后一条)
[root@localhost ~]#
临时开一终端,实时查看当前访问的日志: docker logs  -f mynginx



[root@localhost ~]# docker run -d -P nginx  # -P 随机映射
[root@localhost ~]# docker run -d -p 192.168.132.147:81:80  --name mynginx nginx (nginx为镜像,80为容器的端口,192.168.132.147:81虚拟机的本地IP和端口)
[root@localhost ~]# docker port mynginx            #查看映射

随机映射:docker run -P
指定映射:
-p  hostPort:  containerPort
-p  ip:hostPort: containerPort
-p  ip:: containerPort
-p  hostPort: containerPort:udp



1,不指定虚拟机目录的挂载方式:

[root@localhost ~]# docker run -d --name nginx-volume-tests -v /data nginx
e57a78590a6fe47cdf145a39da56063d79612615af9d830b9ca95457c3b8450
(nginx-volume-tests为容器名,/data 表示将虚拟机的卷挂载到容器的data目录下,nginx为镜像)

接下来怎么查看虚拟机的哪个目录挂载到容器了呢???
[root@localhost ~]# docker inspect -f {{.Mounts}} nginx-volume-tests
[{volume 7f18e59432ee18028bee3f5a731d00510ff268029807c033193ddd5aee7aae3f /var/lib/docker/volumes/7f18e59432ee18028bee3f5a731d00510ff268029807c033193ddd5aee7aae3f/_data /data local  true }]
[root@localhost ~]# docker inspect -f {{   .Mounts   }} nginx-volume-tests
Template parsing error: template: :1: unexpected unclosed action in command
[root@localhost ~]#

2,指定虚拟机目录的挂载方式:
[root@localhost ~]#mkdir -p /data/docker-volume-nginx
[root@localhost ~]# docker run -d --name nginx-volume-test2 -v /data/docker-volume-nginx/:/data  nginx
(将物理主机的/data/docker-volume-nginx挂载到容器的/data目录下)
[root@localhost ~]# mkdir /data/nfs-data -p
[root@localhost ~]# docker run -d --name nfs -v /data/nfs-data:/data centos
494c14f1cb3efc420244a4140f562952369482d904e67a63fe5d79986b0bcd01
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b51e0ea4b1a7        nginx               "nginx -g ‘daemon ..."   27 minutes ago      Up 27 minutes       80/tcp              nginx-volume-test2
e57a78590a6f        nginx               "nginx -g ‘daemon ..."   56 minutes ago      Up 56 minutes       80/tcp              nginx-volume-tests
[root@localhost ~]#
(从上面可以看出容器没有运行起来,啥原因呢??是因为你没有让他执行命令)
[root@localhost ~]# docker run -d --name nfs2 -v /data/nfs-data:/data centos /bin/bash
f8fdd06b9db0dd4b00e3b6c7d6308c6566adbfa54cb99272bee93d0db146941a
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b51e0ea4b1a7        nginx               "nginx -g ‘daemon ..."   31 minutes ago      Up 31 minutes       80/tcp              nginx-volume-test2
e57a78590a6f        nginx               "nginx -g ‘daemon ..."   59 minutes ago      Up 59 minutes       80/tcp              nginx-volume-tests
[root@localhost ~]#

(再次执行加上命令了,为啥容器还是没有起来呢?是因为命令执行完了。你可以把centos换成nginx镜像容器就会一直运行)
3,数据卷容器:
其实就是多个容器间共享数据。就像NFS
[root@localhost ~]#mkdir /app/data
[root@localhost ~]# docker run -d --name nfs-volume1  -v /app/data:/tmp  nginx
62b641f771c209010a56a85bb285546ed1bd667d0763473a2a88e16aaf032713
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
62b641f771c2        nginx               "nginx -g ‘daemon ..."   4 seconds ago       Up 3 seconds        80/tcp              nfs-volume1
b51e0ea4b1a7        nginx               "nginx -g ‘daemon ..."   40 minutes ago      Up 40 minutes       80/tcp              nginx-volume-test2
e57a78590a6f        nginx               "nginx -g ‘daemon ..."   About an hour ago   Up About an hour    80/tcp              nginx-volume-tests
[root@localhost ~]#
[root@localhost ~]# docker run --rm -it --volumes-from nfs-volume1 centos /bin/bash
[root@f9084a10432a /]#
然后再在当前的容器(f9084a10432a)创建文件,可以发现物理机/app/data/下会同样生成。



[root@localhost nginx]# cat Dockerfile 
# This Dockerfile
#  Base  image
FROM centos 
#在什么基础上创建
MAINTAINER liuqi 8523@qq.com
#谁创建的,维护者是谁
RUN rpm -ivh http://download.fedoraproject.org/pub/epel/7/ppc64le/Packages/e/epel-release-7-11.noarch.rpm
RUN yum install -y nginx && yum clean all
RUN echo "daemon off;" >>/etc/nginx/nginx.conf
ADD index.html /usr/share/nginx/html/index.html
#那么在当前目录下就需要先准备好index.html 文件
#你可以写命令,干了什么
EXPOSE 80
CMD ["nginx"]
[root@localhost nginx]#
[root@localhost nginx]# echo "nginx in docker haha">>index.html
(在Dockerfile 的当前目录创建目录首页)
[root@localhost nginx]# docker build -t mynginx:v2 .


[root@localhost nginx]# docker image pull library/hello-world
上面代码中,docker image pull是抓取 image 文件的命令。library/hello-world是 image 文件在仓库里面的位置,其中library是 image 文件所在的组,hello-world是 image 文件的名字。

 

2,docker的基本操作

原文:https://www.cnblogs.com/k8s-pod/p/13513610.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!