1. 常用命令
docker version # 查看当前版本
docker search ubuntu #搜索镜像
sudo docker search -s 100 ubuntu # 查找 star 数至少为 100 的镜像,
#找出只有官方镜像 start 数超过 100,默认不加 s 选项找出所有相关 ubuntu 镜像
docker pull ubuntu # 获取 ubuntu 官方镜像 
sudo docker images # 查看当前镜像列表 
docker info # 显示docker信息
[root@localhost ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
jk/centos7-rabbitmq   latest              c91d28810b39        4 hours ago         1.095 GB
<none>                <none>              2d47bc393a83        4 hours ago         1.095 GB
jk/centos7-ssh        latest              06db3af51341        6 hours ago         259.4 MB
docker.io/centos      latest              67591570dd29        4 weeks ago         191.8 MB
[root@localhost ~]# docker rmi 2d47bc393a83   #删除镜像    
Deleted: sha256:2d47bc393a833e51d5a09b5b3480507ae28cad8909fd4ac1a1359bc8a9844392
Deleted: sha256:42723126f53d25676a3129a9758d89fd1f60570a81c3ef745ca1a84f25782c9a
  1. 运行一个容器
docker run -i -t ubuntu:14.04 /bin/bash
# docker run - 运行一个容器
# -t - 分配一个(伪)tty (link is external)
# -i - 交互模式 (so we can interact with it)
# -p  (端口映射例:-p 10001:22,可一次使用多个-p设置)
# --name参数可以指定启动后的容器名字,如果不指定则docker会帮我们取一个名字
# -v 挂在宿主主机到容器目录(-v /test:/soft 启动一个centos容器,宿主机的/test目录挂载到容器的/soft目录)
# ubuntu:14.04 - 使用 ubuntu 基础镜像 14.04
# /bin/bash - 运行命令 bash shell
# ubuntu 会有多个版本,通过指定 tag 来启动特定的版本 [image]:[tag]
docker start 容器ID #启动已有容器(ID通过docker ps -a 查看)
docker stop 容器ID #停止容器(ID通过docker ps查看)
docker rm 容器ID #删除容器(ID通过docker ps -a 查看)
[root@localhost ~]# docker run -it -p 10001:22 -p 10002:8080 --name centosrabbitmq docker.io/centos:latest /bin/bash
[root@localhost ~]# docker run -it -v /test:/soft -p 10001:22 -p 10002:8080 --name centosrabbitmq docker.io/centos:latest /bin/bash
#(启动一个centos容器,宿主机的/test目录挂载到容器的/soft目录,没有目录自动创建目录,删除容器后,不会删除目录)
[root@localhost ~]# docker start centosrabbitmq  #启动容器
[root@localhost ~]# docker exec -it centosrabbitmq /bin/bash #进入启动容器的交互模式
  1. 查看运行的容器
docker ps # 查看当前运行的容器
docker ps -a #列出当前系统所有的容器

4.将容器提交为镜像

$ docker commit c2df523c3ad8 hub注册名/base:latest
sha256:effb95017710c4ae92291d27ee150d236dc11fd5259258b1bd8936c1e0a8c5af
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED                  SIZE
base                latest              effb95017710        Less than a second ago   498.2 MB
ubuntu              latest              104bec311bcd        5 days ago               129 MB
cariden/centos6.7   latest              659d8194159a        13 months ago            431.4 MB
  1. docker 加速配置
#centos7 配置文件路径:/etc/sysconfig
#使用 https://www.daocloud.io/mirror
#配置文件追后添加:
  1. 推送镜像到docker-hub
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. 
If you don't have a Docker ID, head over to https://hub.docke
Username: 注册用户名
Password:
Login Succeeded
$ docker push hub注册名/base:latest

6.docker 启动一个容器

[root@localhost ~]# docker run -it --rm --net='none' docker.io/centos:latest /bin/bash
docker run -d -p 10006:22 --name zookeeper-01 jk/centos7-ssh /usr/sbin/sshd -D
  1. 制作包含(sshd) 镜像
[root@localhost ~]# docker search centos #搜索 centos镜像 OFFICIAL:OK 为官方
INDEX       NAME                                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                                 The official build of CentOS.                   3015      [OK]       
[root@localhost ~]# docker images  #查看镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    latest              67591570dd29        4 weeks ago         191.8 MB
[root@localhost ~]# docker run -it  -p 10001:22 --name centos_sshd docker.io/centos:latest /bin/bash #新运行一个容器(端口映射 10001到容器22,名称为centos_sshd)
[root@localhost ~]# docker ps #查看运行的容器
[root@localhost ~]# docker ps -a #查看所有容器
#官方centos镜像很干净,打部分基础应用都未安全
[root@localhost ~]#yum install passwd openssl openssh-server -y #安装passwd openssl openssh-server
[root@localhost ~]#yum install sshd -y #安装ssh
[root@d7b9602e7b1d ~]# yum install -y zip unzip #安装zip unzip
[root@d7b9602e7b1d bin]# yum install -y gcc-c++ #安装gcc
[root@d7b9602e7b1d bin]# yum -y install ncurses-devel
[root@d7b9602e7b1d bin]# yum install openssl-devel -y
[root@d7b9602e7b1d bin]#yum search ifconfig
[root@d7b9602e7b1d bin]#yum install -y net-tools.x86_64 #安装ifconfig
# /usr/sbin/sshd -D
这时报以下错误:
[root@ b5926410fe60 /]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key 
执行以下命令解决:
[root@b5926410fe60 /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''    
[root@b5926410fe60 /]#  ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@b5926410fe60 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key  -N '' 
然后,修改 /etc/ssh/sshd_config 配置信息:
UsePAM yes 改为 UsePAM no  #centos7不改,改为no 会报不支持
UsePrivilegeSeparation sandbox 改为 UsePrivilegeSeparation no
[root@b5926410fe60 /]# sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
[root@b5926410fe60 /]# sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config
修改完后,重新启动sshd
[root@b5926410fe60 /]# /usr/sbin/sshd -D
#修改root 密码
 [root@b5926410fe60 /]# passwd root
#将当前容器保存为镜像
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS                       PORTS               NAMES
fa86561ff434        docker.io/centos:latest   "/bin/bash"         46 minutes ago      Exited (137) 6 seconds ago                       centosrabbitmq
[root@localhost ~]# docker commit fa86561ff434 jk/centos7-ssh    
sha256:06db3af5134105bb3508e8b4d2241978981c61ac18ae1b1efc447b876139d66d
#在宿主机上基于新创建的镜像启动新的容器
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS                       PORTS               NAMES
fa86561ff434        docker.io/centos:latest   "/bin/bash"         49 minutes ago      Exited (137) 2 minutes ago                       centosrabbitmq
ab30ecb02c98        docker.io/centos:latest   "/bin/bash"         17 hours ago        Up About an hour                                 silly_wozniak
c9ba1913a750        docker.io/centos:latest   "/bin/bash"         17 hours ago        Exited (137) 14 hours ago                        big_joliot
ed7ba6f5c0f5        docker.io/centos:latest   "/bin/bash"         18 hours ago        Exited (0) 17 hours ago                          sharp_sinoussi
e4f15538d5bc        docker.io/centos:latest   "/bin/bash"         18 hours ago        Exited (0) 18 hours ago                          evil_goodall
[root@localhost ~]# docker rm -f fa86561ff434
fa86561ff434
[root@localhost ~]# docker images #查看镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jk/centos7-ssh      latest              06db3af51341        2 minutes ago       259.4 MB
docker.io/centos    latest              67591570dd29        4 weeks ago         191.8 MB
[root@localhost ~]# docker run -d -p 10001:22 jk/centos7-ssh:latest /usr/sbin/sshd -D    #使用新镜像启ssh   
d7b9602e7b1d03199e66ea5d057f98726389d5f82f326579a05e2d44e75ccc0e
[root@localhost ~]# docker ps #查看端口是否映射成功
CONTAINER ID        IMAGE                   COMMAND               CREATED             STATUS              PORTS                             NAMES
d7b9602e7b1d        jk/centos7-ssh:latest   "/usr/sbin/sshd -D"   10 seconds ago      Up 6 seconds        8080/tcp, 0.0.0.0:10001->22/tcp   cranky_morse
[root@localhost ~]# docker port d7b9602e7b1d #查看端口映射
22/tcp -> 0.0.0.0:10001
  1. 制作rabbitmq镜像(在前面制作的sshd基础镜像上制作),源码制作镜像,目前不能开机自启动,也不能直接run启动,需要启动容器后,执行exce 启动
#下载rabbitmq代码地址
http://www.rabbitmq.com/install-generic-unix.html
[root@d7b9602e7b1d install]# xz -d rabbitmq-server-generic-unix-3.6.6.tar.xz  #解压
[root@d7b9602e7b1d install]# tar -xvf rabbitmq-server-generic-unix-3.6.6.tar #解压
[root@d7b9602e7b1d install]# mv rabbitmq_server-3.6.6/ /usr/local/bin #移动rabbitmq
#下载安装最新版erlang,rabbitmq基础erlang
#erlang源码下载地址
http://www.erlang.org/downloads
[root@d7b9602e7b1d install]# tar -xvf otp_src_19.2.tar.gz #解压erlang源码
[root@d7b9602e7b1d otp_src_19.2]# ./configure --prefix=/usr/local/bin/erlang --without-javac #配置
checking for perl... no_perl
configure: error: Perl is required to generate v2 to v1 mib converter script
configure: error: /bin/sh '/usr/install/otp_src_19.2/lib/snmp/./configure' failed for snmp/.
configure: error: /bin/sh '/usr/install/otp_src_19.2/lib/configure' failed for lib
#报错:安装perl
[root@d7b9602e7b1d otp_src_19.2]# yum install perl -y #安装perl
[root@8fd9513bf75b otp_src_19.2]# yum install unixODBC-devel -y
[root@d7b9602e7b1d otp_src_19.2]# make #编译
[root@d7b9602e7b1d otp_src_19.2]# make install #安装
#/usr/local/bin 路径下有erlang目录
[root@d7b9602e7b1d etc]# vi /etc/profile #修改配置
#添加 export PATH=$PATH:/usr/local/bin/erlang/bin:/usr/local/bin/rabbitmq_server-3.6.6/sbin
[root@d7b9602e7b1d etc]# source /etc/profile #使配置生效
[root@d7b9602e7b1d etc]# erl #查看安装是否成功
[root@d7b9602e7b1d etc]# rabbitmq-server & #后台启动rabbitmq-server
[root@d7b9602e7b1d etc]# rabbitmq-plugins enable rabbitmq_management #启动web管理
#WEB管理界面的默认用户名和密码都是guest,guest拥有所有权限,只能localhost登录
#http://localhost:15672/
#创建可登录远程控制台用户
[root@6f8427e14e71 ~]# rabbitmqctl add_user admin admin #添加用户,用户名/密码 admin/admin
[root@6f8427e14e71 ~]# rabbitmqctl list_users #显示用户列表
Listing users ...
admin   []
guest   [administrator]
[root@6f8427e14e71 ~]# rabbitmqctl set_user_tags admin administrator #标记为管理员
[root@6f8427e14e71 ~]# rabbitmqctl set_permissions -p / admin ".*" ".*" ".*" #分配所有权限
[root@ba23634843ed ~]# rabbitmqctl  delete_user admin1 #删除用户
Deleting user "admin1" ...
 (rabbitmq详细说明)
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND               CREATED             STATUS              PORTS                             NAMES
d7b9602e7b1d        jk/centos7-ssh:latest   "/usr/sbin/sshd -D"   About an hour ago   Up About an hour    8080/tcp, 0.0.0.0:10001->22/tcp   cranky_morse
[root@localhost ~]# docker commit d7b9602e7b1d jk/centos7-rabbitmq
sha256:c91d28810b39d3e9e59d224cfcd06363c8df4bd75ef569deea434d6ff3067c18
[root@localhost ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
jk/centos7-rabbitmq   latest              c91d28810b39        21 seconds ago      1.095 GB
<none>                <none>              2d47bc393a83        2 minutes ago       1.095 GB
jk/centos7-ssh        latest              06db3af51341        About an hour ago   259.4 MB
docker.io/centos      latest              67591570dd29        4 weeks ago         191.8 MB
[root@localhost ~]# docker run -d -p 10001:22 -p 10002:15672 -p 10003:5672 jk/centos7-rabbitmq:latest /usr/sbin/sshd -D
[root@localhost ~]# docker run -d -p 10001:22 -p 10002:15672 -p 10003:5672 jk/centos7-rabbitmq-t:latest /usr/sbin/init #使rc.local systemctl 有效
[root@localhost ~]# docker exec eb8850f6075c /usr/local/bin/rabbitmq_server-3.6.6/sbin/rabbitmq-server -detached #启动rabbitmq-server
eac0abe66bf46d4a3a8fe743d3601a1c00df2d7212859026c9ef594f995d7e1d
  1. 直接使用制作好的rabbitmq
docker pull rabbitmq:management
docker run -d --name rabbitmq --publish 5671:5671 --publish 5672:5672 --publish 4369:4369 --publish 25672:25672 --publish 15671:15671 --publish 15672:15672 rabbitmq:management
http://192.168.0.26:5672(WEB管理页面)
  1. 制作rabbitmq镜像(在前面制作的sshd基础镜像上制作)(erlang rabbitmq 都基于rpm包安装,容器的yum找不到包)
[root@localhost ~]#wget http://www.rabbitmq.com/releases/erlang/erlang-18.1-1.el7.centos.x86_64.rpm
[root@localhost ~]#wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.3/rabbitmq-server-3.6.3-1.noarch.rpm
[root@localhost ~]#rpm -ivh erlang-18.1-1.el7.centos.x86_64.rpm
[root@localhost ~]#rpm -ivh rabbitmq-server-3.6.3-1.noarch.rpm
[root@79932eb25595 install]# rpm -ivh rabbitmq-server-3.6.3-1.noarch.rpm 
warning: rabbitmq-server-3.6.3-1.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 6026dfca: NOKEY
error: Failed dependencies:
        initscripts is needed by rabbitmq-server-3.6.3-1.noarch
        logrotate is needed by rabbitmq-server-3.6.3-1.noarch
        socat is needed by rabbitmq-server-3.6.3-1.noarch
[root@79932eb25595 install]# yum install initscripts -y
[root@79932eb25595 install]# yum install logrotate -y
[root@79932eb25595 install]# yum install socat -y
[root@localhost ~]#rabbitmq-plugins enable rabbitmq_management #开启web管理
[root@localhost ~]#rabbitmqctl stop #停止服务
[root@localhost ~]#rabbitmq-server -detached #启动服务(后台)
  1. 制作rocker镜像
[root@localhost ~]# docker run -d -p 10005:22 -p 10006:9876 -p 10007:10911 jk/centos7-ssh:latest /usr/sbin/sshd -D
#安装git
[root@b5eaa70483ff install]# yum install -y git
#安装jdk
[root@b5eaa70483ff install]# rpm -ivh jdk-8u111-linux-x64.rpm
[root@b5eaa70483ff install]# echo 'export JAVA_HOME=/usr/java/jdk1.8.0_111' >> /etc/profile
#安装maven
[root@b5eaa70483ff install]# unzip apache-maven-3.3.3.zip
[root@b5eaa70483ff install]# echo 'export M2_HOME=/usr/local/maven/apache-maven-3.3.3' >> /etc/profile
[root@b5eaa70483ff install]# echo 'export M2=$M2_HOME/bin' >> /etc/profile
[root@b5eaa70483ff install]# echo 'export MAVEN_OPTS="-Xms256m-Xmx512m"' >> /etc/profile
[root@b5eaa70483ff install]# echo 'export PATH=$M2:$PATH' >> /etc/profile
https://github.com/alibaba/RocketMQ/ #RocketMQ源码地址
[root@b5eaa70483ff install]# wget https://codeload.github.com/alibaba/RocketMQ/zip/v3.5.8
[root@b5eaa70483ff install]# mv v3.5.8 RocketMQ-3.5.8.zip
[root@b5eaa70483ff install]# unzip RocketMQ-3.5.8.zip
[root@b5eaa70483ff install]# cd RocketMQ-3.5.8
[root@b5eaa70483ff install]# chmod 775 install.sh
[root@b5eaa70483ff install]# ./install.sh #安装RockerMQ
#启动服务
[root@b5eaa70483ff devenv]# cd bin
[root@b5eaa70483ff bin]# ls
README.md      cleancache.v1.sh  mqadmin.xml   mqbroker.numanode0  mqbroker.numanode3  mqfiltersrv.exe  mqnamesrv.exe  os.sh         runserver.sh  tools.sh
cachedog.sh    mqadmin           mqbroker      mqbroker.numanode1  mqbroker.xml        mqfiltersrv.xml  mqnamesrv.xml  play.sh       setcache.sh
cleancache.sh  mqadmin.exe       mqbroker.exe  mqbroker.numanode2  mqfiltersrv         mqnamesrv        mqshutdown     runbroker.sh  startfsrv.sh
#启动nameserver
[root@b5eaa70483ff bin]# nohup ./mqnamesrv & 
#启动mqbroker,指定Name Server地址,多个用分号隔开(优先级按照高低顺序排序)
[root@b5eaa70483ff bin]# nohup ./mqbroker -n '127.0.0.1:9876' &
[root@b5eaa70483ff local]# jps  #查看是否启动成功
81 BrokerStartup
42 NamesrvStartup
138 Jps
#创建topic
[root@b5eaa70483ff bin]# sh mqadmin updateTopic
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0
usage: mqadmin updateTopic [-b <arg>] [-c <arg>] [-h] [-n <arg>] [-o <arg>] [-p <arg>] [-r <arg>] [-s <arg>]
       -t <arg> [-u <arg>] [-w <arg>]
 -b,--brokerAddr <arg>       create topic to which broker
 -c,--clusterName <arg>      create topic to which cluster
 -h,--help                   Print help
 -n,--namesrvAddr <arg>      Name server address list, eg: 192.168.0.1:9876;192.168.0.2:9876
 -o,--order <arg>            set topic's order(true|false
 -p,--perm <arg>             set topic's permission(2|4|6), intro[2:W 4:R; 6:RW]
 -r,--readQueueNums <arg>    set read queue nums
 -s,--hasUnitSub <arg>       has unit sub (true|false
 -t,--topic <arg>            topic name
 -u,--unit <arg>             is unit topic (true|false
 -w,--writeQueueNums <arg>   set write queue nums
[root@b5eaa70483ff bin]# sh mqadmin updateTopic -n 127.0.0.1:9876 -c DefaultCluster -t TopicTest1
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0
create topic to 172.17.0.3:10911 success.
TopicConfig [topicName=TopicTest1, readQueueNums=8, writeQueueNums=8, perm=RW-, topicFilterType=SINGLE_TAG, topicSysFlag=0, order=false]
#删除topic
[root@b5eaa70483ff bin]#  sh mqadmin deleteTopic -n 127.0.0.1:9876 -c DefaultCluster -t TopicTest1 
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0
delete topic [TopicTest1] from cluster [DefaultCluster] success.
delete topic [TopicTest1] from NameServer success.
#查看topic信息
[root@b5eaa70483ff bin]# sh mqadmin topicList -n 127.0.0.1:9876
#查看topic统计信息
[root@b5eaa70483ff bin]# sh mqadmin topicStatus -n 127.0.0.1:9876 -t TopicTest1            
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0
#Broker Name                      #QID  #Min Offset           #Max Offset             #Last Updated
b5eaa70483ff                      0     0                     0                       
b5eaa70483ff                      1     0                     0                       
b5eaa70483ff                      2     0                     0                       
b5eaa70483ff                      3     0                     0                       
b5eaa70483ff                      4     0                     0                       
b5eaa70483ff                      5     0                     0                       
b5eaa70483ff                      6     0                     0                       
b5eaa70483ff                      7     0                     0      
#查看所有消费组group
[root@b5eaa70483ff bin]#  sh mqadmin consumerProgress -n 127.0.0.1:9876            
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0
#Group                            #Count  #Version                 #Type  #Model          #TPS     #Diff Total
#查看指定消费组下的所有topic数据堆积情况
[root@b5eaa70483ff bin]# sh mqadmin consumerProgress -n 127.0.0.1:9876 -g ConsumerGroupName
[root@localhost ~]# docker commit b5eaa70483ff jk/centos7-rocketmq #创建镜像
[root@localhost ~]# docker run -d -p 10005:22 -p 10006:9876 -p 10007:10911 jk/centos7-rocketmq:latest /usr/sbin/sshd -D
#启动mqnamesrv
[root@localhost ~]# docker exec b5eaa70483ff /root/install/RocketMQ-3.5.8/devenv/bin/mqnamesrv &
#启动mqbroker
[root@localhost ~]# docker exec b5eaa70483ff /root/install/RocketMQ-3.5.8/devenv/bin/mqbroker -n '127.0.0.1:9876' &
  1. 导出导入镜像或容器
#导出 export - 导入 import
格式:docker export CONTAINER(容器)
[root@localhost export]# docker export 2eb03bb3e1e1 > centos7-chandao.tar
[docker@localhost tmp]$ docker import /usr/docker/tmp/centos7-chandao.tar  
sha256:c912b8e3df6b06aa6bbfdd00ca576774929b3232c04e28e8ce33d67df971df9c
[docker@localhost tmp]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              c912b8e3df6b        41 seconds ago      646.5 MB
#保存save - 加载 load
格式:docker save IMAGE(镜像)
#导出容器
[root@localhost export]# docker save a1fa62e467d1 > /root/export/centos7-chandao.tar
[docker@localhost tmp]$ docker load < /usr/docker/tmp/centos7-chandao.tar
34e7b85d83e4: Loading layer [==================================================>] 199.9 MB/199.9 MB
e478b89b6784: Loading layer [==================================================>] 145.1 MB/145.1 MB
155532329cf9: Loading layer [==================================================>] 337.4 MB/337.4 MB
39bcb51a11d5: Loading layer [==================================================>]  63.3 MB/63.3 MB
Loaded image ID: sha256:a1fa62e467d1039c84076d921489898419c37bb82c17f7eb0da3661e385ab951
[docker@localhost tmp]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              a1fa62e467d1        27 hours ago        730 MB
#导出 export 与 保存 save 的区别
 (1).export导出的镜像文件大小  小于 save保存的镜像
 (2).export 导出(import导入)是根据容器拿到的镜像,再导入时会丢失镜像所有的历史,所以无法进行回滚操作(docker tag <LAYER ID> <IMAGE NAME>);而save保存(load加载)的镜像,没有丢失镜像的历史,可以回滚到之前的层(layer)。(查看方式:docker images --tree)
  1. 启动容器设置规定IP
#创建自定义网络
#备注:这里选取了172.18.0.0网段,也可以指定其他任意空闲的网段
[root@localhost ~]# docker network create --subnet=172.18.0.0/16 manualnet
注:shadown为自定义网桥的名字,可自己任意取名。
#在你自定义的网段选取任意IP地址作为你要启动的container的静态IP地址
#备注:这里在第二步中创建的网段中选取了172.18.0.10作为静态IP地址。
[root@localhost ~]# docker run -d -p 10001:22 --net manualnet --ip 172.18.0.10 jk/centos7-ssh:latest /usr/sbin/sshd -D
  1. 设置容器与宿主主机同一个网段
[root@localhost ~]# wget https://github.com/jpetazzo/pipework/archive/master.zip
[root@localhost install]# unzip master.zip
[root@localhost install]# cp pipework-master/pipework  /usr/local/bin/
[root@localhost pipework-master]# chmod +x /usr/local/bin/pipework
  1. 解决centos7镜像不能使用systemctl 启动服务
-- 错误
[root@0af76365122a etc]# systemctl start sshd
Failed to get D-Bus connection: Operation not permitted
-- 创建容器时 添加 /usr/sbin/init
docker run -d -p 10121:22 -p 10122:8080 --net manualnet2 --ip 172.19.0.12 --name gitlab3 jk/centos7-ssh:latest /usr/sbin/init /usr/sbin/sshd -D
-- 获创建容器时不启动 /usr/sbin/sshd -D
 docker run -d -p 10121:22 -p 10122:8080 --net manualnet2 --ip 172.19.0.12 --name gitlab3 jk/centos7-ssh:latest /usr/sbin/init
-- 进入容器启动sshd服务
[root@0af76365122a etc]# systemctl start sshd