docker

Posted john5yang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker相关的知识,希望对你有一定的参考价值。

Namespaces 命名空间,是linux内核提供的一种对进程资源隔离的机制,比如网络,进程,挂载点等

CGroup 对资源进行限制,防止cpu或内存用的过多等

ls -l /sys/fs/cgroup/

?

虚拟机与docker区别

虚拟机是在硬件基础上通过hypervisor虚拟出硬件,比如虚拟出多个cpu等....

docker是在操作系统上的进程,共享宿主机内核,docker上有文件系统,库文件等,,,

?

docker history nginx:latest

查看镜像层

  1. docker容器的网络访问

?

一, 指定映射(docker 会自动添加一条iptables规则来实现端口映射)

启动docker后会有个docker0的网卡,docker把网关指向docker0 通过内核转发上外网

docker网络主要依赖于iptables

[[email protected] ~]# iptables -t nat -L -n

?

[[email protected] ~]# tcpdump port 80 -nn -i docker0

[[email protected] ~]# tcpdump port 81 -nn -i eth0

?

?

二. docker网络端口设置的5种方法

????-p hostPo????rt:containerPort

????-p ip:hostPort:containerPort

????-p ip::containerPort(随机端口)

????-p hostPort:containerPort:udp

????-p 81:80 p 443:443 可以指定多个-p

?

1, docker 端口映射

把宿主机的端口81映射到docker的80

[[email protected] ~]# docker run -d -p 81:80 nginx:latest

?

?

2, -p ip:hostPort:containerPort

[[email protected] yum.repos.d]# ifconfig eth0:1 10.0.0.100/24 up

[[email protected] yum.repos.d]# docker run -d -p 10.0.0.11:80:80 nginx:latest

23f1620839e3c06810500f6712edf5def2674af25f9d2921ef6f2b56713636ff

[[email protected] yum.repos.d]# docker run -d -p 10.0.0.100:80:80 nginx:latest

8ae65d993f00e16bd4e814f22a5648351572fc2aa2a8da4281b3976062bc940b

?

?

3. -p ip::containerPort(随机端口)

[[email protected] ~]# docker run -d -p 10.0.0.100::80 nginx:latest

?

4. -p 81:80 –p 443:443 可以指定多个-p

?

5. 随机映射

docker run -P (随机端口)

[[email protected] ~]# docker run -d -P nginx:latest

?

三. 数据卷(文件或目录)

docker卷有2种

docker 自行维护管理的卷

/var/lib/docker/volumes/xxxx/_date

绑定挂载卷

?

?

把宿主机的文件,挂载到docker nginx里

mkdir /opt/xiaoniao

把程序放到 /opt/xiaoniao/下

语法: docker run -d -p 80:80 -v 宿主机文件目录:docker nginx容器站点目录 nginx:latest

[[email protected] ~]# docker run -d -p 80:80 -v /opt/xiaoniao:/usr/share/nginx/html nginx:latest

?

数据卷容器

  1. 共享挂载

[[email protected] ~]# docker run -d -p 81:80 --volumes-from 其他容器名nginx:latest

[[email protected] ~]# docker run -d -p 81:80 --volumes-from gracious_stallman nginx:latest

?

四, 基于nginx启动一个容器,监听80和81,访问80,出现nginx默认首页,访问81,出现小鸟

-p 指定多个端口

-v指定多个文件 挂载配置文件

?

1.在/opt下创建nginx 配置文件

[[email protected] ~]# cat /opt/xiaoniao.conf

server {

listen 81;

server_name localhost;

location / {

root /opt/xiaoniao;

index index.html index.htm;

}

}

?

2.启动容器

docker run -d -p 宿主机端口:dockernginx端口 -p 81:81 -v宿主机nginx配置文件: docker nginx配置文件 -v /opt/xiaoniao:/opt/xiaoniao nginx:latest

docker run -d -p 80:80 -p 81:81 -v /opt/xiaoniao.conf:/etc/nginx/conf.d/xiaoniao.conf -v /opt/xiaoniao:/opt/xiaoniao nginx:latest

