本文,只是用于个人学习用途,请到其他的博客查看,docker教程
推荐视频学习地址:https://edu.51cto.com/
学习
安装Docker CE社区版
yum remove docker-common -y cd /etc/yum.repos.d/ wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum -y install docker-ce docker version
[root@localhost ~]# systemctl list-unit-files |grep docker docker.service disabled docker.socket disabled [root@localhost ~]# systemctl enable docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service. [root@localhost ~]# systemctl start docker
Docker 镜像的分类
Docker镜像的相关命令
docker images docker search centos docker pull centos:7 #pull就是下载的意思
标识Docker镜像 , 如centos:7 ,镜像名是centos, 版本号是7
[root@localhost ~]# docker pull centos:7 7: Pulling from library/centos 524b0c1e57f8: Pull complete Digest: sha256:e9ce0b76f29f942502facd849f3e468232492b259b9d9f076f71b392293f1582 Status: Downloaded newer image for centos:7 docker.io/library/centos:7 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 b5b4d78bc90c 2 days ago 203MB [root@localhost ~]#
镜像导出备份,删除和导入
重命名:docker tag hub-mirror.c.163.com/library/centos:7 centos:7 导出镜像:docker save hub-mirror.c.163.com/library/centos:7 >/tmp/centos.tar 删除镜像:docker rmi centos:7 镜像导入:docker load < /tmp/centos7.tar
Docker容器概念
使用Centos7镜像启动容器
docker run -it centos:7 /bin/bash # i表示交互式,t表示打开一个Shell窗口
容器说明
宿主机上查看容器的相关操作
容器后台运行的特点
怎么样启动一个永久运行的容器
后台容器的停止
容器的临时性
Docker自定义镜像
两种方式构建自定义镜像
查看基础镜像信息
给基础镜像新增ifconfig命令和一个while程序
#!/bin/bash while true do echo "well done" sleep 5 done
Commit生成一个新镜像 --非主流做法
tag如果没指定,默认就是latest
dockerfile生成一个新镜像---主流的方式
Dockerfile能把制作镜像的过程写入到配置文件中
Dockerfile使用说明
最简单需求
Dockerfile配置文件实战:/docker/yum163/Dockerfile
FROM centos:7 MAINTAINER shijiange 361589194@qq.com COPY CentOS-Base.repo /etc/yum.repos.d/ RUN yum -y install net-tools
/docker/yum163/CentOS-Base.repo
[base] name=CentOS-$releasever - Base baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 [updates] name=CentOS-$releasever - Updates baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 [extras] name=CentOS-$releasever - Extras baseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
dockerfile 镜像制作命令
实战01制作nginx镜像
Nginx安装脚本/docker/nginx/install.sh
yum install -y wget tar gcc gcc-c++ make pcre pcre-devel zlib zlib-devel cd /usr/local/src wget ‘http://nginx.org/download/nginx-1.14.2.tar.gz‘ tar -zxf nginx-1.14.2.tar.gz cd nginx-1.14.2 ./configure --prefix=/usr/local/nginx && make && make install \rm -rf /usr/local/src/
Nginx镜像启动注意
Dockerfile文件/docker/nginx/Dockerfile
FROM centos:7 COPY install.sh /tmp/install.sh RUN sh /tmp/install.sh COPY gotorun /usr/bin/gotorun ENTRYPOINT ["gotorun"]
编写启动脚本gotorun,加执行权限
#!/bin/bash /usr/local/nginx/sbin/nginx -g "daemon off;
制作Nginx镜像
后台启动容器测试
实战02制作redis镜像
默认
Redis安装脚本/docker/redis/install.sh
yum install -y wget gcc gcc-c++ make tar openssl openssl-devel cmake cd /usr/local/src wget ‘http://download.redis.io/releases/redis-4.0.9.tar.gz‘ tar -zxf redis-4.0.9.tar.gz cd redis-4.0.9 make && make PREFIX=/usr/local/redis install mkdir -pv /usr/local/redis/conf/ cp redis.conf /usr/local/redis/conf/ \rm -rf /usr/local/src/*
Redis镜像制作配置文件/docker/redis/Dockerfile
FROM centos:7 COPY install.sh /tmp/install.sh RUN sh /tmp/install.sh COPY gotorun /usr/bin/gotorun CMD ["gotorun"]
编写启动脚本/docker/redis/gotorun,加执行权限
#!/bin/bash
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
构建Redis镜像
实战03制作nginx + redis 多进程镜像
多进程镜像需求
Nginx安装脚本/docker/multiproc/install_nginx.sh
yum install -y wget tar gcc gcc-c++ make pcre pcre-devel zlib zlib-devel cd /usr/local/src wget ‘http://nginx.org/download/nginx-1.14.2.tar.gz‘ tar -zxf nginx-1.14.2.tar.gz cd nginx-1.14.2 ./configure --prefix=/usr/local/nginx && make && make install \rm -rf /usr/local/src/*
Redis安装脚本/docker/multiproc/install_redis.sh
yum install -y wget gcc gcc-c++ make tar openssl openssl-devel cmake cd /usr/local/src wget ‘http://download.redis.io/releases/redis-4.0.9.tar.gz‘ tar -zxf redis-4.0.9.tar.gz cd redis-4.0.9 make && make PREFIX=/usr/local/redis install mkdir -pv /usr/local/redis/conf/ cp redis.conf /usr/local/redis/conf/ \rm -rf /usr/local/src/*
Dockerfile配置:/docker/multiproc/Dockerfile
FROM centos:7 COPY install_nginx.sh install_redis.sh /tmp/ RUN sh /tmp/install_nginx.sh RUN sh /tmp/install_redis.sh COPY gotorun /usr/bin/ RUN sed -i -e ‘s/bind 127.0.0.1/bind 0.0.0.0/g‘ /usr/local/redis/conf/redis.conf ENTRYPOINT ["gotorun"]
编写启动脚本/docker/multiproc/gotorun,加执行权限
#!/bin/bash /usr/local/nginx/sbin/nginx /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
Nginx+Redis镜像构建
docker网络模式和特权指令
查看网络方式
docker network ls NETWORK ID NAME DRIVER SCOPE c017726e1c76 bridge bridge local 2f0bce224260 host host local b488b8ca5419 none null local
Docker容器的网络模式
Docker默认的网络方式观察
桥接docker0加端口映射可以实现跟外界通讯
使用host模式,直接使用宿主机的网络,可直接外界通讯
使用none模式,关闭容器网络,用得比较少
容器文件共享和特权模式
业务上有时候有如下需求
容器和宿主机怎么做文件共享 ,比较常用
Docker使用特权的方式去启动容器,无特权很多命令无法运行 ,用的比较少
原文:https://www.cnblogs.com/faberbeta/p/12853704.html