docker示例

 

批量删除容器
docker rm $(docker ps -aq)


1 真机
[root@room9pc01 docker 1]# mkdir /var/ftp/docker
[root@room9pc01 docker 1]# mv docker-engine-* /var/ftp/docker
[root@room9pc01 docker 1]# cd /var/ftp
[root@room9pc01 ftp]# ls docker/
docker-engine-1.12.1-1.el7.centos.x86_64.rpm
docker-engine-selinux-1.12.1-1.el7.centos.noarch.rpm
[root@room9pc01 docker 1]# createrepo /var/ftp/docker/

2 虚拟机(docker1)
[root@node1 ~]# hostnamectl set-hostname docker1
[root@node1 ~]# hostname docker1
[root@docker1 network-scripts]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
# Generated by dracut initrd
DEVICE="eth0"
ONBOOT="yes"
IPV6INIT="no"
IPV4_FAILURE_FATAL="no"
NM_CONTROLLED="no"
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR="192.168.4.1"
PREFIX=24
GATEWAY=192.168.4.254
[root@docker1 ~]# systemctl restart network
yum 源
[root@docker1 ~]# vim /etc/yum.repos.d/CentOS.repo    #新加yum源
[docker]
name=docker
baseurl=ftp://192.168.4.254/docker
enabled=1
gpgcheck=0

 

[root@node2 ~]# hostnamectl set-hostname docker2
[root@node2 ~]# hostname docker2
# Generated by dracut initrd
DEVICE="eth0"
ONBOOT="yes"
IPV6INIT="no"
IPV4_FAILURE_FATAL="no"
NM_CONTROLLED="no"
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR="192.168.4.2"
PREFIX=24
GATEWAY=192.168.4.254
[root@docker2 ~]# systemctl restart network
[root@docker2 ~]# vim /etc/yum.repos.d/CentOS.repo    #新加yum源
[docker]
name=docker
baseurl=ftp://192.168.4.254/docker
enabled=1
gpgcheck=0