?

?

3.测试

浏览器访问10.0.0.11出现nginx欢迎界面

访问10.0.0.11:81出现小鸟

?

[[email protected] opt]# docker exec -it elated_proskuriakova /bin/bash

-v 指定的 配置文件在docker nginx 被挂载了

[email protected]:/# cat /etc/nginx/conf.d/xiaoniao.conf

server {

listen 81;

server_name localhost;

location / {

root /opt/xiaoniao;

index index.html index.htm;

}

}

?

?

五,手动将容器保存为镜像

手动制作镜像步骤

启动一个容器,安装服务

把刚才安装好服务的容器,提交为镜像

测试

1):基于容器制作镜像

1 echo ‘192.168.20.200 mirrors.aliyun.com‘ >>/etc/hosts

2 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

3 yum install openssh-server -y

4 /etc/init.d/sshd start

5 echo 123456|passwd --stdin root

6 docker run -d -p 1122:22 cnetos:6.9 -p 1122 暴露端口,防止端口冲突+

7 windows连接测试

shell ssh 10.0.0.11 1022 直接连接容器

8 提交镜像 docker commit 容器id或者容器的名字 新的镜像名字[:版本号可选]

docker commit docker 5cef7c611c35 centos6.9_ssh:v1

9 查看

docker images

?

?

vi /init.sh

#!/bin/bash

/etc/init.d/httpd start

/usr/sbin/sshd -D

?

chmod +x /init.sh

?

?

2)将容器提交为镜像

docker commit oldboy centos6-ssh-httpd:v1

?

3)测试镜像功能是否可用

六,dockerfile

企业使用容器,写dockerfile就是基本功

dockerfile主要组成部分:

????基础镜像信息 FROM:centos:6.9

????制作镜像操作指令 RUN yum install openssh-server -y

????容器启动时执行的初始命令 CMD ["/bin/bash"]

?

?

七 centos6 构建ssh_httpd

创建目录准备构建镜像

dockerfile 文件的名字必须叫dockerfile

[[email protected] opt]# mkdir dockerfile

[[email protected] opt]# cd dockerfile

[[email protected] dockerfile]# mkdir centos6.9_ssh

[[email protected] dockerfile]# cd centos6.9_ssh

[[email protected] centos6.9_ssh]# vim dockerfile

[[email protected] ~]# cat /opt/dockerfile/centos6.9_ssh/dockerfile

FROM centos:6.9 依赖的基础镜像

RUN yum install openssh-server -y RUN 执行的命令

RUN /etc/init.d/sshd start && echo "12345" | passwd --stdin root

CMD ["/usr/sbin/sshd","-D"] 启动后运行 卡住的命令 传参 用 , " 参数"

?

构建镜像

[[email protected] centos6.9_ssh]# docker run -d -p 1033:22 centos6.9_ssh:v1

docker images查看已经存到仓库

启动容器

[[email protected] centos6.9_ssh]# docker run -d -p 1033:22 centos6.9_ssh:v1

进入容器

[[email protected] centos6.9_ssh]# ssh [email protected] -p 1033

?

?

八,构建centos7 ssh httpd镜像

[[email protected] ~]# docker search centos

[[email protected] opt]# mkdir dockerfile

[[email protected] opt]# cd dockerfile

mkdir centos_init

[[email protected] centos7_init]# vim dockerfile

[[email protected] ~]# cat /opt/dockerfile/centos7_init/dockerfile

FROM centos:latest

RUN curl -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

RUN yum -y install iproute

RUN yum install openssh-server -y

RUN yum -y install httpd

RUN yum install vim wget bash-completion lrzsz nmap nc tree htop iftop net-tools -y

RUN echo "12345" | passwd --stdin root

RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key

RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

ADD init.sh /init.sh

CMD ["/bin/bash","/init.sh"]

CMD ["/usr/sbin/init"]

?

