docker镜像制作

南笙酒味 提交于 2019-11-26 17:07:11

1.端口映射

一:容器端口随机映射到物理机的一个端口-P

二:容器的端口映射到物理机的一个特定的端口 -p 80:80

三:容器的端口映射到物理机的一个特定的网卡上的随机端口 -p ip::80

四:容器的端口映射到物理机的一个特定的网卡上的特定端口 -p ip:80:80

 

2.数据卷—实现数据的持久化

一:bind mount volume

-v /ken:/var/www/html

 

二:docker managerment volume

-v /var/www/html

 

三:基于一个现有的容器

–volumes-from

 

 

docker使用数据库实现数据持久化演示

 

第一步:搜索数据库镜像并下载相应的数据库镜像

[root@localhost ~]# docker search mysql

 

第二步:启动数据库

[root@localhost ~]# docker run -d -v /ken:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123 --name mysql1 mysql

 

-e 指定设置环境变量。MYSQL_ROOT_PASSWORD指定数据库登录的初始密码

 

第三步:访问数据库

[root@localhost ~]# docker run -it mysql mysql -uroot -p123 -h172.17.0.2
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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> create database ken;
Query OK, 1 row affected (0.22 sec)

 

第四步:销毁容器并验证数据持久化

[root@localhost ~]# docker run -d -v /ken:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 mysql
d9655ecf33e7b8f11fb231e9b0a398f61d48268c084ef184e110fced55491364
[root@localhost ~]# docker exec -it d9655ecf33e7b8f11fb23 bash
root@d9655ecf33e7:/# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
root@d9655ecf33e7:/# mysql -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ken                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> 

 

 

两个容器之间建立连接

[root@localhost ~]# docker run -it --link mysql:mysql mysql mysql -uroot -p123 -hmysql
Unable to find image 'mysql:latest' locally
^C
[root@localhost ~]# docker run -it --link flamboyant_gauss:mysql mysql mysql -uroot -p123 -hmysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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> 

--link flamboyant_gauss:mysql

格式:容器名:别名

 

Dokcefile详解

Dockerfile编写注意项

  • # 井号进行备注
  • 指令参数,指令的大小写不敏感,但是规范来写都是大写!
  • 第一个非注释行必须是FROM指令
  • 编写Dockerfile必须在一个目录下进行,这个目录称之为 工作目录(WORKSPACE)
  • Dockerfile文件命令的首字母必须大写
  • 制作镜像所要用的文件必须放在工作目录或者工作目录的子目录之下,不能放在父目录
  • 可以通过隐藏文件 .dockeringnore 来指定不要放入到镜像中的文件,一行是一个文件,可以用通配符
  • 基于dockerfile做镜像,本质上还是基于一个现有的镜像做新镜像

 

dockerfile常用指令

 

1.FROM指令

作用:指定使用哪个基础镜像
格式:

FROM 镜像名    注册表/镜像名:tag

例如:

FROM busybox

 

2.ENV

作用:设置环境变量

格式:

1.ENV 变量名=变量值

2.ENV 变量名 变量值

 

例如:

ENV name=ken age=18

引用变量:$name $age

 

3.label

作用:设置标签,注释

格式:

label key=value

例如:

LABEL author=ken

 

4.RUN

作用:执行命令

格式:

RUN CMD

 

多个命令:

方法一:

RUN CMD1;CMD2

 

方法二:

RUN CMD1&&CMD2

 

注意:

1.因为docker镜像是分层机制,应当尽量减少层次,所以如果在创建镜像的时候需要执行多个命令,建议使用一个RUN 指令,执行多个命令。

2.RUN 指令默认是在shell中环境中执行,docker镜像当中的shell类型默认为/bin/sh/ -c

 

5.COPY

作用:复制文件到镜像中

格式:

COPY src dest/

注意:

  • 源文件路径用相对路径,目标一般用绝对路径
  • 也可以通配符
  • 源文件必须在工作目录或者工作目录的子目录中
  • 目标路径可以不存在,会自动创建
  • 如果源文件是一个目录,会自动递归复制目录下的文件到目标位置,但是目录自身不会复制
  • 如果复制多个文件,或者源文件中用了通配符,那么目标路径必须以 / 为结尾

 

6.WORKDIR

