设为首页 收藏本站
查看: 1167|回复: 0

[经验分享] 1、Docker基础入门实战

[复制链接]

尚未签到

发表于 2019-2-21 10:47:19 | 显示全部楼层 |阅读模式
  1、系统版本
  [root@localhost ~]# cat /etc/redhat-release
  CentOS Linux release 7.2.1511 (Core)
  2、关闭selinux
  [root@localhost ~]# sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
  [root@localhost ~]# getenforce
  Enforcing
  [root@localhost ~]# setenforce 0
  [root@localhost ~]# getenforce
  Permissive
  [root@localhost ~]#reboot
  3、修改yum源
  [root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  [root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  [root@localhost ~]# yum makecache
  4、安装网络工具
  [root@localhost ~]# yum -y install net-tools
  5、Docker安装相关操作
  [root@localhost ~]# yum -y install docker
  [root@localhost ~]# docker -v
  Docker version 1.13.1, build 94f4240/1.13.1
  [root@localhost ~]# systemctl status docker
  [root@localhost ~]# systemctl start docker
  [root@localhost ~]# systemctl restart docker
  [root@localhost ~]# systemctl stop docker
  [root@localhost ~]# systemctl enable docker打开开机启动
  [root@localhost ~]# systemctl disable docker关闭开机启动
  6、Docker镜像相关操作
  [root@localhost ~]# docker imags                                                                                        查看镜像
  [root@localhost ~]# docker search centos7                                                                          搜索镜像
  [root@localhost ~]# docker pull docker.io/openshift/base-centos7                                    下载镜像
  [root@localhost ~]# docker save docker.io/openshift/base-centos7 > /tmp/centos7.tar    保存镜像
  [root@localhost ~]# docker rmi docker.io/openshift/base-centos7                                     删除镜像
  [root@localhost ~]# docker load < /tmp/centos7.tar                                                           导入镜像
  [root@localhost ~]# docker tag docker.io/openshift/base-centos7  centos7                       重命名镜像
  7、交互式启动容器相关操作
  [root@localhost ~]# docker run --name docker_01 -it centos7 /bin/bash                            创建容器
  [root@localhost ~]# docker ps -a                                                                                          查看容器
  [root@localhost ~]# docker stop docker_01                                                                         停止容器
  [root@localhost ~]# docker start docker_01                                                                         启动容器
  [root@localhost ~]# docker kill docker_01                                                                            杀掉容器
  [root@localhost ~]# docker rm docker_01                                                                            删除容器
  8、后台启动容器相关操作
  [root@localhost ~]# docker run --name docker_01 -d centos7 /bin/bash -c 'while true; do echo docker_01; sleep 10; done'        创建容器
  [root@localhost ~]# docker exec -it docker_01 /bin/bash                                                                                                                进入后台启动的容器
  [root@localhost ~]# docker logs docker_01                                                                                                                                     查看容器日志
  [root@localhost ~]# for id in `docker ps -a | grep -v CONTAINER | awk '{print $1}'`;do docker start $id; done                               批量启动容器
  [root@localhost ~]# for id in `docker ps -a | grep -v CONTAINER | awk '{print $1}'`;do docker stop $id; done                               批量停止容器
  [root@localhost ~]# for id in `docker ps -a | grep -v CONTAINER | awk '{print $1}'`;do docker rm $id; done                                  批量删除容器
  [root@localhost ~]# for id in `docker ps -a | grep -v CONTAINER | awk '{print $1}'`;do docker kill $id; done                                  批量杀掉容器
  9、自定义镜像commit相关操作
  [root@localhost ~]# docker commit docker_01 centos7_base                                                                                                          用容器docker_01生成镜像centos7_bash
  10、自定义镜像dockerfile相关操作
  [root@localhost ~]# mkdir -pv /docker/nginx
  [root@localhost ~]# cd /docker/nginx
  [root@localhost nginx]# cat install.sh
  yum install -y wget tar gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
  cd /usr/local/src
  wget 'http://nginx.org/download/nginx-1.12.2.tar.gz'
  tar -zxvf nginx-1.12.2.tar.gz
  cd nginx-1.12.2
  ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream  --with-stream_ssl_module
  make
  make install
  rm -rf /usr/local/src
  #使用daemon off配置让nginx在前台运行
  sed -i -e '/worker_processes/a daemon off;' /usr/local/nginx/conf/nginx.conf
  exit 0
  [root@localhost nginx]# cat Dockerfile
  FROM centos7_base
  COPY install.sh /tmp/install.sh
  RUN sh /tmp/install.sh
  [root@localhost nginx]# docker build -t centos7_nginx .
  [root@localhost nginx]# docker run --name docker_nginx -d centos7_nginx /bin/bash -c '/usr/local/nginx/sbin/nginx'
  [root@localhost nginx]# docker exec -it docker_nginx /bin/bash
  bash-4.2# netstat -tuanlp
  Active Internet connections (servers and established)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
  tcp        0      0 0.0.0.0:80
  11、Docker容器网络模式相关操作(默认为桥接模式)
  创建桥接模式容器docker_briage(此模式有独立网络)
  [root@localhost ~]# docker run --name docker_bridge --net=bridge -d centos7_nginx /bin/bash -c '/usr/local/nginx/sbin/nginx'
  [root@localhost ~]# docker exec -it docker_bridge /bin/bash
  bash-4.2# ifconfig
  eth0: flags=4163  mtu 1500
  inet 172.17.0.2  netmask 255.255.0.0  broadcast 0.0.0.0
  inet6 fe80::42:acff:fe11:2  prefixlen 64  scopeid 0x20
  ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
  RX packets 8  bytes 648 (648.0 B)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 8  bytes 648 (648.0 B)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  lo: flags=73  mtu 65536
  inet 127.0.0.1  netmask 255.0.0.0
  inet6 ::1  prefixlen 128  scopeid 0x10
  loop  txqueuelen 0  (Local Loopback)
  RX packets 0  bytes 0 (0.0 B)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 0  bytes 0 (0.0 B)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  bash-4.2#
  创建主机模式容器docker_host(此模式与宿主机共享网络)
  [root@localhost ~]# docker run --name docker_host --net=host -d centos7_nginx /bin/bash -c '/usr/local/nginx/sbin/nginx'
  [root@localhost ~]# docker exec -it docker_host /bin/bash
  bash-4.2# ifconfig
  docker0: flags=4099  mtu 1500
  inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
  inet6 fe80::42:d8ff:fe1d:d64e  prefixlen 64  scopeid 0x20
  ether 02:42:d8:1d:d6:4e  txqueuelen 0  (Ethernet)
  RX packets 76675  bytes 3915531 (3.7 MiB)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 92619  bytes 163138586 (155.5 MiB)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  ens192: flags=4163  mtu 1500
  inet 192.168.130.66  netmask 255.255.255.0  broadcast 192.168.130.255
  inet6 fe80::250:56ff:fe90:d195  prefixlen 64  scopeid 0x20
  ether 00:50:56:90:d1:95  txqueuelen 1000  (Ethernet)
  RX packets 510747  bytes 394855762 (376.5 MiB)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 306995  bytes 432987349 (412.9 MiB)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  lo: flags=73  mtu 65536
  inet 127.0.0.1  netmask 255.0.0.0
  inet6 ::1  prefixlen 128  scopeid 0x10
  loop  txqueuelen 0  (Local Loopback)
  RX packets 4  bytes 200 (200.0 B)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 4  bytes 200 (200.0 B)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  bash-4.2#
  12、Docker容器模式下端口映射
  [root@localhost ~]# docker run -p 8801:80 -p 8802:80 --name docker_map -d centos7_nginx /bin/bash -c '/usr/local/nginx/sbin/nginx'
  [root@localhost ~]# docker exec -it docker_map /bin/bash
  bash-4.2# netstat -tuanlp
  Active Internet connections (servers and established)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
  tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1/nginx: master pro
  bash-4.2# exit
  exit
  [root@localhost ~]# netstat -tuanlp
  Active Internet connections (servers and established)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1344/sshd
  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2253/master
  tcp        0     52 192.168.130.66:22       192.168.53.137:65516    ESTABLISHED 17791/sshd: root@pt
  tcp        0      0 192.168.130.66:22       192.168.53.137:49616    ESTABLISHED 13743/sshd: root@pt
  tcp6       0      0 :::22                   :::*                    LISTEN      1344/sshd
  tcp6       0      0 ::1:25                  :::*                    LISTEN      2253/master
  tcp6       0      0 :::8801                 :::*                    LISTEN      18215/docker-proxy-
  tcp6       0      0 :::8802                 :::*                    LISTEN      18204/docker-proxy-
  [root@localhost ~]#
  13、Docker容器与宿主机文件共享相关操作
  [root@localhost ~]# docker run --privileged=true -v /htdocs:/usr/local/nginx/html/htdocs --name docker_share -d centos7_nginx /bin/bash -c '/usr/local/nginx/sbin/nginx'




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-675247-1-1.html 上篇帖子: Docker三剑客之Docker Compose 下篇帖子: docker镜像基础命令
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表