[[email protected] centos7_init]# cat init.sh

#!/bin/bash

systemctl start sshd

systemctl start httpd

/usr/sbin/sshd -D

?

?

构建镜像并运行

docker image build -t centos7_ssh_httpd:v1 .

docker run --privileged -d -p 1888:80 -p 1222:22 centos7_ssh_httpd:v1

登陆测试

[[email protected] ~]# ssh [email protected] -p 1222

?

九, 基于 centos7 构建 随机端口

docker image build -t centos_expose:v1 .

docker run --privileged -d -P centos_expose:v1

?

?

十., workdir 工作目录

[[email protected] dok]# cat dockerfile

FROM centos:latest

RUN curl -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

RUN yum -y install iproute openssh-server httpd vim wget bash-completion php php-cli unzip php-mbstring php-gd

RUN echo "12345" | passwd --stdin root

WORKDIR /var/www/html/

RUN curl -O http://static.kodcloud.com/update/download/kodexplorer4.36.zip

RUN unzip kodexplorer4.36.zip

RUN chmod -R 777 /var/www/html/

EXPOSE 22 80

ADD init.sh /init.sh

CMD ["/bin/bash","/init.sh"]

CMD ["/usr/sbin/init"]

?

[[email protected] ~]# cat /opt/dockerfile/dok/init.sh

#!/bin/bash

systemctl start httpd

tail -F /var/log/httpd/access_log

?

构建

docker run --privileged -d -p 80:80 dok:v1

docker exec -it elastic_lehmann /bin/bash

在dockerfile 里所有的路径都是在 workdir 指定的目录下

?

?

十一,构建tomcat

[[email protected] tomcat]# cat dockerfile

FROM docker.io/centos:7.4.1708

?

RUN yum -y install jave-1.8.0-openjdk-devel tomcat tomcat-docs-webapp tomcat-admin-webapps tomcat-webapps &&

????yum clean all

?

EXPOSE 8080/tcp

?

CMD source /etc/sysconfig/tomcat && /usr/libexec/tomcat/server start

?

docker build ./ -t docker.io/tomcat:v2

docker run --name tc2 -d -P -p 8080:8080 docker.io/tomcat:v2

docker exec -it tomcat:v2 /bin/bash

?

?

?

十二, nginx 反代tomcat

?

docker run --name tc1 -d -P -p 8080:8080 docker.io/tomcat:v2

docker inspect tc1

"NetworkID": "75c1e3cc4c51a55c63d49c092e37e504d2163a9dd489553d8bb12c591a97098f",

"EndpointID": "12cce43d64c7cdcfe952c51984f42b0f4a42d9a95567a147175ed752e97f6d75",

"Gateway": "172.17.0.1",

"IPAddress": "172.17.0.2",

"IPPrefixLen": 16,

"IPv6Gateway": "",

"GlobalIPv6Address": "",

"GlobalIPv6PrefixLen": 0,

"MacAddress": "02:42:ac:11:00:02"

}

}

}

}

]

?

?

?

[[email protected] snginx]# cat dockerfile

FROM docker.io/centos:7.4.1708

RUN curl -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

ADD tc1.conf /etc/nginx/conf.d/

ADD nginx.repo /etc/yum.repos.d/

ENV path=/bin:/sbin:/usr/bin:/usr/sbin

RUN yum -y install nginx net-tools

EXPOSE 80/tcp

ADD init.sh /init.sh

CMD ["/bin/bash","/init.sh"]

CMD ["/usr/sbin/init"]

?

?

cat tc1.conf

server {

????listen 80;

????server_name ng.ng.com;

????location / {

????????proxy_pass http://tc1.db01.com:8080/;

????}

}

docker build ./ -t docker.io/nginx:v3

docker run --name nt1 -P -p 80:80 -d --add-host ng.ng.com:172.17.0.2 docker.io/nginx:v3

?

测试:

[[email protected] ~]# curl -I http://ng.ng.com:8080

HTTP/1.1 200 OK