作用:指定工作目录

格式:

WORKDIR path

例如:

WORKDIR /ken

 

7.EXPOSE 80

作用:暴露容器端口

格式:

EXPOSE PORT

例如:

EXPOSE 80/tcp

 

注意:这里声明暴露端口并不是说在启动容器的时候,容器会自动暴露相关的端口,而是需要管理员使用-P 选型进行端口暴露。

 

8.VOLUME

作用:声明需要映射的数据卷

格式:

VOLUME path

例如:

VOLUME /usr/share/nginx/html/

注意:如果在构建镜像时使用volume声明了需要暴露的数据卷路径,在启动容器的时候回自动和宿主机的一个目录进行绑定!docker manaagerment volume

 

9.ADD

作用:和copy相似,也是复制文件到镜像中,可以在网上下载文件到镜像中,也可以自动复制tar.gz包到镜像中并实现自动解决

格式:

ADD sec dest

例如:

ADD web.tar.gz /usr/share/nginx/html/

 

10.CMD    注意!CMD和ENTRYPOINT执行的命令都是pid为1的命令

作用:执行命令,一个Dockerfile中可以有多个CMD指令,但是只有最后一个CMD指令生效

三种格式:

格式1:

CMD com  –>是在shell中执行相关的cmd,默认类型为/bin/sh -c

例如:

CMD cat test –> /bin/sh -c cat test

格式2:

CMD [“com”,”para1″,”para2″]

例如:

CMD [“cat”,”test”]  –>cat test

 

格式三:

CMD [“para1″,”para2”]  –>需要和ENTRYPOIN指令一起使用,CMD的选项会被当做参数传递给ENTRYPOINT指令

 

11.ENTRYPOINT

作用:也是执行命令

格式:

格式1:

ENTRYPOINT cat test –> /bin/sh -c cat test

格式二:

ENTRYPOINT  [“com”,”para1″,”para2″]

 

 

12,ONBUILD

作用:定义触发器

格式:

ONBUILD cmd

例如:

ONBUILD yum install lrzsz -y

 

 

演示1:使用nginx镜像来演示上述所有指令

第一步:编写dockerfile

[root@localhost ~]# mkdir /nginx
[root@localhost ~]# cd /nginx
[root@localhost nginx]# vim Dockerfile
#this is for test nginx
FROM nginx:latest
LABEL author=ken
ENV path=/usr/share/nginx/html/ pack=nginx-1.12.2.tar.gz
WORKDIR $path
COPY jd1  $path
RUN touch testnginx
ADD https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/at-sysvinit-3.1.13-24.el7.x86_64.rpm ./
ADD $pack ./
EXPOSE 80
VOLUME $path
ONBUILD ADD https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/at-sysvinit-3.1.13-24.el7.x86_64.rpm ./
CMD ["-g","daemon off;"]
ENTRYPOINT ["nginx"]
FROM nginx:latest   来自那个镜像
LABEL author=ken    标签 
ENV path=/usr/share/nginx/html/ pack=nginx-1.12.2.tar.gz    定义变量
WORKDIR $path    指定工作目录
COPY jd1  $path  复制文件到工作目录
RUN touch testnginx  执行命令
ADD https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/at-sysvinit-3.1.13-24.el7.x86_64.rpm ./   下载文件
ADD $pack ./   自动解压当前文件
EXPOSE 80      暴露端口
VOLUME $path   挂载目录
ONBUILD ADD https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/at-sysvinit-3.1.13-24.el7.x86_64.rpm ./  触发器
CMD ["-g","daemon off;"]  启动
ENTRYPOINT ["nginx"]

 

 

 

第二步:构建镜像

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

 

第三步:查看镜像信息

[root@localhost nginx]# docker image ls
[root@localhost nginx]# docker history nginx:v1

 

第四步:使用创建的镜像启动容器

工作目录:

数据卷:/var/lib/docker/volume/

端口:

[root@localhost nginx2]# docker run -d -P nginx:v2

 

错误日志:

[root@localhost nginx]# docker logs 2db0a5411551
2019/08/15 03:18:30 [emerg] 1#1: unexpected end of parameter, expecting ";" in command line
nginx: [emerg] unexpected end of parameter, expecting ";" in command line

 

 

 

 

 

 

 

 

 

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!