docker映象製作、資料管理

2020-09-23 12:01:00

映象製作

docker映象的分層

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-jTWVw99T-1600760512918)(C:\Users\hu\AppData\Roaming\Typora\typora-user-images\1600743718009.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-vyaXq02g-1600760512920)(C:\Users\hu\AppData\Roaming\Typora\typora-user-images\1600743731249.png)]

分層是自下往上

FROM 後面跟的是基礎映象

ADD 從宿主系統中往映象檔案中新增指定指令碼(可以自己寫)

VOLUME 可以使用宿主系統的空間 類似於掛載 (資料卷)

CMD 映象製作完成後 執行指令/指令碼

docker映象簡介

  • 應用釋出的標準格式
  • 支撐一個docker容器的執行
    - [外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-rgygIrhd-1600760512920)(C:\Users\hu\AppData\Roaming\Typora\typora-user-images\1600743872239.png)]

docker映象的建立方式

三種建立方式

  • 基於已有映象建立

    • 將容器裡面執行的程式及執行環境打包生成新的映象

    docker commit [選項] 容器ID/名稱 倉庫名稱:[標籤]

    -m 說明資訊

    -a 作者資訊

    -p 生成過程中停止容器的執行

  • 基於本地模板建立

    • 通過匯入作業系統模板檔案生成新的映象
    • 使用wget命令匯入為本地映象
    • 匯入成功後可檢視本地映象資訊
  • 基於dockerfile建立

    • Dockerfile是由一組指令組成的檔案
    • dockerfile結構四部分
      • 基礎映象資訊
      • 維護者資訊
      • 映象操作指令
      • 容器啟動時執行命令
    • dockerfile每行支援一條指令,每條指令可攜帶多個引數,支援使用以「#」號開頭的註釋

dockerfile操作指令

指令含義
FROM 映象指定新映象所基於的映象,第一條指令必須為FROM指令,沒建立一個映象就需要一條FROM指令
MAINTSINER 名字說明映象的維護人資訊
RUN命令在所基於的映象上執行命令,並提交到新的映象中
CMD[ 「要執行的程式」,「引數1」,「引數2」]指令啟動容器時要執行的命令或者指令碼,Dockerfile只能有一條CMD命令,如果指定多條則只能最後一條被執行
EXPOSE 埠號指定新映象載入到docker時要開啟的埠
ENV 環境變數 變數值設定一個環境變數的值,會被後面的RUn使用
ADD 原始檔/目錄 目標檔案/目錄將原始檔複製到目標檔案,原始檔要與Dockerfile位於相同的目錄中,或者是一個URL
COPY 原始檔/目錄 目標檔案/目錄將本地主機上的檔案/目錄複製到目標地點,原始檔/目錄要與Dockerfile在相同的目錄中
VOLUM [「目錄」]在容器中建立一個掛載點
USER 使用者名稱/UID指定執行容器時的使用者
WORKDIR 路徑為後續的RUN、CMD、ENTRYPOINT指定工作目錄
OMBUILD 命令指定所生成的映象作為一個基礎映象時所要執行的命令
HEALTHCHECK健康檢查

實驗

容器安裝Apache

[root@localhost opt]# mkdir apache 

[root@localhost opt]# cd apache

[root@localhost apache]# vim Dockerfile 

基於基礎映象

FROM centos:7

維護映象的使用者資訊

MAINTAINER This is hui 

映象操作指令安裝Apache軟體

RUN yum -y update

RUN yum -y install httpd 

開啟80埠

EXPOSE 80 (內埠過會要對映一個埠)

複製網站首頁檔案

ADD index.html /var/www/html/index.html

將執行指令碼複製到映象中

ADD run.sh /run.sh

RUN chmod 755 /run.sh

啟動容器時執行指令碼

CMD ["/run.sh"]



[root@localhost apache]# vim index.html 

<h1>this is hui </h1>

[root@localhost apache]# vim run.sh 

#!/bin/bash