Server: Apache-Coyote/1.1

Content-Type: text/html;charset=ISO-8859-1

Transfer-Encoding: chunked

Date: Sun, 24 Mar 2019 15:25:33 GMT

?

十三, docker卷管理

?

docker volume ls????查看卷

docker volume create ????创建卷

docker inspect nginx-vol 查看卷详情

用卷创建容器

docker run -d -it -p 88:80 --name=nginx-test --mount src=nginx-vol dst=/usr/share/nginx/html nginx1

docker run -d -it --name=nginx-test --mount src=nginx-vol dst=/usr/share/nginx/html nginx

nginx 容器中的/usr/share/nginx/html的内容在 宿主机docker /var/lib/docker/volome/_data里 容器删了之后,文件保存在_date里

?

删除卷

docker volume rm nginx-vol

注意: 如果没有指定卷,会自动创建,建议使用--mount 创建

示例

创建1个nginx卷,用与挂载nginx

[[email protected] centos7]# docker volume create nginx-vol

nginx-vol

[[email protected] centos7]# docker volume ls

DRIVER VOLUME NAME

local nginx-vol

?

[[email protected] centos7]# docker volume inspect nginx-vol

[

{

"CreatedAt": "2019-04-05T16:27:47+08:00",????????#创建时间

"Driver": "local",

"Labels": {},

"Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data", #挂载点

"Name": "nginx-vol",

"Options": {},

"Scope": "local"

}

]

?

启动容器

docker run -d -it -p 88:80 --name=nginx-test --mount src=nginx-vol dst=/usr/share/nginx/html nginx

?

--mount src=创建的卷(持久化与宿主机目录关联)

查看nginx-vol目录有nginx安装时的首页文件等.....

[[email protected] snginx]# ll /var/lib/docker/volumes/nginx-vol/_data/

total 12

-rw-r--r-- 1 root root 494 Mar 26 22:04 50x.html

-rw-r--r-- 1 root root 19 Apr 5 17:27 aaa.html

-rw-r--r-- 1 root root 612 Mar 26 22:04 index.html

?

十四 ,管理应用程序数据 --mount type=bind

docker run --name=nginx-test -d -it -p 88:80 --mount type=bind,src=/app/www,dst=/usr/share/nginx/html nginx:latest

src指定的目录要事先存在,

dst指定的目录里面的内容会隐藏掉,适用于war ,jar包挂载 nfs共享等,直接挂载宿主机目录

验证绑定

docker inspect nginx-test

注意:

1.如果src指定的源目录不存在,会报错,

2.如果dest目录非空,目录里的东西会被隐藏????

?

上传docker镜像到 docker hub

?

宿主机创建挂载目录

mkdir /app/wwwroot -p

unzip wordpress.unzip 解压到 /app/wwwroot

?

创建nginx容器

docker run --name=nginx-test -d -it -p 88:80 --mount type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html nginx

十五,官方进行lnmp 运行wrodpress

创建网络lnmp

docker network lnmp

创建myslq容器

docker run -itd

--name lnmp_mysql

--net lnmp

-p 3306:3306

--mount src=mysql-vol,dst=/var/lib/mysql

-e MYSQL_ROOT_PASSWORD=123456

mysql --character-set-server=utf8

?

ALTER USER ‘root‘@‘%‘ IDENTIFIED WITH mysql_native_password BY ‘123456‘;

flush privileges;

?

创建数据库

docker exec lnmp_mysql sh

-c ‘exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "create database wp"‘

?

[[email protected] ~]# mysql -uroot -p123456 -h 10.0.0.51

?

?

docker logs lnmp_mysql

docker top lnmp_mysql

?

创建php容器

docker run -itd

--name lnmp_web

--net lnmp -p 88:80

--mount type=bind,src=/app/wwwroot,dst=/var/www/html richarvey/nginx-php-fpm

?

浏览器10.0.0.51:88/wordpress 就可以访问wrodpress了

?

同一自定义网络里可用容器名通信

