新建基础镜像,希望能够SSH,安装java,用户,声明22端口等等;拷贝基础hadoop安装文件
新建Dockerfile
FROM centos:7.9.2009
RUN rm -f /etc/localtime && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
RUN export LANG=zh_CN.UTF-8
# 创建用户和用户组,跟yaml编排里的user: 10000:10000
RUN groupadd --system --gid=10000 hadoop && useradd --system --home-dir /home/hadoop --uid=10000 --gid=hadoop hadoop
# 安装sudo
RUN yum -y install sudo ; chmod 640 /etc/sudoers
# 给hadoop添加sudo权限
RUN echo "hadoop ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
#安装 sshd
RUN yum install -y openssh-server openssh-clients vim net-tools lrzsz telnet wget nc
RUN sed -i '/^HostKey/'d /etc/ssh/sshd_config
RUN echo 'HostKey /etc/ssh/ssh_host_rsa_key'>>/etc/ssh/sshd_config
#生成ssh-keye
RUN ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key
#更改root用户登录密码为
RUN echo 'root:123805' | chpasswd
#声明22端口
EXPOSE 22
#容器运行时启动sshd
RUN mkdir -p /opt
RUN echo '#!/bin/bash'>> /opt/run.sh
RUN echo '/usr/sbin/sshd -D'>>/opt/run.sh
RUN chmod +x /opt/run.sh
CMD ["/opt/run.sh"]
RUN mkdir /opt/apache/
# 安装 JDK
ADD jdk-8u333-linux-x64.tar.gz /opt/apache/
ENV JAVA_HOME /opt/apache/jdk1.8.0_333
ENV PATH ${JAVA_HOME}/bin:$PATH
# 配置 Hadoop
ENV HADOOP_VERSION 2.7.7
ADD hadoop-${HADOOP_VERSION}.tar.gz /opt/apache/
ENV HADOOP_HOME /opt/apache/hadoop
RUN ln -s /opt/apache/hadoop-${HADOOP_VERSION} $HADOOP_HOME
ENV HADOOP_COMMON_HOME=${HADOOP_HOME} \
HADOOP_HDFS_HOME=${HADOOP_HOME} \
HADOOP_MAPRED_HOME=${HADOOP_HOME} \
HADOOP_YARN_HOME=${HADOOP_HOME} \
HADOOP_CONF_DIR=${HADOOP_HOME}/etc/hadoop \
PATH=${PATH}:${HADOOP_HOME}/bin
# 创建namenode、datanode存储目录
RUN mkdir /opt/apache/hadoop/tmp
RUN mkdir /opt/apache/hadoop/dfs
RUN mkdir /opt/apache/hadoop/dfs/name
RUN mkdir /opt/apache/hadoop/dfs/node
RUN mkdir /opt/apache/hadoop/dfs/data
COPY config/hadoop-config/* ${HADOOP_HOME}/etc/hadoop/
RUN chown -R hadoop:hadoop /opt/apache
ENV ll "ls -l"
WORKDIR /opt/apache
容器停止 docker stop XXX 容器移除 docker rm XXX 查看镜像 docker images 删除镜像 docker rmi hadoop-base:v1 构建镜像 docker build -t hadoop-base:v1 ./ 根据镜像,构建容器 docker run -itd --name test -p 222:22 hadoop-base:v1 查看容器 docker ps -a 进入docekr容器内部 docker exec -it 6304e8f62f08 bash docker exec -it 6304e8f62f08 /bin/bash
输入ip addr 查看网络
Docker 提供三种 user-defined 网络驱动:bridge
, overlay
和 macvlan
overlay 和 macvlan 用于创建跨主机的网络
输入创建桥接命令
docker network create -d macvlan --subnet=192.168.0.254/24 --gateway=192.168.0.1 -o parent=enp0s31f6 bridge-host
命令解释:
192.168.0.254/24 (网段,/24是掩码,等于255.255.255.0)
192.168.0.1 (网关IP,一般是路由器的LAN口IP)
enp0s31f6 (网卡名称)
bridge-host (接口名称,可自定义)
配置 docker run -itd --network=bridge-host --ip=192.168.0.216 --dns=114.114.114.114 --name hadoop216 -h hadoop216 --restart=always hadoop-base:v1docker run -itd --network=bridge-host --ip=192.168.0.217 --dns=114.114.114.114 --name hadoop217 -h hadoop217 --restart=always hadoop-base:v1docker run -itd --network=bridge-host --ip=192.168.0.218 --dns=114.114.114.114 --name hadoop218 -h hadoop218 --restart=always hadoop-base:v1
docker run -itd --network=bridge-host --ip=192.168.0.217 --dns=114.114.114.114 --name hadoop217 -h hadoop217 --restart=always hadoop-base:v1
docker run -itd \ #创建容器 参数为itd (含义请百度Docker run 命令)
--name hadoop216 \ #容器名称,可以自定义
--network=bridge-host \ #接口名称,上面创建桥接时写的什么这里就改成什么
--ip=192.168.0.216 \ #容器的IP地址,建议在路由器的DHCP地址池范围外,避免造成冲突
--dns=114.114.114.114 \ #DNS服务器地址,可自己更换其他的
--restart=always \ #自动重新启动=开启
hadoop-base:v1 镜像名称
测试访问正常