docker centerOS 安装

##卸载

###查找主机上关于Docker的软件包

rpm -qa | grep docker – – 列出包含docker字段的软件的信息

  1. [pi@x201i nginx]$ rpm -qa | grep docker
  2. docker-common-1.13.1-96.gitb2f74b2.el7.centos.x86_64
  3. docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64
  4. docker-client-1.13.1-96.gitb2f74b2.el7.centos.x86_64

###使用yum remove卸载软件

  1. sudo yum remove docker-common-1.13.1-96.gitb2f74b2.el7.centos.x86_64
  2. sudo yum remove docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64
  3. sudo yum remove docker-client-1.13.1-96.gitb2f74b2.el7.centos.x86_64

###在使用docker命令会提示docker不存在

[pi@x201i nginx]$ docker
-bash: /usr/bin/docker: 没有那个文件或目录

#安装  

##设置yum源

  1. sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

##可以查看所有仓库中所有docker版本,并选择特定版本安装

yum list docker-ce —showduplicates | sort -r

  1. [pi@x201i nginx]$ yum list docker-ce --showduplicates | sort -r
  2. 已加载插件:fastestmirror
  3. 可安装的软件包
  4. * updates: mirrors.163.com
  5. Loading mirror speeds from cached hostfile
  6. * extras: mirrors.163.com
  7. docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
  8. docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
  9. docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
  10. docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
  11. docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
  12. docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
  13. docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
  14. docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable

##安装docker

由于repo中默认只开启stable仓库,故这里安装的是最新稳定版17.12.0

  1. sudo yum install docker-ce
  2. sudo yum install <FQPN> # 例如:sudo yum install docker-ce-17.12.0.ce
  3. rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
  4. yum -y install docker-io

###查看版本

  1. docker version

###启动并加入开机启动

  1. sudo systemctl start docker
  2. sudo systemctl enable docker

#常用命令

##1、进入容器内部

####查看容器ID

docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0607d754d3f0 docker.io/jenkins/jenkins:2.194 “/sbin/tini — /us…” 22 minutes ago Up 5 minutes 0.0.0.0:50000->50000/tcp, 0.0.0.0:3080->8080/tcp x201i_jenkins

####进入容器

docker exec 后边可以跟多个参数,这里主要说明 -i -t 参数。

只用 -i 参数时,由于没有分配伪终端,界面没有我们熟悉的 Linux 命令提示符,但命令执行结果仍然可以返回。
当 -i -t 参数一起使用时,则可以看到我们熟悉的 Linux 命令提示符。

  1. [pi@x201i ~]$ docker exec -it 0607 bash
  2. jenkins@0607d754d3f0:/$

如果从这个 stdin 中 exit,不会导致容器的停止。这就是为什么推荐大家使用 docker exec 的原因。

##2、删除镜像

删除images,通过image的id来指定删除谁

  1. docker rmi <image id>

##3、删除容器

docker rm

##4、删除所有已经停止的容器

docker rm $(docker ps -a -q)

##5、列出容器

语法
docker ps [OPTIONS]
OPTIONS说明:

  1. -a :显示所有的容器,包括未运行的。
  2. -f :根据条件过滤显示的内容。
  3. --format :指定返回值的模板文件。
  4. -l :显示最近创建的容器。
  5. -n :列出最近创建的n个容器。
  6. --no-trunc :不截断输出。
  7. -q :静默模式,只显示容器编号。
  8. -s :显示总的文件大小。
  9. -a :显示所有的容器,包括未运行的

##docker镜像加速

为了永久性保留更改,您可以修改 /etc/docker/daemon.json 文件并添加上 registry-mirrors 键值。

  1. {
  2. "registry-mirrors": ["http://hub-mirror.c.163.com"]
  3. }

##修改docker镜像和容器数据存储位置

####方法1(不知道为什么不生效):

修改配置CentOS 中的位置是:/etc/sysconfig/docker
1、其中—graph /backup/software/docker-data为新增部分

# Modify these options if you want to change the way the docker daemon runs
OPTIONS='--graph /backup/software/docker-data  --selinux-enabled --log-driver=journald --signature-verification=false'
if [ -z "${DOCKER_CERT_PATH}" ]; then
    DOCKER_CERT_PATH=/etc/docker
fi

# Do not add registries in this file anymore. Use /etc/containers/registries.conf
# instead. For more information reference the registries.conf(5) man page.

# Location used for temporary files, such as those created by
# docker load and build operations. Default is /var/lib/docker/tmp
# Can be overriden by setting the following environment variable.
# DOCKER_TMPDIR=/var/tmp

# Controls the /etc/cron.daily/docker-logrotate cron job status.
# To disable, uncomment the line below.
# LOGROTATE=false

# docker-latest daemon can be used by starting the docker-latest unitfile.
# To use docker-latest client, uncomment below lines
#DOCKERBINARY=/usr/bin/docker-latest
#DOCKERDBINARY=/usr/bin/dockerd-latest
#DOCKER_CONTAINERD_BINARY=/usr/bin/docker-containerd-latest
#DOCKER_CONTAINERD_SHIM_BINARY=/usr/bin/docker-containerd-shim-latest

###方法2(亲测有效):

修改/etc/docker/daemon.json
如果不存在就新建。添加 “graph”:”YOUR_PATH”, 便可

{
  "storage-driver": "overlay2",
  "graph":"/data/docker",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}

2、reload配置文件

systemctl daemon-reload
systemctl restart docker.service

3、检查是否生效

Docker Root Dir: /backup/docker-data

docker info

[root@x201i sysconfig]# docker info
......
Docker Root Dir: /backup/docker-data
Debug Mode (client): false