3 虚拟机安装docker
[root@docker1 ~]# yum -y install docker-engine
[root@docker1 ~]# systemctl restart docker
[root@docker1 ~]# systemctl enable docker
[root@docker1 ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 02:42:21:0f:0b:28  txqueuelen 0  (Ethernet)
        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
[root@docker1 ~]# docker version

[root@docker2 ~]# yum -y install docker-engine
[root@docker2 ~]# systemctl restart docker
[root@docker2 ~]# systemctl enable docker
[root@docker2 ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 02:42:af:6e:df:f4  txqueuelen 0  (Ethernet)
        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
[root@docker2 ~]# docker version

下载镜像
[root@docker1 ~]# docker pull busybox

上传镜像
[root@docker1 ~]# docker push busybox

查看镜像
[root@docker1 ~]# docker images

查找busybox镜像
[root@docker1 ~]# docker search busybox

导出busybox镜像为busybox.tar
[root@docker1 ~]# docker  save  busybox:latest >busybox.tar

导入镜像
[root@docker1 ~]# scp  busybox.tar 192.168.4.2:/root
[root@docker2 ~]# docker load <busybox.tar
[root@docker2 ~]# docker images

删除镜像
[root@docker2 ~]# docker rmi  busybox

一次性导入多个镜像
[root@docker1 ~]# yum -y install unzip
[root@docker1 ~]# unzip docker_images.zip
[root@docker1 ~]# ls
[root@docker1 ~]# cd docker_images
[root@docker1 docker_images]# ls
[root@docker1 docker_images]# docker images
[root@docker1 docker_images]# for i in *; do docker load <$i; done

启动镜像
[root@docker1 docker_images]# docker run -it centos /bin/bash
[root@docker1 docker_images]# docker run -itd centos /bin/bash        #放入后台

配yum源
[root@7a652fc72a9f yum.repos.d]#vi dvd.repo
[local]
name=local
baseurl=ftp://192.168.4.254/centos
enable=1
gpgcheck=0

[root@docker1 ~]# docker run -d nginx    #放后台运行
[root@docker1 ~]# docker ps    #查看正在运行的容器
[root@docker1 ~]# docker ps -q        #查看正在运行的容器,只显示容器的ID号    
23b0f656c7c8
[root@docker1 ~]# docker ps  -a    #查看所有的容器,包括没有运行的
[root@docker1 ~]# docker ps -qa    #查看所有的容器,包括没有运行的,只显示容器的ID号
[root@docker1 ~]# docker history centos    #查看某镜像的制作过程
[root@docker1 ~]# docker rmi nginx    #删除某镜像,nginx为镜像名字,删除镜像前要先容器,否则会报错
[root@docker1 ~]# docker stop 容器ID     #停止某容器
[root@docker1 ~]# docker rm 容器ID    #删除某容器

[root@docker1 ~]# docker tag centos:latest cen:v1        #修改镜像的名字和标签,默认标签为latest,改为v1
[root@docker1 ~]# docker inspect centos    #查看镜像的底层信息
[root@docker1 ~]# docker ps -a        #查看所有容器,其中包含有容器ID
[root@docker1 ~]# docker start 23    #启动容器,23为容器ID
[root@docker1 ~]# docker stop 23    #停止容器
[root@docker1 ~]# docker restart 23    #重启容器
[root@docker1 ~]# docker rm 23        #删除容器,若在使用中,会报错,应先关闭,在删除

连接容器
[root@docker1 ~]# docker attach 23    #中断连接(exit),容器会关闭
[root@docker1 ~]# docker exec -it 23 /bin/bash    #中断连接(exit),容器不会关闭
[root@docker1 ~]# docker top 23    #查看容器进程列表

 

制作自定义镜像
[root@docker1 ~]# docker run -it centos    #用镜像启动容器
[root@f36a0daa967d /]# cd /etc/yum.repos.d/    #修改yum源
[root@f36a0daa967d yum.repos.d]# rm -rf *
[dvd]
name=dvd
baseurl=ftp://192.168.4.254/rhel7
enabled=1
gpgcheck=0
[root@f36a0daa967d yum.repos.d]# yum -y install net-tools iproute psmisc vim-enhanced    #测试软件
[root@f36a0daa967d ~]# ifconfig    #查看是否有IP
[root@f36a0daa967d ~]# exit
[root@docker1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
f36a0daa967d        centos              "/bin/bash"         11 minutes ago      Exited (0) 39 seconds ago                       peaceful_poitras
[root@docker1 ~]# docker start f3    #启动docker
f3
[root@docker1 ~]# docker ps
[root@docker1 ~]# docker commit f3 myos:v1        #制作docker镜像
sha256:0fd7501c8fc7a9fd333381c61690d0b07d8531111cd58d9d26a1c5f7f0b9d9c5
[root@docker1 ~]# docker images    #查看镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myos                v1                  0fd7501c8fc7        14 seconds ago      302.8 MB
[root@docker1 ~]# docker run -it myos:v1    #验证,使用镜像启动容器
[root@91137e9419cb /]# ifconfig


使用Dockerfile文件创建一个新的镜像文件
Dockerfile语法格式:
– FROM:基础镜像
– MAINTAINER:镜像创建者信息(说明)
– EXPOSE:开放的端口
– ENV:设置环境变量
– ADD:复制文件到镜像
– RUN:制作镜像时执行的命令,可以有多个
– WORKDIR:定义容器默认工作目录
– CMD:容器启动时执行的命令,仅可以有一条CMD

创建一个Apache的镜像文件(以文件的方式来达到创建镜像的目的)
[root@docker1 ~]# mkdir oo
[root@docker1 ~]# cd oo
[root@docker1 oo]# touch Dockerfile    #Dockerfile文件首字母大写
[root@docker1 oo]# ls
Dockerfile
[root@docker1 oo]# cp /etc/yum.repos.d/CentOS.repo ./
[root@docker1 oo]# ls
CentOS.repo  Dockerfile
[root@docker1 oo]# vim Dockerfile
 
[root@docker1 oo]# cat Dockerfile     #执行时要去掉注释
FROM myos:v1    #以myos:1作为基础镜像
RUN yum -y install httpd    #在制作镜像时执行此命令
ENV EnvironmentFile=/etc/sysconfig/httpd    #设置环境变量
WORKDIR /var/www/html/    #定义容器的默认工作目录
RUN echo "test" > /var/www/html/index.html
EXPOSE 80     #设置开放端口
CMD ["/usr/sbin/httpd","-DFOREGROUND"]    #容器启动时执行此命令
[root@docker1 oo]# docker build -t myos:http .
[root@docker1 oo]# docker run -d myos:http
7531699269ed5d4112b18f8ef7755953a6d9369db405e9077ef0b0f57f891202
[root@docker1 oo]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
7531699269ed        myos:http           "/usr/sbin/httpd -DFO"   46 seconds ago      Up 43 seconds       80/tcp              hopeful_swirles
[root@docker1 oo]# docker inspect 75
[root@docker1 oo]# curl 172.17.0.3    #上条命令查到的IP
test


创建私有镜像仓库
自定义私有仓库
[root@docker1 oo]# vim /etc/docker/daemon.json    #不写此文件会报错
[root@docker1 oo]# cat /etc/docker/daemon.json
{
"insecure-registries":["192.168.4.1:5000"]        #使用私有仓库运行容器
}
[root@docker1 oo]# systemctl restart docker
[root@docker1 oo]# docker run -d -p 5000:5000 registry
79a7eedb26978229fdd2f24021e3826c3ef00554cfcc9a381f3775bc65408d57
[root@docker1 oo]# curl 192.168.4.1:5000/v2/
{}
[root@docker1 oo]# docker tag myos:http 192.168.4.1:5000/myos:http    #打标签
[root@docker1 oo]# docker push 192.168.4.1:5000/myos:http    #上传
The push refers to a repository [192.168.4.1:5000/myos]
eec11c270257: Pushed
c1453bc59a38: Pushed
275d86320fa0: Pushed
43e653f84b79: Pushed
http: digest: sha256:e3afe596e055a3eced3708030867990e496567eb866436c82f7d2e6c8242e3e1 size: 1160

在docker2上操作
[root@docker2 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              452a96d81c30        5 months ago        79.62 MB
centos              latest              e934aafc2206        6 months ago        198.6 MB
registry            latest              d1fd7d86a825        9 months ago        33.26 MB
nginx               latest              a5311a310510        2 years ago         181.4 MB
redis               latest              1aa84b1b434e        2 years ago         182.8 MB

[root@docker2 docker]# scp 192.168.4.1:/etc/docker/daemon.json /etc/docker/
[root@docker2 docker]# docker run -it 192.168.4.1:5000/myos:http /bin/bash    #直接可以启动
[root@84fccab57149 html]# rpm -qa httpd
httpd-2.4.6-67.el7.x86_64

查看私有仓库
[root@docker1 oo]# curl http://192.168.4.1:5000/v2/_catalog    #查看镜像
{"repositories":["myos"]}
[root@docker1 oo]# curl http://192.168.4.1:5000/v2/myos/tags/list    #查看镜像标签
{"name":"myos","tags":["http"]}

 

NFS共享存储
[root@node3 ~]# yum -y install nfs-utils
[root@node3 ~]# mkdir /content
[root@node3 ~]# vim /etc/exports
/content *(rw,no_root_squash)
[root@node3 ~]# systemctl restart nfs-server.service
[root@node3 ~]# systemctl restart nfs-secure.service
[root@node3 ~]# exportfs -rv
exporting *:/content
[root@node3 ~]# chmod 777 /content
[root@node3 ~]# echo 11 > /content/index.html

[root@docker1 oo]# yum -y install nfs-utils
[root@docker1 oo]# systemctl restart nfs-server.service
[root@docker1 oo]# showmount -e 192.168.4.3
Export list for 192.168.4.3:
/content *
[root@docker1 oo]# mkdir /mnt/qq
[root@docker1 oo]# mount -t nfs 192.168.4.3:/content /mnt/qq
[root@docker1 oo]# ls /mnt/qq
index.html
[root@docker1 oo]# docker run -d -p 80:80 -v /mnt/qq:/var/www/html -it myos:http
42b49d8b16449c1fbcc5f365be6e5a03e7a95228e056b37f82db45c04fc56376
[root@docker1 oo]# curl 192.168.4.1
11

[root@docker2 ~]# yum -y install nfs-utils
[root@docker2 ~]# showmount -e 192.168.4.3
Export list for 192.168.4.3:
/content *
[root@docker2 ~]# mkdir /mnt/qq
[root@docker2 ~]# mount -t nfs 192.168.4.3:/content /mnt/qq
[root@docker2 ~]# docker run -d -p 80:80 -v /mnt/qq:/var/www/html -it 192.168.4.1:5000/myos:http
84cf0f06816c5135254cbb7fd0a36d6b3398459bacfa821a007404c3b7154b69
[root@docker2 ~]# curl 192.168.4.2
11

[root@docker1 oo]# touch /mnt/qq/a.sh
[root@docker1 oo]# echo 22 > /mnt/qq/index.html
[root@docker2 ~]# ls /mnt/qq
a.sh  index.html
[root@docker2 ~]# cat /mnt/qq/index.html
22


创建自定义网桥

新建Docker1网络模型
[root@docker1 oo]# docker network create --subnet=172.30.0.0/16 docker01
9c8f353eba54d5efc2be00979112639c6837776c86f38049ca52f6ccb4986baa
[root@docker1 oo]# docker network list
NETWORK ID          NAME                DRIVER              SCOPE
e0925bb39eab        bridge              bridge              local               
9c8f353eba54        docker01            bridge              local               
0bfee7bdd8c2        host                host                local               
9cb8d1b8d55b        none                null                local               
[root@docker1 oo]# ip a s
[root@docker1 oo]# docker network inspect docker01
[root@docker1 oo]# docker run --network=docker01 -id nginx
6fd08e79e644ab247edad7ac37b80203f467b9fb38e5301e3b3735b20695e479
[root@docker1 oo]# docker run -p 8080:80 -id nginx
68426a194eb42a7705505d8e93cfa743f6a15e11a3a03c02ffba6704cba4f16d
[root@docker1 oo]# curl 192.168.4.1:8080

新建一个docker02网络模型
[root@docker1 oo]# docker network create --driver bridge docker02
ebc155cc220fea47d459b8c15755723030317741d1dfa8f88813300ecb21ae46
[root@docker1 oo]# ifconfig
br-9c8f353eba54: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.30.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:4fff:fed3:7f2d  prefixlen 64  scopeid 0x20<link>
        ether 02:42:4f:d3:7f:2d  txqueuelen 0  (Ethernet)
        RX packets 14  bytes 1264 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 26  bytes 1943 (1.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@docker1 oo]# docker network list
NETWORK ID          NAME                DRIVER              SCOPE
e0925bb39eab        bridge              bridge              local               
9c8f353eba54        docker01            bridge              local               
ebc155cc220f        docker02            bridge              local               
0bfee7bdd8c2        host                host                local               
9cb8d1b8d55b        none                null                local

[root@docker1 oo]# docker network inspect e0925bb39eab
 "com.docker.network.bridge.name": "docker0",

[root@docker1 oo]# docker network rm docker02    #删除docker02
docker02
[root@docker1 oo]# docker network create docker02 -o com.docker.network.bridge.name=docker02    #创建docker02网桥
9436c5633b846eb9061a5d25a3cc484041bb8e20693978716fc386ae71c7dea9
[root@docker1 oo]# ifconfig
docker02: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 02:42:cd:75:2b:eb  txqueuelen 0  (Ethernet)
        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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值