引言

让我们一起来学习一下Docker的一些简单命令,让我们更快速的了解容器的使用方法

一、安装 Docker

Docker 软件包已经包括在默认的 CentOS-Extras 软件源里。因此想要安装 docker,只需要运行下面的 yum 命令:

  1. [root@localhost ~]# yum install docker

二、启动 Docker 服务

安装完成后,使用下面的命令来启动 docker 服务,并将其设置为开机启动:

  1. [root@localhost ~]# service docker start
  2. [root@localhost ~]# chkconfig docker on

译注:此处采用了旧式的 sysv 语法,如采用CentOS 7中支持的新式 systemd 语法,如下:

  1. [root@localhost ~]# systemctl start docker.service
  2. [root@localhost ~]# systemctl enable docker.service

下载官方的 CentOS 镜像到本地 ( 译注:由于 Docker 被墙 :-< ,所以请使用 http://docker.cn 的镜像。 )

  1. [root@localhost ~]# docker pull centos
  2. Pulling repository centos
  3. 192178b11d36: Download complete
  4. 70441cac1ed5: Download complete
  5. ae0c2d0bdc10: Download complete
  6. 511136ea3c5a: Download complete
  7. 5b12ef8fd570: Download complete

确认 CentOS 镜像已经被获取:

  1. [root@localhost ~]# docker images centos
  2. REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
  3. centos centos5 192178b11d36 2 weeks ago 466.9 MB
  4. centos centos6 70441cac1ed5 2 weeks ago 215.8 MB
  5. centos centos7 ae0c2d0bdc10 2 weeks ago 224 MB
  6. centos latest ae0c2d0bdc10 2 weeks ago 224 MB

三、运行一个 Docker 容器

  1. [root@localhost ~]# docker run -i -t centos /bin/bash
  2. [root@dbf66395436d /]#

我们可以看到,CentOS 容器已经被启动,并且我们得到了 bash 提示符。在 docker 命令中我们使用了

-i 捕获标准输入输出

-t 分配一个终端或控制台 选项。

-d:让容器在后台运行。

-P:将容器内部使用的网络端口映射到我们使用的主机上

docker容器内执行命令提示 cannot open directory

问题原因及解决办法

原因是CentOS7中的安全模块selinux把权限禁掉了,至少有以下三种方式解决挂载的目录没有权限的问题:

1.在运行容器的时候,给容器加特权,及加上 —privileged=true 参数:

docker run -i -t -v /soft:/soft —privileged=true 686672a1d0cc /bin/bash

若要断开与容器的连接

输入 exit。

  1. [root@cd05639b3f5c /]# cat /etc/redhat-release
  2. CentOS Linux release 7.0.1406 (Core)
  3. [root@cd05639b3f5c /]# exit
  4. exit
  5. [root@localhost ~]#

我们还可以搜索基于 Fedora 和 Ubuntu 操作系统的容器。

  1. [root@localhost ~]# docker search ubuntu
  2. [root@localhost ~]# docker search fedora

使用 docker ps 来查看我们正在运行的容器

  1. runoob@runoob:~$ docker ps

停止WEB应用容器

  1. runoob@runoob:~$ docker stop determined_swanson
  2. determined_swanson

重启WEB应用容器

已经停止的容器,我们可以使用命令 docker start 来启动。

  1. runoob@runoob:~$ docker start determined_swanson
  2. determined_swanson
  3. docker ps -l 查询最后一次创建的容器:

移除WEB应用容器

我们可以使用 docker rm 命令来删除不需要的容器

  1. runoob@runoob:~$ docker rm determined_swanson
  2. determined_swanson

删除容器时,容器必须是停止状态,否则会报如下错误

  1. runoob@runoob:~$ docker rm determined_swanson
  2. Error response from daemon: You cannot remove a running container 7a38a1ad55c6914b360b565819604733db751d86afd2575236a70a2519527361. Stop the container before attempting removal or use -f

四、网络端口的快捷方式

通过docker ps 命令可以查看到容器的端口映射,docker还提供了另一个快捷方式:docker port,使用 docker port 可以查看指定 (ID或者名字)容器的某个确定端口映射到宿主机的端口号。

上面我们创建的web应用容器ID为:7a38a1ad55c6 名字为:determined_swanson

我可以使用docker port 7a38a1ad55c6 或docker port determined_swanson来查看容器端口的映射情况

  1. runoob@runoob:~$ docker port 7a38a1ad55c6
  2. 5000/tcp -> 0.0.0.0:5000

五、docker进入容器

  1. docker exec -it fbc3e3e77ef9 /bin/bash

六、更新(创建)一个镜像

  1. docker commit -m="yyt update" -a="yyt" 9b6f50751f15 yyt/tomcat8.