docker linux 使用

  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.將容器提交爲鏡像html

$ 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 啓動一個容器java

[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" ...
https://my.oschina.net/u/1179666/blog/492953 (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
相關文章
相關標籤/搜索