常用的数据,使用数据卷,因为常访问的数据放在容器里是经过一层驱动.性能会下降, 是docker优化的一部分

?

????

?

十六 ,自定义docker 运行wordpress

php dockerfile

[[email protected] php]# cat Dockerfile

FROM centos7.4_ssh_base:v1

#RUN yun install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel

RUN yum install -y zlib libjpeg freetype libpng gd curl zlib-devel

libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel

curl-devel libjpeg-turbo-devel libcurl-devel libxslt-devel

libmcrypt-devel mhash mcrypt perl openssl-devel libacl-devel install gcc gcc-c++

systemtap-sdt-devel libzip-devel systemtap-sdt-devel make && yum clean all && rm -fr /var/cache/yum/*

?

ADD php-7.2.8.tar.gz /tmp/

RUN cd /tmp/php-7.2.8 &&

????./configure --prefix=/usr/local/php

????--enable-calendar

????--enable-sysvsem

????--enable-sysvshm

????--enable-sysvmsg

????--enable-bcmath

????--enable-exif

????--enable-ftp

????--enable-mbstring

????--enable-sockets

????--enable-dtrace

????--enable-soap

????--enable-zip

????--enable-mbregex

????--enable-inline-optimization

????--enable-pcntl

????--enable-pdo

????--enable-opcache

????--enable-xml

????--enable-libzip

????--enable-maintainer-zts

????--enable-fpm

????--enable-mysqlnd

????--with-iconv-dir=/usr/local/libiconv

????--with-gettext

????--with-libxml-dir

????--with-zlib

????--with-kerberos=/usr

????--with-openssl

????--with-mhash

????--with-mysqli=mysqlnd

????--with-pdo-mysql=mysqlnd

????--with-gd

????--with-fpm-acl

????--disable-rpath &&

make && make install &&

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf &&

sed -i "81a daemonize = no" /usr/local/php/etc/php-fpm.conf &&

????cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf &&

sed -i "s/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/g" /usr/local/php/etc/php-fpm.d/www.conf &&

????cp /tmp/php-7.2.8/php.ini-production /usr/local/php/lib/php.ini &&

cp /tmp/php-7.2.8/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && chmod +x /etc/init.d/php-fpm &&

rm -fr /tmp/php-7.2.8* && yum clean all

WORKDIR /usr/local/php

EXPOSE 9000

CMD ["./sbin/php-fpm","-c","/usr/local/php/etc/php-fpm.conf"]

#构建

[[email protected] php]docker build -t php7.2.8:v3 .

# 查看

[[email protected] php]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

php7.2.8 v3 a1e5da9f7c61 About an hour ago 788MB

# 运行

docker run -itd

--name lnmp_php

--net lnmp

--mount type=bind,src=/app/wwwroot,dst=/usr/local/nginx/html

php7.2.8:v1

?

?

nginx dockerfile

docker

FROM centos7.4_ssh_base:v1

RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel && yum clean all && rm -fr /var/cache/yum/*

ADD nginx-1.14.2.tar.gz /tmp

RUN cd /tmp/nginx-1.14.2 &&

./configure --prefix=/usr/local/nginx

--with-http_stub_status_module

--with-http_ssl_module &&

make && make install &&

rm -fr /tmp/nginx-1.14.2/*

WORKDIR /usr/local/nginx

EXPOSE 80

CMD ["./sbin/nginx","-g","daemon off;"]

?

构建

docker bulid -t nginx nginx-1.14:v1 .

启动

docker run -itd

--name=lnmp_nginx

--net lnmp -p 188:80

-v /root/dockefile/runtime/nginx/word.conf:/usr/local/nginx/conf/nginx.conf

--mount type=bind,src=/app/wwwroot,dst=/usr/local/nginx/html

nginx-1.14:v1

浏览器测试

http://10.0.0.51:188/

?

mysql dockerfile

[[email protected] mysql4]# cat dockerfile

FROM centos7.4_ssh_base:v1

ADD mysql-5.6.40.tar.gz /opt/

RUN yum install -y cmake make gcc gcc-c++ ncurses-devel autoconf && yum clean all && rm -fr /var/cache/yum/*

&& useradd mysql -s /sbin/nologin/ -M && mkdir /application && cd /opt/mysql-5.6.40/ && cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.40

????-DMYSQL_DATADIR=/application/mysql-5.6.40/data

????-DMYSQL_UNIX_ADDR=/application/mysql-5.6.40/tmp/mysql.sock

????-DDEFAULT_CHARSET=utf8

????-DDEFAULT_COLLATION=utf8_general_ci

????-DWITH_EXTRA_CHARSETS=all

????-DWITH_INNOBASE_STORAGE_ENGINE=1

????-DWITH_FEDERATED_STORAGE_ENGINE=1

????-DWITH_BLACKHOLE_STORAGE_ENGINE=1

????-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1

????-DWITH_ZLIB=bundled

????-DWITH_SSL=bundled

????-DENABLED_LOCAL_INFILE=1

????-DWITH_EMBEDDED_SERVER=1

????-DENABLE_DOWNLOADS=1

????-DWITH_DEBUG=0 &&

???? make && make install &&

rm -fr /opt/* &&

????ln -s /application/mysql-5.6.40/ /application/mysql &&

????cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld && chmod +x /etc/init.d/mysqld &&

????mkdir /application/mysql-5.6.40/tmp &&

????chown -R mysql.mysql /application/mysql-5.6.40/ &&

????chown -R mysql.mysql /application/mysql &&

????cd /application/mysql/scripts && ./mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data

?

COPY docker-entrypoint.sh /usr/local/bin/

ENV PATH=/application/mysql/bin:$PATH

RUN ln -s /usr/local/bin/docker-entrypoint.sh /entrypoint.sh

ENTRYPOINT ["docker-entrypoint.sh"]

CMD ["/application/mysql/bin/mysqld_safe"]

?

构建镜像

docker build -t mysql:v2 .

启动

docker run -itd --name lnmp_mysql --net lnmp -p 3306:3306 --mount src=mysql-vol,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql-5.6.40:v2

docker exec -it lnmp_mysql bash

mysql

mysql> create database wp;

mysql> grant all on *.* to [email protected]‘%‘ identified by ‘123456‘;

mysql> flush privileges;

?

?

?

?

/** WordPress数据库的名称 */

