docker下载地址:
https://get.docker.com/builds/Linux/x86_64/docker-1.13.1.tgz
docker的rpm包下载网址:
http://yum.dockerproject.org/repo/main/centos/7/Packages/
##docker中文网址:
http://www.docker.org.cn/thread/126.html
##docker官网文档库:
https://docs.docker.com/engine/installation/linux/centos/
##dockerfile官网 命令解释:
https://docs.docker.com/engine/reference/builder/#cmd
##docker-compose官网 命令解释:
https://docs.docker.com/compose/compose-file/
##docker官网 docker run命令解释:
https://docs.docker.com/engine/reference/run/
##docker官网: 使用Docker容器启动应用程序
http://hadoop.apache.org/docs/r3.1.0/hadoop-yarn/hadoop-yarn-site/DockerContainers.html
##docker官方镜像仓库
https://hub.docker.com/search/
centos镜像下载地址:
https://hub.docker.com/_/centos
docker部署与基础镜像准备
1.docker安装
方法1:yum方式安装
-
lsb_release -a
##查看系统版本
-
uname -r
##查看内核版本
-
yum list installed | grep docker
##查看是否已安装docker列表
-
yum install -y docker
##yum命令安装
-
docker -v
##查看docker版本
-
systemctl start docker
##启动docker
-
systemctl status docker
##查看docker状态
-
systemctl
enable docker
##设置开机自启
-
docker --
help
##查看docker命令帮助
方法2:tar包方式安装
##docker下载地址:
https://get.docker.com/builds/Linux/x86_64/docker-1.13.1.tgz
##解压docker
tar --strip-components=1 -zxvf docker-1.13.1.tgz -C /usr/local/bin
##查看docker版本
docker -v
##设置docker开机启动
vi /usr/lib/systemd/system/docker.service ##添加如下内容:
-
[Unit]
-
Description=Docker Application Container Engine
-
Documentation=https://docs.docker.com
-
After=network.target
-
-
[Service]
-
Type=notify
-
ExecStart=/usr/
local/bin/dockerd
-
ExecReload=/bin/
kill -s HUP
$MAINPID
-
LimitNOFILE=infinity
-
LimitNPROC=infinity
-
LimitCORE=infinity
-
TimeoutStartSec=0
-
Delegate=yes
-
KillMode=process
-
-
[Install]
-
WantedBy=multi-user.target
##启动docker
-
systemctl start docker.service
##启动docker
-
systemctl
enable docker.service
##设置开机启动
-
systemctl status docker.service
##查看docker运行情况
-
docker --
help
##查看命令帮助
2.下载centos镜像
docker search centos ##搜索官方仓库的centos镜像(需要访问外网)
-
docker pull centos
##拉取官方仓库最新版本的centos
-
docker images
##查看本地镜像
##启动本地镜像,生成容器并查看centos版本
-
docker run -it 300e315adb2f /bin/bash
-
cat /etc/redhat-release
##下载指定版本的centos
docker pull centos:centos7.9.2009
3.制作基础镜像
##首先使用tag命令打标签对镜像重命令
-
docker tag 8652b9f0cb4c centos7.9:v1
-
docker rmi -f centos:centos7.9.2009
-
docker images
方法1:dockerfile方式
##编辑Dockerfile文件
vi Dockerfile
-
FROM centos7.9:v1
-
-
MAINTAINER bonc
-
-
USER root
-
-
# install dev tools
-
RUN yum install -y autoconf make automake wget httpd-tools zip vim
-
RUN yum install -y curl tar unzip
which sudo initscripts openssh-server openssh-clients rsync
-
RUN yum install -y gcc-c++
-
RUN yum install -y pcre pcre-devel
-
RUN yum install -y zlib zlib-devel
-
RUN yum install -y openssl openssl-devel
-
-
# clock sync
-
RUN rm -f /etc/localtime
-
RUN ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
-
-
# passwordless ssh
-
RUN
echo
"root"| passwd --stdin root
-
RUN ssh-keygen -q -N
"" -t dsa -f /etc/ssh/ssh_host_dsa_key
-
RUN ssh-keygen -q -N
"" -t rsa -f /etc/ssh/ssh_host_rsa_key
-
RUN ssh-keygen -q -N
"" -t rsa -f /etc/ssh/ssh_host_ecdsa_key
-
RUN ssh-keygen -q -N
"" -t rsa -f /etc/ssh/ssh_host_ed25519_key
-
RUN ssh-keygen -q -N
"" -t rsa -f /root/.ssh/id_rsa
-
RUN cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
-
-
WORKDIR /root
-
EXPOSE 22
-
USER root
##构建基础镜像
-
docker build --
help
##查看docker build命令提示
-
docker build -t centos7.9:20210106 -f Dockerfile .
##使用Dockerfile生成镜像
-
docker images
##查看刚生成的镜像
方法2:docker commit方式
##启动基础镜像,直接在生成的容器里面进行操作
-
docker run --name httpd001 -it centos7.9:20210106 /bin/bash
-
yum install -y httpd
##容器里面yum安装httpd
echo "2021 hello world" >/var/www/html/index.html
##退出容器并使用commit保存容器的状态为新镜像httpd001:v1
-
exit
-
docker commit e18d811e46b8 httpd001:v1
##启动commit方式生成的镜像httpd001:v1,并启动httpd服务
-
docker run --name httpd001 -it -p 10080:80 httpd001:v1 /bin/bash
-
/usr/sbin/httpd -DFOREGROUND
##web查看
http://192.168.30.1:10080/
4.Dockerfile部署tomcat演示
##准备相关文件
其中,
beh_env 为环境变量文件
my.repo 为本地yum源
jdk,tomcat 为原始解压包后重命名
start_tomcat.sh 为tomact启动脚本
Dockerfile_tomcat 为生成tomcat镜像的Dockerfile文件
cat start_tomcat.sh
-
#!/bin/bash
-
source /etc/profile
-
/usr/
local/tomcat/bin/startup.sh
-
ps -ef | grep -v
$0 | grep -v grep > /dev/null
-
while [[ $? -eq 0 ]]
-
do
-
sleep 20
-
ps -ef | grep -v
$0 | grep -v grep > /dev/null
-
done
cat beh_env
-
export JAVA_HOME=/usr/
local/jdk
-
export CLASSPATH=
$JAVA_HOME/lib:
$JAVA_HOME/jre/lib:
$CLASSPATH
-
export TOMCAT_HOME=/usr/
local/tomcat
-
export PATH=
$JAVA_HOME/bin:
$TOMCAT_HOME/bin:
$PATH
cat Dockerfile_tomcat
-
FROM centos7.9:20210106
-
-
MAINTAINER bonc
-
-
USER root
-
-
# install dev tools
-
RUN rm -rf /etc/yum.repos.d/*
-
ADD my.repo /etc/yum.repos.d
-
RUN yum install -y ftp lrzsz
-
-
-
# Set password
-
RUN
echo
"root"| passwd --stdin root
-
-
# Add files
-
ADD jdk /usr/
local/jdk
-
ADD tomcat /usr/
local/tomcat
-
ADD beh_env /usr/
local/beh_env
-
ADD start_tomcat.sh /usr/
local/start_tomcat.sh
-
-
#Set env
-
RUN
echo
"source /usr/local/beh_env" >> /etc/profile
-
-
WORKDIR /usr/
local/tomcat/bin
-
EXPOSE 8080
-
-
CMD /usr/
local/start_tomcat.sh
##创建并查看生成的tomcat镜像
-
docker build -t tomcat:v1 -f Dockerfile_tomcat .
-
docker images
##启动tomcat容器
-
docker run --name tomcat18080 -itd -p 18080:8080 tomcat:v1
-
docker ps -a
##查看当前的所有容器
##web访问验证:
5.save load演示
##使用save命令将docker01节点上的镜像打包成文件
docker save tomcat:v1 > tomcat.tar
##将打包文件传输到docker02节点;
##使用load命令将docker02节点的打包文件加载为镜像
docker load < tomcat.tar
转载:https://blog.csdn.net/qq_36546699/article/details/116310391