创建并且生成镜像
在当前目录下创建一个名为Dockerfile
文件
vi Dockerfile
填入下面配置
# 使用 CentOS 作为基础镜像
FROM centos:7# 设置工作目录
WORKDIR /app# 复制项目文件到容器中
COPY bin/ /app/bin/
COPY config/ /app/config/
COPY lib/ /app/lib/
COPY plugin/ /app/plugin/
COPY resource/ /app/resource/
COPY wall.jar /app/wall.jar
COPY startup.sh /app/startup.sh
COPY stop.sh /app/stop.sh# 设置 start.sh 脚本为可执行
RUN chmod +x /app/startup.sh# 设置容器启动时执行的命令
CMD ["./startup.sh"]
注意,第一行 FROM centos:7
为官方基础镜像,可以为mysql,也可以为jdk
构建镜像
docker build -t 镜像名字 .
注意后面那个.
这个标识当前根目录下的,如果是其他目录则是./test
查看镜像
docker imeages
运行该镜像
docker run wall
在没有启动镜像时临时进入镜像
找到需要进入的镜像的ID或名称,然后使用以下命令创建一个临时容器,并进入该容器:
docker run -i --rm 镜像名称或ID /bin/bash
没有启动镜像时修改镜像内容
进入镜像
docker run -it --entrypoint /bin/bash <image_name>
提交镜像信息
docker commit <container_id_or_name> <new_image_name>
创建一个基于centos和mysql和jdk1.8的Dockerfile镜像
# 使用 CentOS 作为基础镜像
FROM centos:7# 设置维护者信息
LABEL maintainer="yourname@example.com"# 删除原有的 CentOS-Base.repo 并添加新的阿里云镜像源
RUN rm -f /etc/yum.repos.d/CentOS-Base.repo && \echo "[base]" > /etc/yum.repos.d/CentOS-Base.repo && \echo "name=CentOS-\$releasever - Base" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "baseurl=http://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "gpgcheck=1" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "[updates]" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "name=CentOS-\$releasever - Updates" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "baseurl=http://mirrors.aliyun.com/centos/\$releasever/updates/\$basearch/" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "gpgcheck=1" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "[extras]" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "name=CentOS-\$releasever - Extras" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "baseurl=http://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "gpgcheck=1" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "[centosplus]" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "name=CentOS-\$releasever - Plus" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "baseurl=http://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "gpgcheck=1" >> /etc/yum.repos.d/CentOS-Base.repo && \echo "gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7" >> /etc/yum.repos.d/CentOS-Base.repo# 清理 YUM 缓存并更新缓存
RUN yum clean all && \yum makecache# 更新并安装必要的软件包
# RUN yum -y update && \
# yum -y install wget && \
# yum -y install gcc && \
# yum -y install libaio && \
# yum -y install epel-release && \
# yum -y install java-1.8.0-openjdk && \
# yum -y install mysql57-server# 更新系统
RUN yum -y update# 安装 wget
RUN yum -y install wget# 安装 gcc
RUN yum -y install gcc# 安装 libaio
RUN yum -y install libaio# 安装 epel-release
RUN yum -y install epel-release# 安装 Java 1.8
RUN yum -y install java-1.8.0-openjdk# 安装 MySQL 5.7 官方仓库
# RUN wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm && \
# rpm -ivh mysql57-community-release-el7-11.noarch.rpm# 导入 MySQL GPG 公钥
# RUN rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql# 安装 MySQL 5.7
# RUN yum -y install mysql-community-server# 安装 MariaDB
RUN yum -y install mariadb-server# 设置 JAVA_HOME 环境变量
ENV JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
ENV PATH=$JAVA_HOME/bin:$PATH# 创建目录并设置工作目录
WORKDIR /data# 暴露 MySQL 默认端口 3306
EXPOSE 3306# 启动 MySQL 服务
CMD service mariadb start && \tail -f /dev/null