define(‘DB_NAME‘, ‘wp‘);

/** MySQL数据库用户名 */

define(‘DB_USER‘, ‘root‘);

/** MySQL数据库密码 */

define(‘DB_PASSWORD‘, ‘123456‘);

/** MySQL主机 */

define(‘DB_HOST‘, ‘lnmp_mysql‘);

/** 创建数据表时默认的文字编码 */

?

?

十七Tomcat

[[email protected] tomcat7]# cat dockerfile

FROM centos7.4_ssh_base:v1

?

ADD jdk-8u151-linux-x64.tar.gz /usr/local/

ADD apache-tomcat-7.0.78.tar.gz /usr/local/

?

ENV JAVA_HOME=/usr/local/jdk1.8.0_151

ENV PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

ENV CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar

ENV CATALINA_BASE=/usr/local/tomcat

ENV PATH=$CATALINA_BASE/bin:$PATH

?

?

RUN ln -s /usr/local/apache-tomcat-7.0.78 /usr/local/tomcat &&

ln -s /usr/local/jdk1.8.0_151 /usr/local/jdk

#COPY context.xml server.xml /usr/local/apache-tomcat-7.0.78/conf/

?

WORKDIR /usr/local/tomcat/lib/

#COPY commons-pool2-2.6.0.jar jedis-2.5.2.jar tomcat-redis-session-manager-2.0.0.jar /usr/local/tomcat/lib/

?

EXPOSE???? 8080

WORKDIR /usr/local/tomcat

CMD ["./bin/catalina.sh","run"]

?

?