rm -rf /run/httpd/*   刪除多餘的pid檔案相當於初始化

exec /usr/sbin/apachectl -D FOREGROUND 啟動Apache



執行

[root@localhost apache]# docker build -t httpd:centos .   (最後有一個空格和點)

載入每一層,每執行一步 會建立一個臨時容器 執行完後就會刪除

[root@localhost apache]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               centos              c1b1b6ac3906        20 minutes ago      419MB
centos              7                   7e6257c9f8d8        5 weeks ago         203MB



[root@localhost apache]# docker run -d -p 1123:80 httpd:centos

[root@localhost apache]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
8f05a37c2ba6        httpd:centos        "/run.sh"           7 minutes ago       Up 7 minutes        0.0.0.0:1123->80/tcp   epic_mayer

-P 隨機對映一個埠

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-kyaISYDz-1600760512922)(C:\Users\hu\AppData\Roaming\Typora\typora-user-images\1600587710230.png)]

容器開啟ssh

[root@localhost ~]# cd /opt
[root@localhost opt]# mkdir sshd
[root@localhost opt]# cd sshd
[root@localhost sshd]# vim Dockerfile

FROM centos:7
MAINTAINER The CentOS Project <cloud-centos>
RUN yum -y update
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo '123456' | passwd --stdin root
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN sed -i '/^session\s\+required\s\+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]

[root@localhost sshd]# docker build -t sshd -t sshd:new .

[root@localhost sshd]# docker run -d -P sshd:new
6684b7a32ed3c8cde55751659024ef7f5419007411ff13401208e767536064df

[root@localhost sshd]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
6684b7a32ed3        sshd:new            "/usr/sbin/sshd -D"   32 seconds ago      Up 32 seconds       0.0.0.0:32769->22/tcp   romantic_shamir

[root@localhost sshd]# ssh localhost -p 32769
The authenticity of host '[localhost]:32769 ([::1]:32769)' can't be established.
RSA key fingerprint is SHA256:rRDAbTc+TPcRGn/ZINgwQwNlOb1wubfdhM8B4MXvAq0.
RSA key fingerprint is MD5:23:20:48:d9:67:38:17:79:82:8d:70:85:35:9a:b1:6c.
Are you sure you want to continue connecting (yes/no)? yes  //輸入yes
Warning: Permanently added '[localhost]:32769' (RSA) to the list of known hosts.
root@localhost's password:  //輸入設定的密碼123456
[root@6684b7a32ed3 ~]# 

容器使用systemctl

[root@localhost opt]# mkdir systemctl 
[root@localhost opt]# cd systemctl
[root@localhost systemctl]# vim Dockerfile
FROM sshd:new
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/;for i in *;do [$i == \
systemd-tmpfiles-setup.service ] || rm -f $i;done);\
rm -f /lib/systemd/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/local-fs.target.wants/*;\
rm -f /lib/systemd/sockets.target.wants/*udev*;\
rm -f /lib/systemd/sockets.target.wants/*initctl*;\
rm -f /lib/systemd/basic.target.wants/*;\
rm -f /lib/systemd/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]

[root@localhost systemctl]# docker build -t systemd:new .

[root@localhost systemctl]# docker run --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:new /sbin/init &
[1] 99516

[root@localhost systemctl]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
3fccd7f83d50        systemd:new         "/sbin/init"          48 seconds ago      Up 46 seconds       22/tcp                  happy_merkle
6684b7a32ed3        sshd:new            "/usr/sbin/sshd -D"   39 minutes ago      Up 39 minutes       0.0.0.0:32769->22/tcp   romantic_shamir

[root@localhost systemctl]# docker exec -it 3fccd7f83d50 /bin/bash

[root@3fccd7f83d50 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:sshd(8)
           man:sshd_config(5)

[root@3fccd7f83d50 /]# systemctl start sshd

[root@3fccd7f83d50 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-09-21 08:01:09 UTC; 2s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 55 (sshd)
   CGroup: /docker/3fccd7f83d5015141f9648c047e37d77bf3a8b7b0fb54191ccbc56a97d76721e/system.slice/sshd.service
           └─55 /usr/sbin/sshd -D

Sep 21 08:01:09 3fccd7f83d50 systemd[1]: Starting OpenSSH server daemon...
Sep 21 08:01:09 3fccd7f83d50 sshd[55]: WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and...lems.
Sep 21 08:01:09 3fccd7f83d50 sshd[55]: Server listening on 0.0.0.0 port 22.
Sep 21 08:01:09 3fccd7f83d50 sshd[55]: Server listening on :: port 22.
Sep 21 08:01:09 3fccd7f83d50 systemd[1]: Started OpenSSH server daemon.
Hint: Some lines were ellipsized, use -l to show in full.

3.nginx容器(手工編譯)

ADD 除了能複製還能對壓縮包進行解壓 解壓到新增目錄

[root@localhost opt]# mkdir nginx
[root@localhost opt]# cd nginx
[root@localhost nginx]# ll
總用量 960
-rw-r--r--. 1 root root 980831 92 17:14 nginx-1.12.0.tar.gz

[root@localhost nginx]# vim Dockerfile

FROM centos:7
MAINTAINER this is nginx images
RUN yum -y update
RUN yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src
WORKDIR /usr/local/src/nginx-1.12.0/
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]

[root@localhost nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx

[root@localhost nginx]# docker build -t nginx:new .

[root@localhost nginx]# docker run -d -P nginx:new
99527449f21649b01002b1cb42a8f8dda529e88c5b125a309a6ecedb4dcb887c
[root@localhost nginx]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                                           NAMES
aa44e6f0d3f1        nginx:new           "/run.sh"                13 seconds ago      Up 12 seconds              0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   hardcore_wescoff
3fccd7f83d50        systemd:new         "/sbin/init"          58 minutes ago      Up 58 minutes       22/tcp                                          happy_merkle
6684b7a32ed3        sshd:new            "/usr/sbin/sshd -D"   2 hours ago         Up 2 hours          0.0.0.0:32769->22/tcp                           romantic_shamir
[root@localhost nginx]# setenforce 0
[root@localhost nginx]# systemctl stop firewalld

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-8tSjgVVJ-1600760512922)(C:\Users\hu\AppData\Roaming\Typora\typora-user-images\1600679770257.png)]

Tomcat容器

[root@localhost nginx]# cd /opt
[root@localhost opt]# mkdir tomcat
[root@localhost opt]# cd tomcat/
[root@localhost tomcat]# ll
總用量 187652
-rw-r--r--. 1 root root  10783080 812 17:25 apache-tomcat-9.0.16.tar.gz
-rw-r--r--. 1 root root 181367942 93 16:12 jdk-8u91-linux-x64.tar.gz

[root@localhost tomcat]# vim Dockerfile
RUN mv jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /usr/local/java
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
ADD apache-tomcat-9.0.16.tar.gz /usr/local
WORKDIR /usr/local
RUN mv apache-tomcat-9.0.16 /usr/local/tomcat8
EXPOSE 8080
ENTRYPOINT ["/usr/local/tomcat8/bin/catalina.sh","run"]

[root@localhost tomcat]# docker run -d --name tomcat01 -p 1123:8080 tomcat:new
c8c3ee509d1808224cf4edfcddc227de94bc357b4352cc0e5521b4fc2d04cd3c
[root@localhost tomcat]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
c8c3ee509d18        tomcat:new          "/usr/local/tomcat8/…"   9 seconds ago       Up 8 seconds        0.0.0.0:1123->8080/tcp   tomcat01

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-rcv8uKO5-1600760512923)(C:\Users\hu\AppData\Roaming\Typora\typora-user-images\1600683988810.png)]

啟動時報錯

docker: Error response from daemon: driver failed programming external connectivity on endpoint tomcat01 (2fb9c01a021d437c4ba3d01cb0437719d9efc00903dc2c545a77a5f89ab62ae5):  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 1123 -j DNAT --to-destination 172.17.0.3:8080 ! -i docker0: iptables: No chain/target/match by that name.
 (exit status 1)).

解決
[root@localhost tomcat]# systemctl restart docker

刪除容器或者重新命名

[root@localhost tomcat]# docker ps -a | awk '{print "docker rm " $1 }' | bash

mysql容器(手工編譯)

[root@localhost tomcat]# cd /opt
[root@localhost opt]# mkdir mysql
[root@localhost opt]# cd mysql
[root@localhost mysql]# vim my.cnf 
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
#

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZER
O_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

[root@localhost mysql]# ll
總用量 47696
-rw-r--r--. 1 root root 48833145 1023 2017 mysql-boost-5.7.20.tar.gz

[root@localhost mysql]# vim Dockerfile
FROM centos:7
MAINTAINER build image mysqld
RUN yum -y install \
ncurses \
ncurses-devel \
autoconf \
bison \
make \
cmake \
gcc \
gcc-c++
RUN useradd -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make && make install
RUN chown -R mysql:mysql /usr/local/mysql/
RUN rm -rf /etc/my.cnf
ADD my.cnf /etc
RUN chown mysql:mysql /etc/my.cnf
ENV PATH /usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/
RUN bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306
RUN echo -e "#!/bin/bash \nsystemctl enable mysqld" > /run.sh
RUN chmod 755 /run.sh
RUN sh /run.sh
CMD ["init"]

[root@localhost ~]# docker build -t centos:mysql .
[root@localhost mysql]# docker run --name=mysql_server -d -P --privileged centos:mysql /usr/sbin/init
06da4f89c789a73af05df55ab2ac87381754cfc3ae106e6e25584a11940b9885
[root@localhost mysql]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
06da4f89c789        centos:mysql        "/usr/sbin/init"    32 seconds ago      Up 31 seconds       0.0.0.0:32768->3306/tcp   mysql_server
[root@localhost mysql]# docker exec -it 06da4f89c789 /bin/bash
[root@06da4f89c789 mysql]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant all privileges on *.* to 'root'@'%' identified  by 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> create database school;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

[root@localhost mysql]# setenforce 0
[root@localhost mysql]# iptables -F

驗證
開一個新的終端
[root@localhost ~]# yum -y install mariadb*
[root@localhost ~]# mysql -h 192.168.20.50 -u root -P 32768 -p 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

刪除none映象

docker ps -a|grep "Exited"|awk '{print $1}'|xargs docker stop
docker ps -a|grep "Exited"|awk '{print $1}'|xargs docker rm
docker images|grep none|awk '{print $3 }'|xargs docker rmi

Docker的資料管理

  • 資料管理操作
    • 方便檢視容器內產生的資料
    • 多容器間實現資料共用
  • 兩種管理方式
    • 資料卷
    • 資料卷容器

資料卷容器 容器和容器之間的資料共用

資料卷 容器和宿主之間的資料享

資料卷

  • 資料卷是一個提供容器使用的特殊目錄

資料卷

[root@localhost apache]# docker pull centos:7

宿主機目錄/var/www掛載容器中的/data1

[root@localhost apache]# docker run -v /var/www:/data1 --name web1 -it centos:7 /bin/bash

[root@9db6dfc71788 /]# ls
anaconda-post.log  data1  etc   lib    media  opt   root  sbin  sys  usr
bin                dev    home  lib64  mnt    proc  run   srv   tmp  var

[root@9db6dfc71788 /]# cd data1

[root@9db6dfc71788 data1]# touch 1.txt

[root@9db6dfc71788 data1]# ls
1.txt

另一個終端

[root@localhost ~]# cd /var/www

[root@localhost www]# ls
1.txt

資料卷容器

[root@localhost apache]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS                  NAMES
9db6dfc71788        centos:7            "/bin/bash"         2 minutes ago       Exited (127) 2 seconds ago                          web1
8f05a37c2ba6        httpd:centos        "/run.sh"           About an hour ago   Up About an hour             0.0.0.0:1123->80/tcp   epic_mayer

[root@localhost apache]# docker run --name web100 -v /data1 -v /data2 -it centos:7 /bin/bash

[root@ac804f314989 /]# ls
anaconda-post.log  data1  dev  home  lib64  mnt  proc  run   srv  tmp  var
bin                data2  etc  lib   media  opt  root  sbin  sys  usr

另一臺終端

[root@localhost ~]# docker run -it --volumes-from web100 --name web200 centos:7 /bin/bash

[root@888b9c20cb25 /]# ls
anaconda-post.log  data1  dev  home  lib64  mnt  proc  run   srv  tmp  var
bin                data2  etc  lib   media  opt  root  sbin  sys  usr

[root@888b9c20cb25 /]#  cd data1 

[root@888b9c20cb25 data1]# touch 123.txt

[root@888b9c20cb25 data1]# cd ..

[root@888b9c20cb25 /]# cd data2 

[root@888b9c20cb25 data2]# touch 456.txt

另一個終端驗證同步

[root@ac804f314989 /]# cd data1

[root@ac804f314989 data1]# ls
123.txt

[root@ac804f314989 data1]# cd ..

[root@ac804f314989 /]# cd data2

[root@ac804f314989 data2]# ls
456.txt

私有倉庫的建立

私有倉庫(個人映象倉庫)

[root@localhost opt]# docker pull registry

[root@localhost opt]# vim /etc/docker/daemon.json

"insecure-registries": ["192.168.20.50:5000"],     //新增。注意最後y有個,不加服務起不來

[root@localhost opt]# systemctl restart docker

[root@localhost opt]# docker create -it registry /bin/bash
13d2be46f309cbfc98182d1d56f92aedbdf46476a71474332b64ec7511f5b7

[root@localhost opt]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
13d2be46f309        registry            "/entrypoint.sh /bin…"   7 minutes ago       Created                                           crazy_chaum
888b9c20cb25        centos:7            "/bin/bash"              14 minutes ago      Exited (137) 11 minutes ago                       web200
ac804f314989        centos:7            "/bin/bash"              15 minutes ago      Exited (0) 12 minutes ago                         web100
9db6dfc71788        centos:7            "/bin/bash"              18 minutes ago      Exited (127) 15 minutes ago                       web1
8f05a37c2ba6        httpd:centos        "/run.sh"                About an hour ago   Exited (137) 11 minutes ago                       epic_mayer

[root@localhost opt]# docker start 13d2be46f309 
13d2be46f309

[root@localhost opt]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry
4213ef914d8100a803e3ad3b00f17db5912f5318bd5d04afc8041b856f603bb4

[root@localhost opt]# docker pull nginx

[root@localhost opt]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               centos              c1b1b6ac3906        2 hours ago         419MB
nginx               latest              7e4d58f0e5f3        9 days ago          133MB
centos              7                   7e6257c9f8d8        5 weeks ago         203MB
registry            latest              2d4f4b5309b1        3 months ago        26.2MB

[root@localhost opt]# docker tag nginx:latest 192.168.20.50:5000/nginx

[root@localhost opt]# docker images 
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
httpd                      centos              c1b1b6ac3906        2 hours ago         419MB
192.168.20.50:5000/nginx   latest              7e4d58f0e5f3        9 days ago          133MB
nginx                      latest              7e4d58f0e5f3        9 days ago          133MB
centos                     7                   7e6257c9f8d8        5 weeks ago         203MB
registry                   latest              2d4f4b5309b1        3 months ago        26.2MB

[root@localhost opt]# curl -XGET http://192.168.20.50:5000/v2/_catalog
{"repositories":[]}

[root@localhost opt]# docker push 192.168.20.50:5000/nginx

[root@localhost opt]# curl -XGET http://192.168.20.50:5000/v2/_catalog
{"repositories":["nginx"]}

測試

[root@localhost opt]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
httpd                      centos              c1b1b6ac3906        2 hours ago         419MB
192.168.20.50:5000/nginx   latest              7e4d58f0e5f3        9 days ago          133MB
nginx                      latest              7e4d58f0e5f3        9 days ago          133MB
centos                     7                   7e6257c9f8d8        5 weeks ago         203MB
registry                   latest              2d4f4b5309b1        3 months ago        26.2MB
[root@localhost opt]# docker rmi nginx
Untagged: nginx:latest
Untagged: nginx@sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
[root@localhost opt]# docker rmi 192.168.20.50:5000/nginx:latest 
Untagged: 192.168.20.50:5000/nginx:latest
Untagged: 192.168.20.50:5000/nginx@sha256:794275d96b4ab96eeb954728a7bf11156570e8372ecd5ed0cbc7280313a27d19
Deleted: sha256:7e4d58f0e5f3b60077e9a5d96b4be1b974b5a484f54f9393000a99f3b6816e3d
Deleted: sha256:eedfd37abc0b05520a719addf6675b372687fe9a2ed1746fa988335720d4376c
Deleted: sha256:4f052ddd517f6dc5b74d2f7d881838b8ec6a5e67ec7a1da8bbbe91e1e068ad3a
Deleted: sha256:5f78154d356577280e4ac0d9bf0d021d488828e57f953b011dcbadaecd539ce4
Deleted: sha256:db5783668220e16186080f3e9d69ad748aab8c72abb8e457f6fc45369461634b
Deleted: sha256:07cab433985205f29909739f511777a810f4a9aff486355b71308bb654cdc868

[root@localhost opt]# docker pull 192.168.20.50:5000/nginx
Using default tag: latest
latest: Pulling from nginx
d121f8d1c412: Pull complete 
ebd81fc8c071: Pull complete 
655316c160af: Pull complete 
d15953c0e0f8: Pull complete 
2ee525c5c3cc: Pull complete 
Digest: sha256:794275d96b4ab96eeb954728a7bf11156570e8372ecd5ed0cbc7280313a27d19
Status: Downloaded newer image for 192.168.20.50:5000/nginx:latest
192.168.20.50:5000/nginx:latest

[root@localhost opt]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
httpd                      centos              c1b1b6ac3906        2 hours ago         419MB
192.168.20.50:5000/nginx   latest              7e4d58f0e5f3        9 days ago          133MB
centos                     7                   7e6257c9f8d8        5 weeks ago         203MB
registry                   latest              2d4f4b5309b1        3 months ago        26.2MB

埠對映

[root@localhost opt]# docker run -d -P 192.168.20.50:5000/nginx
53d26ca86a11cae2b815fa6cf72fdb9f164fb3e49f0a0ad04898184fb22098d3

[root@localhost opt]# docker ps 
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
53d26ca86a11        192.168.20.50:5000/nginx   "/docker-entrypoint.…"   15 seconds ago      Up 14 seconds       0.0.0.0:32768->80/tcp    dazzling_shirley
4213ef914d81        registry                   "/entrypoint.sh /etc…"   11 minutes ago      Up 11 minutes       0.0.0.0:5000->5000/tcp   elegant_galois

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-omN7z1Em-1600760512924)(C:\Users\hu\AppData\Roaming\Typora\typora-user-images\1600593880986.png)]

容器互聯

[root@localhost opt]# docker run -itd -P --name web11 centos:7
0b4e50fdbe23f17ab4b40f8dea0ebc2e11f01f146ea27c49b0df28a13e14e850

[root@localhost opt]# docker ps -a 
CONTAINER ID        IMAGE                      COMMAND                  CREATED              STATUS                        PORTS                    NAMES
0b4e50fdbe23        centos:7                   "/bin/bash"              7 seconds ago        Up 6 seconds                                           web11

[root@localhost opt]# docker exec -it 0b4e50fdbe23 /bin/bash

再開啟一個終端

[root@localhost ~]# docker run -itd -P --name web12 --link web11:web11 centos:7 /bin/bash
349164b10b351970ac95ff147e1bb1e2bf864fb37b7e48beaf0b430948e0d30c

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                      COMMAND                  CREATED              STATUS                        PORTS                    NAMES
349164b10b35        centos:7                   "/bin/bash"              2 seconds ago        Up 1 second                                            web12
0b4e50fdbe23        centos:7                   "/bin/bash"              About a minute ago   Up About a minute                                      web11

[root@localhost ~]# docker exec -it 349164b10b35 /bin/bash

[root@349164b10b35 /]# ping web11
PING web11 (172.17.0.4) 56(84) bytes of data.
64 bytes from web11 (172.17.0.4): icmp_seq=1 ttl=64 time=0.084 ms
64 bytes from web11 (172.17.0.4): icmp_seq=2 ttl=64 time=0.048 ms
64 bytes from web11 (172.17.0.4): icmp_seq=3 ttl=64 time=0.049 ms
^C
--- web11 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.048/0.060/0.084/0.017 ms



[root@0b4e50fdbe23 /]# yum install net-tools -y 

[root@0b4e50fdbe23 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.4  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)
        RX packets 4961  bytes 12032149 (11.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3101  bytes 170965 (166.9 KiB)
        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
        loop  txqueuelen 1000  (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



[root@349164b10b35 /]# yum install net-tools -y 

[root@349164b10b35 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.5  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:05  txqueuelen 0  (Ethernet)
        RX packets 3734  bytes 11961122 (11.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2636  bytes 145827 (142.4 KiB)
        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
        loop  txqueuelen 1000  (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
 install net-tools -y 

[root@349164b10b35 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.5  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:05  txqueuelen 0  (Ethernet)
        RX packets 3734  bytes 11961122 (11.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2636  bytes 145827 (142.4 KiB)
        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
        loop  txqueuelen 1000  (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