分层是自下往上
FROM 后面跟的是基础镜像
ADD 从宿主系统中往镜像文件中添加指定脚本(可以自己写)
VOLUME 可以使用宿主系统的空间 类似于挂载 (数据卷)
CMD 镜像制作完成后 执行指令/脚本
三种创建方式
基于已有镜像创建
docker commit [选项] 容器ID/名称 仓库名称:[标签]
-m 说明信息
-a 作者信息
-p 生成过程中停止容器的运行
基于本地模板创建
基于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 | 健康检查 |
FROM 镜像
定制的镜像都是基于FROM的镜像
指定新镜像所基于的镜像,第一条指令必须为FROM指令,每创建一个镜像就需要一条FROM指令
格式:
shell格式 RUN <命令行命令>
exec格式 RUN [“可执行文件”,“参数1”,“参数2”]
注意:Dockerfile的指令每执行一次都会在docker上新建一层。所以过多无意义的层,会造成经镜像膨胀过大。以 && 符号连接命令,这样执行后,只会创建 1 层镜像。
MAINTSINER 名字
说明镜像的维护人信息
RUN 命令
在所基于的镜像上执行命令,并提交到新的镜像中
RUN 是在 docker build。
为启动的容器指定默认要运行的程序,程序运行结束,容器也就结束。CMD 指令指定的程序可被 docker run 命令行参数中指定要运行的程序所覆盖。
CMD[ “要运行的程序”,“参数1”,“参数2”]
指令启动容器时要运行的命令或者脚本,Dockerfile只能有一条CMD命令,如果指定多条则只能最后一条被执行
用于运行程序
CMD 在docker run 时运行。
为启动的容器指定默认要运行的程序,程序运行结束,容器也就结束。CMD 指令指定的程序可被 docker run 命令行参数中指定要运行的程序所覆盖。
格式:
CMD
CMD ["<可执行文件或命令>","","",…]
CMD ["","",…] #该写法是为 ENTRYPOINT 指令指定的程序提供默认参数
ENTRYPOINT
类似于 CMD 指令,但其不会被 docker run 的命令行参数指定的指令所覆盖,而且这些命令行参数会被当作参数送给 ENTRYPOINT 指令指定的程序。
但是, 如果运行 docker run 时使用了 --entrypoint 选项,此选项的参数可当作要运行的程序覆盖 ENTRYPOINT 指令指定的程序。
在执行 docker run 的时候可以指定 ENTRYPOINT 运行所需的参数。
如果 Dockerfile 中如果存在多个 ENTRYPOINT 指令,仅最后一个生效。
格式:
ENTRYPOINT ["","","",…]
EXPOSE 端口号
指定新镜像加载到docker时要开启的端口
仅仅只是声明端口。
作用:
帮助镜像使用者理解这个镜像服务的守护端口,以方便配置映射。
在运行时使用随机端口映射时,也就是 docker run -P 时,会自动随机映射 EXPOSE 的端口。
格式:
EXPOSE <端口1> [<端口2>…]
ENV 环境变量 变量值
设置一个环境变量的值,会被后面的RUN使用
设置环境变量,定义了环境变量,那么在后续的指令中,就可以使用这个环境变量。
格式:
ENV
ENV = =…
ARG
构建参数,与 ENV 作用一至。不过作用域不一样。ARG 设置的环境变量仅对 Dockerfile 内有效,也就是说只有 docker build 的过程中有效,构建好的镜像内不存在此环境变量。
构建命令 docker build 中可以用 --build-arg <参数名>=<值> 来覆盖。
格式:
ARG <参数名>[=<默认值>]
ADD 源文件/目录 目标文件/目录
将源文件复制到目标文件,源文件要与Dockerfile位于相同的目录中,或者是一个URL
在执行 <源文件> 为 tar 压缩文件的话,压缩格式为 gzip, bzip2 以及 xz 的情况下,会自动复制并解压到 <目标路径>。
COPY 源文件/目录 目标文件/目录
将本地主机上的文件/目录复制到目标地点,源文件/目录要与Dockerfile在相同的目录中
复制指令,从上下文目录汇总复制文件或者目录到容器里指定路径。
COPY [–chown=:] <源路径1>… <目标路径>
COPY [–chown=:] ["<源路径1>",… “<目标路径>”]
[–chown=:]:可选参数,用户改变复制到容器内文件的拥有者和属组。
<源路径>:源文件或者源目录,这里可以是通配符表达式,其通配符规则要满足 Go 的 filepath.Match 规则。
<目标路径>:容器内的指定路径,该路径不用事先建好,路径不存在的话,会自动创建。
VOLUM [“目录”]
在容器中创建一个挂载点
定义匿名数据卷。在启动容器时忘记挂载数据卷,会自动挂载到匿名卷。
作用:
避免重要的数据,因容器重启而丢失,这是非常致命的。
避免容器不断变大。
格式:
VOLUME ["<路径1>", “<路径2>”…]
VOLUME <路径>
在启动容器 docker run 的时候,我们可以通过 -v 参数修改挂载点。
USER 用户名/UID
指定运行容器时的用户
用于指定执行后续命令的用户和用户组,这边只是切换后续命令执行的用户(用户和用户组必须提前已经存在)。
格式:
USER <用户名>[:<用户组>]
WORKDIR 路径
为后续的RUN、CMD、ENTRYPOINT指定工作目录
指定工作目录。用 WORKDIR 指定的工作目录,会在构建镜像的每一层中都存在。(WORKDIR 指定的工作目录,必须是提前创建好的)。
docker build 构建镜像过程中的,每一个 RUN 命令都是新建的一层。只有通过 WORKDIR 创建的目录才会一直存在。
格式:
WORKDIR <工作目录路径>
OMBUILD 命令
指定所生成的镜像作为一个基础镜像时所要运行的命令
用于延迟构建命令的执行。简单的说,就是 Dockerfile 里用 ONBUILD 指定的命令,在本次构建镜像的过程中不会执行(假设镜像为 test-build)。当有新的 Dockerfile 使用了之前构建的镜像 FROM test-build ,这是执行新镜像的 Dockerfile 构建时候,会执行 test-build 的 Dockerfile 里的 ONBUILD 指定的命令。
格式:
ONBUILD <其它指令>
HEALTHCHECK 健康检查
用于指定某个程序或者指令来监控 docker 容器服务的运行状态。
格式:
HEALTHCHECK [选项] CMD <命令>:设置检查容器健康状况的命令
HEALTHCHECK NONE:如果基础镜像有健康检查指令,使用这行可以屏蔽掉其健康检查指令
HEALTHCHECK [选项] CMD <命令> : 这边 CMD 后面跟随的命令使用,可以参考 CMD 的用法。
[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 随机映射一个端口
[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 ~]#
[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 9月 2 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
[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 8月 12 17:25 apache-tomcat-9.0.16.tar.gz
-rw-r--r--. 1 root root 181367942 9月 3 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
启动时报错
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
[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 10月 23 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
数据卷容器 容器和容器之间的数据共享
数据卷 容器和宿主之间的数据享
数据卷
[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
[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