docker构建镜像 发布镜像

x33g5p2x  于2022-02-14 转载在 Docker  
字(5.3k)|赞(0)|评价(0)|浏览(693)

Dockerfile命令大全

# 引用
FROM centos
# 作者信息
MAINTAINER vitcloud<vitcluod@163.com>

# 运行环境路径
ENV MYPATH /usr/local
WORKDIR $MYPATH

# java软件环境
# 配置java环境目录
ENV JAVA_HOME /usr/local/jdk1.8.0_191
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
# 配置tomcat环境目录
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.58
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.58
# path路径
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

# 安装第三方
RUN yum -y install vim			# vim
RUN yum -y install net-tools	# 网络

# python
RUN pip install flsk

# 暴露端口
EXPOSE 80

# 输出
CMD echo "------end------"

# 执行指令
CMD /bin/bash			# 只有最后一个会生效,可被替代
ENTRYPOINT ["ls","-a"]	# 可以追加命令

1 构建centos

1.1 Dockerfile

FROM centos
MAINTAINER vitcloud<vitcluod@163.com>

ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim
RUN yum -y install net-tools

# 暴露端口
EXPOSE 80

CMD echo "------end------"
CMD /bin/bash

1.2 生成自己的容器镜像

名字:centos-base(若带版本 centos-base:1.0)
-f: dockerfile地址()

docker build -f ./Dockerfile -t centos-base .
docker build -f ./Dockerfile -t centos-base:1.0 .

1.3 运行(启动容器)

docker run -it centos-base

2 构建java环境(包含tomcat)

2.1 Dockerfile

FROM centos
MAINTAINER vitcloud<vitcloud@163.com>

COPY readme.txt /usr/local/readme.txt

# 添加 软件包
ADD jdk-8u191-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.58.tar.gz /usr/local/

# 安装vim
RUN yum -y install vim

# 配置环境目录
ENV MYPATH /usr/lcoal
WORKDIR $MYPATH

# 配置java环境目录
ENV JAVA_HOME /usr/local/jdk1.8.0_191
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

# 配置tomcat环境目录
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.58
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.58

# path路径
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

# 暴露端口
EXPOSE 8080

# 运行脚本
CMD /usr/local/apache-tomcat-9.0.58/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.58/bin/logs/catalina.out

2.2 构建镜像

docker build -t vitcloud-tomcat .

2.3 启动容器

docker run -d -p 9090:8080 \
-v /usr/local/docker-build/tomcat/test:/usr/local/apache-tomcat-9.0.58/webapps/test \
-v /home/vitcloud-docker-build/tomcatlogs/:/usr/local/apache-tomcat-9.0.58/logs \
--name blog-tomcat \
vitcloud-tomcat

交互方式进入

docker exec -it 654c26bc7abb /bin/bash

2.4 发布项目

由于做了卷挂载,我们字节在本地编写项目就可以发布了

<web-app version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID">
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>云间来客</title>
</head>
<body>
    <div>测试日志</div>
</body>
</html>

3 centos7-python38

  • centos7.6.1810
  • python 3.8.5

3.1 Dockerfile

https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz

FROM centos:7.6.1810
MAINTAINER vitcloud<vitcloud@163.com>
RUN set -ex \
    # 预安装所需组件
    && yum install -y wget tar libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make initscripts \
    && wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz \
    && tar -zxvf Python-3.8.5.tgz \
    && cd Python-3.8.5 \
    && ./configure prefix=/usr/local/python3 \
    && make \
    && make install \
    && make clean \
    && rm -rf /Python-3.8.5* \
    && yum install -y epel-release \
    && yum install -y python-pip
# 设置默认为python3
RUN set -ex \
    # 备份旧版本python
    && mv /usr/bin/python /usr/bin/python27 \
    && mv /usr/bin/pip /usr/bin/pip27 \
    # 配置默认为python3
    && ln -s /usr/local/python3/bin/python3.8 /usr/bin/python \
    && ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
# 修复因修改python版本导致yum失效问题
RUN set -ex \
    && sed -i "s#/usr/bin/python#/usr/bin/python2.7#" /usr/bin/yum \
    && sed -i "s#/usr/bin/python#/usr/bin/python2.7#" /usr/libexec/urlgrabber-ext-down \
    && yum install -y deltarpm
# 基础环境配置
RUN set -ex \
    # 修改系统时区为东八区
    && rm -rf /etc/localtime \
    && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && yum install -y vim \
    # 安装定时任务组件
    && yum -y install cronie
# 支持中文
RUN yum install kde-l10n-Chinese -y
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
# 更新pip版本
RUN pip install --upgrade pip
ENV LC_ALL zh_CN.UTF-8

3.2 构建

docker build -t centos7-python38 .

3.3 容器启动

docker run -itd \
--restart always --privileged=true \
-v /root/dockers/python:/root/python \
-v /root/dockers/python/cron:/var/spool/cron /usr/sbin/init
python:37
--name python

9 commit、push、save

9.1 将容器提交镜像 commit

docker commit 提交同期成为一个副本

# 命令和git原理类似
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名:[TAG]

案例

docker commit -a="有勇气的牛排" -m="add webapps app" 48629b5a69d2 tomcat02:1.0

9.2 发布镜像 push

DockerHub
地址:https://hub.docker.com/

# 帮助
docker login --help

# 登录
docker login -u 用户名

# 推动自己的镜像到 DockerHub
docker push vitcloud/tomcat:1.0

给镜像添加tag

docker tag d89b35c35d33 vitcloud/tomcat:1.0

发布到阿里云镜像服务

创建命名空间
创建容器镜像服务
创建命名空间
创建容器镜像
浏览阿里云

# 登录阿里云
docker login --username=1809296387@qq.com registry.cn-zhangjiakou.aliyuncs.com

# 给镜像生成版本号
docker tag [ImageId] registry.cn-zhangjiakou.aliyuncs.com/warm-test/charles-test:[镜像版本号]

# 推送
docker push registry.cn-zhangjiakou.aliyuncs.com/warm-test/diytomcat:1.0

docker push vitcloud/tomcat:1.0

docker push registry.cn-zhangjiakou.aliyuncs.com/warm-test/charles-test:[镜像版本号]

9.3 save

将镜像保存到本地

docker save -o centos7-python38_2.0.tar centos7-python38:2.0

加载本地镜像

docker load -i centos7-python38_2.0.tar

相关文章