[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
1.安装docker环境依赖
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
2.配置阿里云docker的yum源
[root@localhost yum.repos.d]# wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost yum.repos.d]# yum clean all && yum makecache
3.安装docker-ce
[root@localhost yum.repos.d]# yum install -y docker-ce docker-ce-cli containerd.io
[root@localhost ~]# systemctl start docker && systemctl enable docker
4.配置docker镜像加速
[root@localhost ~]# tee /etc/docker/daemon.json<<-'EOF'
{
"registry-mirrors":["https://rncxm540.mirror.aliyuncs.com"]
}
EOF
重启docker服务使daemon.json生效
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker version #查看docker版本
[root@localhost ~]# docker info #查看docker信息
[root@localhost ~]# docker search centos #搜索images
docker pull下载镜像时报错解决方法
[root@localhost ~]# yum install bind-utils
[root@localhost ~]# dig @114.114.114.114 registry-1.docker.io
[root@localhost ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
34.192.182.239 registry-1.docker.io #添加这行解析
[root@localhost ~]# docker pull centos #下载镜像
[root@localhost ~]# docker images #查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 67fa590cfc1c 4 weeks ago 202MB
[root@localhost ~]# echo 1 >/proc/sys/net/ipv4/ip_forward #开启网络转发功能
[root@localhost ~]# systemctl stop firewalld #关闭防火墙
[root@localhost ~]# systemctl disable firewalld #开机不启动
[root@localhost ~]# systemctl restart docker #重启docker
[root@localhost ~]# docker run -it centos /bin/bash #启动docker实例
docker常用参数:
-d 后台运行容器,并返回容器ID;
-c 后面跟待完成的命令
[root@localhost ~]# docker run -d centos /bin/bash -c "while true;do echo hello docker;sleep 6;done"
fae63815ad725bc28290331f2c74a67ad480ac6befcca264de9636264956a6e8
[root@localhost ~]# docker logs fa #查看日志输出
hello docker
hello docker
[root@localhost ~]# docker ps -a #-a列出所有容器
[root@localhost ~]# docker stop fa #关闭容器
docker kill <容器ID> #杀死一个容器
[root@localhost ~]# docker start f4 #运行实例
删除指定container
docker rm <容器ID>
docker镜像制作方法
docker image的两种制作方法:
方法一:docker commit #保存container的当前状态到image后,然后生成对应的image
方法二:docker build #使用docker file文件自动化制作image
方法一:docker commit
创建一个安装好apache web服务器的容器镜像
[root@localhost ~]# docker run -it centos /bin/bash
[root@c3e1f6ffc1e7 /]# yum install -y httpd
[root@c3e1f6ffc1e7 /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3e1f6ffc1e7 centos "/bin/bash" 3 minutes ago Exited (0) 12 seconds ago elastic_jang
[root@localhost ~]# docker images #当前只有一个image
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 67fa590cfc1c 4 weeks ago 202MB
[root@localhost ~]# docker commit c3e1f6ffc1e7 centos:apache
sha256:1727828f5bc8a778c80e99d73e90f22f2cf85a90d9044fd588680ec9686ab04f
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos apache 1727828f5bc8 10 seconds ago 303MB
centos latest 67fa590cfc1c 4 weeks ago 202MB
使用新创建的centos:apache镜像生成一台容器实例
[root@localhost ~]# docker run -it centos:apache /bin/bash
[root@cdff4d007a86 /]# rpm -qa httpd
httpd-2.4.6-90.el7.centos.x86_64
基于apache镜像的容器创建成功
方法二:通过docker bulid创建一个基于centos的httpd web服务器镜像。
使用docker build创建镜像时,需要使用dockerfile文件自动化操作image镜像。
[root@localhost ~]# mkdir /docker-build
[root@localhost ~]# cd /docker-build/
[root@localhost docker-build]# touch Dockerfile
[root@localhost docker-build]# vim Dockerfile
FROM centos:latest #FROM 基于哪个镜像
MAINTAINER <longqm> #MAINTAINER 镜像创建者
RUN yum -y install httpd #RUN 安装软件
ADD start.sh /usr/local/bin/start.sh #将文件拷贝到新产生的镜像的文件系统对应的路径。所有拷贝到新镜像中的文件和文件夹权限为0755,uid和gid为0
ADD index.html /var/www/html/index.html
CMD /usr/local/bin/start.sh
#当docker实例启动成功后,会执行CMD后面的命令。所以CMD后面一般跟需要开机启动的服务或脚本。一个Dockerfile中只能有一条CMD命令,多条则只执行最后一条CMD。
[root@localhost docker-build]# echo "/usr/sbin/httpd -DFOREGROUND">start.sh
[root@localhost docker-build]# chmod a+x start.sh
[root@localhost docker-build]# echo "docker image build test" >index.html
[root@localhost docker-build]# docker build -t centos:httpd ./
dcoker build -t 父镜像名:镜像的tag Dockerfile路径
-t:表示tage,镜像名。
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd 7da9368c9071 4 minutes ago 303MB
centos apache 1727828f5bc8 37 minutes ago 303MB
centos latest 67fa590cfc1c 4 weeks ago 202MB
docker image的发布
方法一:save image to tarball
[
root@localhost ~]# docker save -o docker.io-centos-httpd-image.tar centos:httpd
[root@localhost ~]# docker rmi centos:httpd
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos apache 1727828f5bc8 5 days ago 303MB
centos latest 67fa590cfc1c 5 weeks ago 202MB
#导入本地镜像
[root@localhost ~]# docker load -i docker.io-centos-httpd-image.tar
16ddcc287a85: Loading layer [==================================================>] 102MB/102MB
25d369eef2b3: Loading layer [==================================================>] 3.584kB/3.584kB
10a133e4b522: Loading layer [==================================================>] 3.584kB/3.584kB
Loaded image: centos:httpd
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd 7da9368c9071 5 days ago 303MB
centos apache 1727828f5bc8 5 days ago 303MB
centos latest 67fa590cfc1c 5 weeks ago 202MB
方法二:push image to docker hub 发布到外网
1.signup on docker hub &create repo #注册一个账号
https://hub.docker.com
2.login to docker hub
#docker login -u userabc -p abc123 -e userab@gmail.com
3.push image to docker hub #上传镜像
#docker push centos:httpd
4.pull push userabc/centos:httpd
#docker pull userabc/centos:http
container容器端口映射
[root@localhost ~]# docker run -d -p 80:80 docker.io/centos:httpd
[root@localhost ~]# docker ps
[root@localhost ~]# netstat -antup | grep 80
[root@localhost ~]# curl 192.168.1.25
docker image build test
[root@localhost ~]# docker exec -it b1 /bin/bash
-it后跟CONTAINER ID或NAMES
[root@b15759cc983e /]# echo hello world >/var/www/html/index.html
[root@localhost ~]# curl 192.168.1.25
hello world
查看容器的网络,配置容器root密码
[root@b15759cc983e /]# yum install -y net-tools
[root@b15759cc983e /]# ifconfig
[root@b15759cc983e /]# echo 123456 |passwd --stdin root