docker基本操作
环境:centos7.4 docker1.3 官方网站:https://www.docker.com/
1、安装docker
[root@localhost ~]# yum -y install docker
2、查看当前版本
[root@localhost ~]# docker --version Docker version 1.13.1, build 94f4240/1.13.1
3、启动服务
[root@localhost ~]# systemctl start docker && systemctl enable docker
4、下载镜像
[root@localhost ~]# docker search centos INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/centos The official build of CentOS. 4419 [OK] docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 114 [OK]
- 第一种:从公网docker hub 拉取(下载)image
[root@localhost ~]# docker pull docker.io/centos
如果以上不能直接下载或速度缓慢,请添加国内镜像加速地址(阿里或网易)
阿里:https://cr.console.aliyun.com (需登陆后获得专属加速地址)
网易:https://www.163yun.com (需登陆后获得专属加速地址)
获得专属地址后如下配置
[root@localhost ~]# vim /etc/docker/daemon.json
{
"registry-mirrors":["https://lgc971x1.mirror.aliyuncs.com"] #获得的专属地址
}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker pull docker.io/centos
- 第二种:从本地导入镜像
上传 docker.io-centos-lastest-image.tar到centos下
[root@localhost ~]# docker load -i /root/docker.io-centos-lastest-image.tar #加载镜像到库
- 第三种:直接从私有库下载
[root@localhost ~]# docker pull hub.c.163.com/library/tomcat:latest
5、查看当前镜像
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos apache 42aa44f22168 57 minutes ago 308 MB docker.io/centos latest 49f7960eb7e4 4 weeks ago 200 MB
6、开启路由转发
[root@localhost ~]# echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf
[root@localhost ~]# sysctl -p net.ipv4.ip_forward = 1
7、启动实例
语法: docker run [选项] 镜像名 [shell]
[root@localhost ~]# docker run -itd docker.io/centos /bin/bash #run启动关键字 ,-i 交互式,-t启用终端,-d后台运行
8、查看实例
- 正在运行实例
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d85b7c78ccb8 docker.io/centos "/bin/bash" 23 seconds ago Up 23 seconds b833b8c3fd80 docker.io/centos "/bin/bash" 30 seconds ago Up 29 seconds
- 查看所有(包括已退出实例)
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d85b7c78ccb8 docker.io/centos "/bin/bash" 23 seconds ago Up 23 seconds b833b8c3fd80 docker.io/centos "/bin/bash" 30 seconds ago Up 29 seconds d3a8ec13d928 docker.io/centos "bash" 31 minutes ago Exited (0) 26 minutes ago centos7.4
9、查看实例日志
语法: docker logs 容器Name/ID
[root@localhost ~]# docker logs e01e01c0c8a6 hello world hello world
10、启动/关闭/重启实例
- 语法: docker start 容器Name/ID
[root@localhost ~]# docker start de9207f46b88 de9207f46b88
- 语法: docker stop 容器Name/ID
[root@localhost ~]# docker stop centos7.2 centos7.2
- 语法: docker restart 容器Name/ID
[root@localhost ~]# docker restart centos7 centos7
11、杀死实例
- 语法:docker kill 容器Name/ID
[root@localhost ~]# docker kill de9207f46b88 de9207f46b88
12、删除实例
- 第一种,已退出的实例,语法:docker rm 容器Name/ID
[root@localhost ~]# docker rm 4b9f2fbece7a 4b9f2fbece7a
- 第二种,强制删除(包括正在运行)实例,语法:docker rm -f 容器Name/ID
[root@localhost ~]# docker rm -f b833b8c3fd80 b833b8c3fd
13、调用后台运行实例
语法: docker run [选项] 镜像名 [shell]
[root@localhost ~]# docker exec -itd docker.io/centos /bin/bash
14、删除镜像
语法: docker rmi 镜像名
[root@localhost ~]# docker rmi docker.io/centos
常见错误:
错误1:
[root@localhost ~]# docker rm ae07f38f92b3 Error response from daemon: You cannot remove a running container ae07f38f92b3cb0ed0d05ed9c4627d288e5a689f151da93513bb4157994c0120. Stop the container before attempting removal or use -f
原因:实例正在运行,直接删除会报以上错误
解决方法:1、关闭实例后,再删除
2、直接加-f 选项强制删除
错误2:
[root@localhost ~]# docker run -it docker.io/centos:apache bash /usr/bin/docker-current:Error response from daemon: error creating overlay mount to /var/lib/docker/overlay2/1b1d4a0234ee4ca31eaf3c0b9f25a7c854116daba0da1fa08730639ae7080431/merged: invalid argument. See '/usr/bin/docker-current run --help'.
原因:docker新版本用的overlay2文件系统,而系统默认只能识别overlay文件系统
解决方法:更新文件系统
[root@localhost ~]# systemctl stop docker
[root@localhost ~]# rm -rf /var/lib/docker #注意会清掉docker images的镜像
[root@localhost ~]# vim /etc/sysconfig/docker-storage #添加以下内容
DOCKER_STORAGE_OPTIONS="--storage-driver overlay "
[root@localhost ~]# vim /etc/sysconfig/docker #去掉option后面的--selinux-enabled
温馨提示:如无特殊说明,本站文章均为作者原创,转载请注明出处!