1Docker基础入门实战

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1Docker基础入门实战相关的知识,希望对你有一定的参考价值。

1、系统版本

[[email protected] ~]# cat /etc/redhat-release 

CentOS Linux release 7.2.1511 (Core) 


2、关闭selinux

[[email protected] ~]# sed -i 's/=enforcing/=disabled/g' /etc/selinux/config

[[email protected] ~]# getenforce

Enforcing

[[email protected] ~]# setenforce 0

[[email protected] ~]# getenforce  

Permissive

[[email protected] ~]#reboot


3、修改yum源

[[email protected] ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

[[email protected] ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[[email protected] ~]# yum makecache


4、安装网络工具

[[email protected] ~]# yum -y install net-tools


5、Docker安装相关操作

[[email protected] ~]# yum -y install docker

[[email protected] ~]# docker -v

Docker version 1.13.1, build 94f4240/1.13.1

[[email protected] ~]# systemctl status docker

[[email protected] ~]# systemctl start docker

[[email protected] ~]# systemctl restart docker

[[email protected] ~]# systemctl stop docker

[[email protected] ~]# systemctl enable docker打开开机启动

[[email protected] ~]# systemctl disable docker关闭开机启动


6、Docker镜像相关操作

[[email protected] ~]# docker imags                                                                                        查看镜像

[[email protected] ~]# docker search centos7                                                                          搜索镜像

[[email protected] ~]# docker pull docker.io/openshift/base-centos7                                    下载镜像

[[email protected] ~]# docker save docker.io/openshift/base-centos7 > /tmp/centos7.tar    保存镜像

[[email protected] ~]# docker rmi docker.io/openshift/base-centos7                                     删除镜像

[[email protected] ~]# docker load < /tmp/centos7.tar                                                           导入镜像

[[email protected] ~]# docker tag docker.io/openshift/base-centos7  centos7                       重命名镜像


7、交互式启动容器相关操作

[[email protected] ~]# docker run --name docker_01 -it centos7 /bin/bash                            创建容器

[[email protected] ~]# docker ps -a                                                                                          查看容器

[[email protected] ~]# docker stop docker_01                                                                         停止容器

[[email protected] ~]# docker start docker_01                                                                         启动容器

[[email protected] ~]# docker kill docker_01                                                                            杀掉容器

[[email protected] ~]# docker rm docker_01                                                                            删除容器


8、后台启动容器相关操作

[[email protected] ~]# docker run --name docker_01 -d centos7 /bin/bash -c 'while true; do echo docker_01; sleep 10; done'        创建容器

[[email protected] ~]# docker exec -it docker_01 /bin/bash                                                                                                                进入后台启动的容器

[[email protected] ~]# docker logs docker_01                                                                                                                                     查看容器日志

[[email protected] ~]# for id in `docker ps -a | grep -v CONTAINER | awk '{print $1}'`;do docker start $id; done                               批量启动容器

[[email protected] ~]# for id in `docker ps -a | grep -v CONTAINER | awk '{print $1}'`;do docker stop $id; done                               批量停止容器

[[email protected] ~]# for id in `docker ps -a | grep -v CONTAINER | awk '{print $1}'`;do docker rm $id; done                                  批量删除容器

[[email protected] ~]# for id in `docker ps -a | grep -v CONTAINER | awk '{print $1}'`;do docker kill $id; done                                  批量杀掉容器


9、自定义镜像commit相关操作

[[email protected] ~]# docker commit docker_01 centos7_base                                                                                                          用容器docker_01生成镜像centos7_bash


10、自定义镜像dockerfile相关操作

[[email protected] ~]# mkdir -pv /docker/nginx

[[email protected] ~]# cd /docker/nginx

[[email protected] 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

[[email protected] nginx]# cat Dockerfile 

FROM centos7_base

COPY install.sh /tmp/install.sh

RUN sh /tmp/install.sh

[[email protected] nginx]# docker build -t centos7_nginx . 

[[email protected] nginx]# docker run --name docker_nginx -d centos7_nginx /bin/bash -c '/usr/local/nginx/sbin/nginx'

[[email protected] 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(此模式有独立网络)

[[email protected] ~]# docker run --name docker_bridge --net=bridge -d centos7_nginx /bin/bash -c '/usr/local/nginx/sbin/nginx'

[[email protected] ~]# docker exec -it docker_bridge /bin/bash

bash-4.2# ifconfig 

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  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<link>

        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<UP,LOOPBACK,RUNNING>  mtu 65536

        inet 127.0.0.1  netmask 255.0.0.0

        inet6 ::1  prefixlen 128  scopeid 0x10<host>

        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(此模式与宿主机共享网络)

[[email protected] ~]# docker run --name docker_host --net=host -d centos7_nginx /bin/bash -c '/usr/local/nginx/sbin/nginx'            

[[email protected] ~]# docker exec -it docker_host /bin/bash

bash-4.2# ifconfig 

docker0: flags=4099<UP,BROADCAST,MULTICAST>  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<link>

        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<UP,BROADCAST,RUNNING,MULTICAST>  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<link>

        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<UP,LOOPBACK,RUNNING>  mtu 65536

        inet 127.0.0.1  netmask 255.0.0.0

        inet6 ::1  prefixlen 128  scopeid 0x10<host>

        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容器模式下端口映射

[[email protected] ~]# docker run -p 8801:80 -p 8802:80 --name docker_map -d centos7_nginx /bin/bash -c '/usr/local/nginx/sbin/nginx'

[[email protected] ~]# 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

[[email protected] ~]# 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: [email protected] 

tcp        0      0 192.168.130.66:22       192.168.53.137:49616    ESTABLISHED 13743/sshd: [email protected] 

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- 

[[email protected] ~]# 


13、Docker容器与宿主机文件共享相关操作

[[email protected] ~]# 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'  



以上是关于1Docker基础入门实战的主要内容,如果未能解决你的问题,请参考以下文章

小D课堂 - 零基础入门SpringBoot2.X到实战_汇总

Docker 基础实战教程:入门

Docker基础入门实战

Kafka入门实战教程基础概念与术语

如何入门爬虫(基础篇)

如何入门爬虫(基础篇)