当前位置: 代码迷 >> 综合 >> docker镜像制作、数据管理
  详细解决方案

docker镜像制作、数据管理

热度:41   发布时间:2024-02-21 09:00:32.0

镜像制作

文章目录

  • 镜像制作
    • docker镜像的分层
    • docker镜像简介
    • docker镜像的创建方式
      • 实验
        • 容器安装Apache
        • 容器开启ssh
        • 容器使用systemctl
        • Tomcat容器
        • mysql容器(手工编译)
    • Docker的数据管理
      • 数据卷
      • 私有仓库的建立
      • 端口映射
      • 容器互联

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 updateRUN yum -y install httpd 开启80端口EXPOSE 80 (内端口过会要映射一个端口)复制网站首页文件ADD index.html /var/www/html/index.html将执行脚本复制到镜像中ADD run.sh /run.shRUN 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/bashrm -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 DockerfileFROM 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 daemonLoaded: 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 daemonLoaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)Active: active (running) since Mon 2020-09-21 08:01:09 UTC; 2s agoDocs: man:sshd(8)man:sshd_config(5)Main PID: 55 (sshd)CGroup: /docker/3fccd7f83d5015141f9648c047e37d77bf3a8b7b0fb54191ccbc56a97d76721e/system.slice/sshd.service└─55 /usr/sbin/sshd -DSep 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 DockerfileFROM 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 "deamon 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 distributionCopyright (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]# systemctl stop firewalld
[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 distributionCopyright (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 1500inet 172.17.0.4  netmask 255.255.0.0  broadcast 172.17.255.255ether 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 0TX packets 3101  bytes 170965 (166.9 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0loop  txqueuelen 1000  (Local Loopback)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX 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 1500inet 172.17.0.5  netmask 255.255.0.0  broadcast 172.17.255.255ether 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 0TX packets 2636  bytes 145827 (142.4 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0loop  txqueuelen 1000  (Local Loopback)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX 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