创建 src 目录用户挂载tomcat站点,与 容器/usr/local/apache-tomcat-7.0.78/webapps 关联

mkdir /app/webapps/test -p

[[email protected] ~]# cat /app/webapps/test/index.jsp

<html>

<head><title>TomcatA</title></head>

<body>

<h1><font color="blue">TomcatA.linuxinfo.top</font></h1>

<tablealign="centre" border="1">

<tr>

<td>SessionID</td>

<% session.setAttribute("linuxinfo.top","linuxinfo.top");%>

<td><%=session.getId() %></td>

</tr>

<tr>

<td>Createdon</td>

<td><%=session.getCreationTime() %></td>

</tr>

</table>

</body>

</html>

?

?

构建

[[email protected] tomcat7]# docker build -t tomcat7:v1 .

运行

docker run -itd

--name=tomcat

-p 8080:8080

--mount type=bind,src=/app/webapps/,dst=/usr/local/apache-tomcat-7.0.78/webapps

tomcat7:v1

?

浏览器访问测试

http://10.0.0.51:8080/test/

?

?

Tomcat-redis-session

?

[[email protected] tomcat-redis]# cat dockerfile

FROM centos7.4_ssh_base:v1

?

ADD pak1/jdk-8u151-linux-x64.tar.gz /usr/local/

ADD pak1/apache-tomcat-7.0.78.tar.gz /usr/local/

?

ENV JAVA_HOME=/usr/local/jdk1.8.0_151

ENV PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

ENV CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar

ENV CATALINA_BASE=/usr/local/tomcat

ENV PATH=$CATALINA_BASE/bin:$PATH

?

RUN ln -s /usr/local/apache-tomcat-7.0.78 /usr/local/tomcat &&

ln -s /usr/local/jdk1.8.0_151 /usr/local/jdk

COPY context.xml server.xml /usr/local/apache-tomcat-7.0.78/conf/

COPY pak1 /usr/local/tomcat/lib/

?

WORKDIR /usr/local/tomcat/lib/

EXPOSE???? 8080

WORKDIR /usr/local/tomcat

CMD ["./bin/catalina.sh","run"]

?

?

[[email protected] ~]# cat /app/webapps/test/index.jsp

<html>

<head><title>TomcatA</title></head>

<body>

<h1><font color="blue">TomcatA.linuxinfo.top</font></h1>

<tablealign="centre" border="1">

<tr>

<td>SessionID</td>

<% session.setAttribute("linuxinfo.top","linuxinfo.top");%>

<td><%=session.getId() %></td>

</tr>

<tr>

<td>Createdon</td>

<td><%=session.getCreationTime() %></td>

</tr>

</table>

</body>

</html>

?

[[email protected] ~]# cat /app/webappsB/test/index.jsp

<html>

<head><title>TomcatB</title></head>

<body>

<h1><font color="red">TomcatB.linuxinfo.top</font></h1>

<tablealign="centre" border="1">

<tr>

<td>SessionID</td>

<% session.setAttribute("linuxinfo.top","linuxinfo.top");%>

<td><%=session.getId() %></td>

</tr>

<tr>

<td>Createdon</td>

<td><%=session.getCreationTime() %></td>

</tr>

</table>

</body>

</html>

?

[[email protected] tomcat-redis]# docker run -itd --name=tomcat -p 8080:8080 --mount type=bind,src=/app/webapps/,dst=/usr/local/apache-tomcat-7.0.78/webapps tomcat-redis:v1

[[email protected] tomcat-redis]# docker run -itd --name=tomcatB -p 8080:8080 --mount type=bind,src=/app/webapps/,dst=/usr/local/apache-tomcat-7.0.78/webapps tomcat-redis:v1

测试:

?

以上是关于docker的主要内容,如果未能解决你的问题,请参考以下文章

DockerDocker常用命令

DockerDocker配置

dockerdocker镜像和镜像发布方法

dockerdocker

dockerdocker安装rabbitmq

dockerdocker下安装mysql