1、Docker基础入门实战

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

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

[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'  



猜你喜欢

转载自blog.51cto.com/kaiyuandiantang/2133109