DockerFile案例

  • A+
所属分类:linux技术
摘要

1、在Centos7宿主机上启用ip转发功能2、编写DockerFile文件:vim Dockerfile3、构建mycentos镜像(不要忘记最后的.)


DockerFile案例

案例1:自定义镜像mycentos

1、在Centos7宿主机上启用ip转发功能

# Avoid WARNING: IPv4 forwarding is disabled. Networking will not work. echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf systemctl restart network 

2、编写DockerFile文件:vim Dockerfile

# 查询centos镜像版本:https://hub.docker.com/ FROM centos:7 # author<email> MAINTAINER auth<auth@163.com>  ENV MYPATH /usr/local WORKDIR $MYPATH  RUN yum -y install vim RUN yum -y install net-tools  # 暴露端口 EXPOSE 80  CMD echo $MYPATH CMD /bin/bash 

3、构建mycentos镜像(不要忘记最后的.)

docker build -f Dockerfile -t mycentos:1.0 . 

4、通过mycentos镜像生成容器,并且更改容器的yum源

# 创建并运行容器 docker run -it --name mycentos01 mycentos:1.0  # yum repo (163) mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo yum clean all && yum makecache  # ping test ping www.baidu.com 

案例2:带有JDK的Centos7镜像

1、下载JDK到宿主机工作目录(如jdk-8u281-linux-x64.tar.gz)

  • 在linux中安装jdk的教程(若不需要在宿主机上安装jdk,可以跳过)

  • Java官方文档:https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html

    1. Change directory to the location where you would like the JDK to be installed, then move the .tar.gz archive file to the current directory.

    2. Unpack the archive file and install the JDK. ( tar zxvf jdk-8uversion-linux-x64.tar.gz )

      tar zxvf jdk-8u281-linux-x64.tar.gz

    3. The JDK files are installed in a directory called jdk1.8.0_version in the current directory.

      jdk1.8.0_281

    4. Delete the .tar.gz file if you want to save disk space.

2、编写DockerFile文件:vim Dockerfile1

FROM centos:7 MAINTAINER auth<auth@163.com>  ENV MYPATH /usr/local WORKDIR $MYPATH  RUN yum -y install vim RUN yum -y install net-tools  # COPY:将宿主机中指定的文件复制到docker容器中指定的路径  ADD:复制(若是压缩文件会先解压) ADD jdk-8u281-linux-x64.tar.gz /usr/local ENV JAVA_HOME /usr/local/jdk1.8.0_281 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV PATH $PATH:$JAVA_HOME/bin  EXPOSE 80  CMD echo $MYPATH CMD /bin/bash 

3、构建 jdk+centos7 镜像(不要忘记最后的.)

docker build -f Dockerfile1 -t mycentos:2.0 .