Dockerfile文件详细教程

这篇具有很好参考价值的文章主要介绍了Dockerfile文件详细教程。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

写在前面

Dockerfile是用来构建镜像的,他实际上就是把在linux下的命令操作写到了Dockerfile中,通过Dockerfile去执行设置好的操作命令,保证通过Dockerfile的构建镜像是一致的。

实战分析

该例子来自于 chromium 项目
主要干的事情:

  1. 来指定用哪个镜像;
  2. 安装一些编译需要的环境:C++编译环境,Java环境等
# This Dockerfile specifies the recipe for creating an image for the tests
# to run in.
#
# We install as many test dependencies here as we can, because these setup
# steps can be cached.  They do *not* run every time we run the build.
# The Docker image is only rebuilt when the Dockerfile (ie. this file)
# changes.

# 指定了使用什么镜像 debian
# Base Dockerfile for gRPC dev images
FROM debian:latest

# 添加软件源
# Apt source for old Python versions.
RUN echo 'deb http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu trusty main' > /etc/apt/sources.list.d/deadsnakes.list && \
  apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DB82666C

# Apt source for Oracle Java.
RUN echo 'deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main' > /etc/apt/sources.list.d/webupd8team-java-trusty.list && \
  apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886 && \
  echo "oracle-java7-installer shared/accepted-oracle-license-v1-1 select true" | debconf-set-selections

# Apt source for Mono
RUN echo "deb http://download.mono-project.com/repo/debian wheezy main" | tee /etc/apt/sources.list.d/mono-xamarin.list && \
  echo "deb http://download.mono-project.com/repo/debian wheezy-libjpeg62-compat main" | tee -a /etc/apt/sources.list.d/mono-xamarin.list && \
  apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

# Apt source for php
RUN echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu trusty main" | tee /etc/apt/sources.list.d/various-php.list && \
  apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F4FCBB07

# Install dotnet SDK based on https://www.microsoft.com/net/core#debian
# (Ubuntu instructions need apt to support https)
RUN apt-get update && apt-get install -y --force-yes curl libunwind8 gettext && \
  curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=847105 &&  \
  mkdir -p /opt/dotnet && tar zxf dotnet.tar.gz -C /opt/dotnet && \
  ln -s /opt/dotnet/dotnet /usr/local/bin

# Install dependencies.  We start with the basic ones require to build protoc
# and the C++ build
RUN apt-get clean && apt-get update && apt-get install -y --force-yes \
  autoconf \
  autotools-dev \
  build-essential \
  bzip2 \
  ccache \
  curl \
  gcc \
  git \
  libc6 \
  libc6-dbg \
  libc6-dev \
  libgtest-dev \
  libtool \
  make \
  parallel \
  time \
  wget \
  # -- For csharp --
  mono-devel \
  referenceassemblies-pcl \
  nunit \
  # -- For all Java builds -- \
  maven \
  # -- For java_jdk6 -- \
  #   oops! not in jessie. too old? openjdk-6-jdk \
  # -- For java_jdk7 -- \
  openjdk-7-jdk \
  # -- For java_oracle7 -- \
  oracle-java7-installer \
  # -- For python / python_cpp -- \
  python-setuptools \
  python-pip \
  python-dev \
  python2.6-dev \
  python3.3-dev \
  python3.4-dev \
  # -- For Ruby --
  ruby \
  # -- For C++ benchmarks --
  cmake \
  # -- For PHP --
  php5.6     \
  php5.6-dev \
  php5.6-xml \
  php7.0     \
  php7.0-dev \
  php7.0-xml \
  phpunit    \
  valgrind   \
  libxml2-dev \
  && apt-get clean

##################
# C# dependencies

RUN wget www.nuget.org/NuGet.exe -O /usr/local/bin/nuget.exe

##################
# Python dependencies

# These packages exist in apt-get, but their versions are too old, so we have
# to get updates from pip.

RUN pip install pip --upgrade
RUN pip install virtualenv tox yattag

##################
# Ruby dependencies

# Install rvm
RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
RUN \curl -sSL https://get.rvm.io | bash -s stable

# Install Ruby 2.1, Ruby 2.2 and JRuby 1.7
RUN /bin/bash -l -c "rvm install ruby-2.1"
RUN /bin/bash -l -c "rvm install ruby-2.2"
RUN /bin/bash -l -c "rvm install jruby-1.7"
RUN /bin/bash -l -c "echo 'gem: --no-ri --no-rdoc' > ~/.gemrc"
RUN /bin/bash -l -c "echo 'export PATH=/usr/local/rvm/bin:$PATH' >> ~/.bashrc"
RUN /bin/bash -l -c "gem install bundler --no-ri --no-rdoc"

##################
# Java dependencies

# This step requires compiling protoc. :(

ENV MAVEN_REPO /var/maven_local_repository
ENV MVN mvn --batch-mode

RUN cd /tmp && \
  git clone https://github.com/google/protobuf.git && \
  cd protobuf && \
  git reset --hard 129a6e2aca95dcfb6c3e717d7b9cca1f104fde39 && \
  ./autogen.sh && \
  ./configure && \
  make -j4 && \
  cd java && \
  $MVN install dependency:go-offline -Dmaven.repo.local=$MAVEN_REPO && \
  cd ../javanano && \
  $MVN install dependency:go-offline -Dmaven.repo.local=$MAVEN_REPO

##################
# PHP dependencies.
RUN wget http://am1.php.net/get/php-5.5.38.tar.bz2/from/this/mirror
RUN mv mirror php-5.5.38.tar.bz2
RUN tar -xvf php-5.5.38.tar.bz2
RUN cd php-5.5.38 && ./configure --enable-maintainer-zts --prefix=/usr/local/php-5.5-zts && \
    make && make install && cd ..
RUN cd php-5.5.38 && make clean && ./configure --enable-bcmath --prefix=/usr/local/php-5.5 && \
    make && make install && cd ..

RUN wget http://am1.php.net/get/php-5.6.30.tar.bz2/from/this/mirror
RUN mv mirror php-5.6.30.tar.bz2
RUN tar -xvf php-5.6.30.tar.bz2
RUN cd php-5.6.30 && ./configure --enable-maintainer-zts --prefix=/usr/local/php-5.6-zts && \
    make && make install && cd ..
RUN cd php-5.6.30 && make clean && ./configure --enable-bcmath --prefix=/usr/local/php-5.6 && \
    make && make install && cd ..

RUN wget http://am1.php.net/get/php-7.0.18.tar.bz2/from/this/mirror
RUN mv mirror php-7.0.18.tar.bz2
RUN tar -xvf php-7.0.18.tar.bz2
RUN cd php-7.0.18 && ./configure --enable-maintainer-zts --prefix=/usr/local/php-7.0-zts && \
    make && make install && cd ..
RUN cd php-7.0.18 && make clean && ./configure --enable-bcmath --prefix=/usr/local/php-7.0 && \
    make && make install && cd ..

RUN wget http://am1.php.net/get/php-7.1.4.tar.bz2/from/this/mirror
RUN mv mirror php-7.1.4.tar.bz2
RUN tar -xvf php-7.1.4.tar.bz2
RUN cd php-7.1.4 && ./configure --enable-maintainer-zts --prefix=/usr/local/php-7.1-zts && \
    make && make install && cd ..
RUN cd php-7.1.4 && make clean && ./configure --enable-bcmath --prefix=/usr/local/php-7.1 && \
    make && make install && cd ..

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php
RUN mv composer.phar /usr/bin/composer
RUN php -r "unlink('composer-setup.php');"
RUN composer config -g -- disable-tls true
RUN composer config -g -- secure-http false
RUN cd /tmp && \
  rm -rf protobuf && \
  git clone https://github.com/google/protobuf.git && \
  cd protobuf && \
  git reset --hard 49b44bff2b6257a119f9c6a342d6151c736586b8 && \
  cd php && \
  ln -sfn /usr/local/php-5.5/bin/php /usr/bin/php && \
  ln -sfn /usr/local/php-5.5/bin/php-config /usr/bin/php-config && \
  ln -sfn /usr/local/php-5.5/bin/phpize /usr/bin/phpize && \
  composer install && \
  mv vendor /usr/local/vendor-5.5 && \
  ln -sfn /usr/local/php-5.6/bin/php /usr/bin/php && \
  ln -sfn /usr/local/php-5.6/bin/php-config /usr/bin/php-config && \
  ln -sfn /usr/local/php-5.6/bin/phpize /usr/bin/phpize && \
  composer install && \
  mv vendor /usr/local/vendor-5.6 && \
  ln -sfn /usr/local/php-7.0/bin/php /usr/bin/php && \
  ln -sfn /usr/local/php-7.0/bin/php-config /usr/bin/php-config && \
  ln -sfn /usr/local/php-7.0/bin/phpize /usr/bin/phpize && \
  composer install && \
  mv vendor /usr/local/vendor-7.0 && \
  ln -sfn /usr/local/php-7.1/bin/php /usr/bin/php && \
  ln -sfn /usr/local/php-7.1/bin/php-config /usr/bin/php-config && \
  ln -sfn /usr/local/php-7.1/bin/phpize /usr/bin/phpize && \
  composer install && \
  mv vendor /usr/local/vendor-7.1

##################
# Go dependencies.
RUN apt-get install -y  \
  # -- For go -- \
  golang

##################
# Javascript dependencies.
RUN apt-get install -y \
  # -- For javascript -- \
  npm

##################
# Python 3.5 3.6 dependencies.
RUN apt-get clean && apt-get update && apt-get install -y --force-yes \
  python3.5-dev \
  python3.6-dev \
  && apt-get clean

# On Debian/Ubuntu, nodejs binary is named 'nodejs' because the name 'node'
# is taken by another legacy binary. We don't have that legacy binary and
# npm expects the binary to be named 'node', so we just create a symbol
# link here.
RUN ln -s `which nodejs` /usr/bin/node

##################
# Prepare ccache

RUN ln -s /usr/bin/ccache /usr/local/bin/gcc
RUN ln -s /usr/bin/ccache /usr/local/bin/g++
RUN ln -s /usr/bin/ccache /usr/local/bin/cc
RUN ln -s /usr/bin/ccache /usr/local/bin/c++
RUN ln -s /usr/bin/ccache /usr/local/bin/clang
RUN ln -s /usr/bin/ccache /usr/local/bin/clang++

# Define the default command.
CMD ["bash"]

常见命令解释

FROM 指定镜像源

FROM ubuntu
# Base Dockerfile for gRPC dev images
FROM debian:latest

网站:http://hub.docker.com
FROM命令是用来指定你需要用哪个镜像作为基础镜像进行构建,一般来说Dockerfile的第一行都会先指定一个镜像,官方仓库中有很多镜像,可以通过http://hub.docker.com进行搜索,建议尽量使用官方镜像作为基础镜像,因为官方镜像都是精简过的稳定版。

RUN 执行linux命令

比如我们需要安装vim
写法1:

RUN apt-get update -y
RUN apt-get install -y vim

RUN是用来执行linux命令的指令,例如ubuntu系统下,需要更新软件包list,就需要执行apt-get update,之后会跟着执行apt-get install
写法2:

RUN apt-get update -y \
    && apt-get install -y vim

参考例子:

# Apt source for old Python versions.
RUN echo 'deb http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu trusty main' > /etc/apt/sources.list.d/deadsnakes.list && \
  apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DB82666C

# Apt source for Oracle Java.
RUN echo 'deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main' > /etc/apt/sources.list.d/webupd8team-java-trusty.list && \
  apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886 && \
  echo "oracle-java7-installer shared/accepted-oracle-license-v1-1 select true" | debconf-set-selections

# Apt source for Mono
RUN echo "deb http://download.mono-project.com/repo/debian wheezy main" | tee /etc/apt/sources.list.d/mono-xamarin.list && \
  echo "deb http://download.mono-project.com/repo/debian wheezy-libjpeg62-compat main" | tee -a /etc/apt/sources.list.d/mono-xamarin.list && \
  apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

# Apt source for php
RUN echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu trusty main" | tee /etc/apt/sources.list.d/various-php.list && \
  apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F4FCBB07

# Install dotnet SDK based on https://www.microsoft.com/net/core#debian
# (Ubuntu instructions need apt to support https)
RUN apt-get update && apt-get install -y --force-yes curl libunwind8 gettext && \
  curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=847105 &&  \
  mkdir -p /opt/dotnet && tar zxf dotnet.tar.gz -C /opt/dotnet && \
  ln -s /opt/dotnet/dotnet /usr/local/bin

# Install dependencies.  We start with the basic ones require to build protoc
# and the C++ build
RUN apt-get clean && apt-get update && apt-get install -y --force-yes \
  autoconf \
  autotools-dev \
  build-essential \
  bzip2 \
  ccache \
  curl \
  gcc \
  git \
  libc6 \
  libc6-dbg \
  libc6-dev \
  libgtest-dev \
  libtool \
  make \
  parallel \
  time \
  wget \
  # -- For csharp --
  mono-devel \
  referenceassemblies-pcl \
  nunit \
  # -- For all Java builds -- \
  maven \
  # -- For java_jdk6 -- \
  #   oops! not in jessie. too old? openjdk-6-jdk \
  # -- For java_jdk7 -- \
  openjdk-7-jdk \
  # -- For java_oracle7 -- \
  oracle-java7-installer \
  # -- For python / python_cpp -- \
  python-setuptools \
  python-pip \
  python-dev \
  python2.6-dev \
  python3.3-dev \
  python3.4-dev \
  # -- For Ruby --
  ruby \
  # -- For C++ benchmarks --
  cmake \
  # -- For PHP --
  php5.6     \
  php5.6-dev \
  php5.6-xml \
  php7.0     \
  php7.0-dev \
  php7.0-xml \
  phpunit    \
  valgrind   \
  libxml2-dev \
  && apt-get clean
  
##################
# C# dependencies

RUN wget www.nuget.org/NuGet.exe -O /usr/local/bin/nuget.exe

##################
# Python dependencies

# These packages exist in apt-get, but their versions are too old, so we have
# to get updates from pip.

RUN pip install pip --upgrade
RUN pip install virtualenv tox yattag

##################
# Ruby dependencies

# Install rvm
RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
RUN \curl -sSL https://get.rvm.io | bash -s stable

# Install Ruby 2.1, Ruby 2.2 and JRuby 1.7
RUN /bin/bash -l -c "rvm install ruby-2.1"
RUN /bin/bash -l -c "rvm install ruby-2.2"
RUN /bin/bash -l -c "rvm install jruby-1.7"
RUN /bin/bash -l -c "echo 'gem: --no-ri --no-rdoc' > ~/.gemrc"
RUN /bin/bash -l -c "echo 'export PATH=/usr/local/rvm/bin:$PATH' >> ~/.bashrc"
RUN /bin/bash -l -c "gem install bundler --no-ri --no-rdoc"

USER 指定当前用户

USER root
USER username

EXPOSE 暴露端口

声明端口,作用是帮助镜像使用者理解这个镜像服务的守护端口,以方便配置映射。在运行时使用随机端口映射时,也就是 docker run -P时,会自动随机映射EXPOSE的端口。

EXPOSE <端口1> [<端口2>...]

在 Dockerfile 中写入这样的声明有两个好处:

  1. 一个是帮助镜像使用者理解这个镜像服务的守护端口,以方便配置映射;
  2. 另一个用处则是在运行时使用随机端口映射时,也就是 docker run -P 时,会自动随机映射 EXPOSE 的端口。
    例子:
EXPOSE 端口号
EXPOSE 端口号/协议

默认协议是 TCP

EXPOSE 80/tcp
EXPOSE 80/udp
FROM nginx
EXPOSE 80

在docker-compose中

通常在docker-compose中,会通过ports来映射并暴露端口,docker-compose用法如下:文章来源地址https://www.toymoban.com/news/detail-719412.html

ports:
    - "8001:80"

到了这里,关于Dockerfile文件详细教程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Docker】个人镜像文件Dockerfile制作详解

    前言 洁洁的个人主页 我就问你有没有发挥! 知行合一,志存高远。 Docker 是一个 开源的应用容器引擎 ,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux或Windows 操作系统的机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之

    2024年02月13日
    浏览(41)
  • 【Docker】用Dockerfile制作个人的镜像文件

    作者简介: 辭七七,目前大一,正在学习C/C++,Java,Python等 作者主页: 七七的个人主页 文章收录专栏: 七七的闲谈 欢迎大家点赞 👍 收藏 ⭐ 加关注哦!💖💖 前言 Docker 是一个 开源的应用容器引擎 ,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发

    2024年02月10日
    浏览(40)
  • 【Docker】Docker容器数据卷、容器卷之间的继承和DockerFIle的详细讲解

    🚀欢迎来到本文🚀 🍉个人简介:陈童学哦,目前学习C/C++、算法、Python、Java等方向,一个正在慢慢前行的普通人。 🏀系列专栏:陈童学的日记 💡其他专栏:C++STL,感兴趣的小伙伴可以看看。 🎁希望各位→点赞👍 + 收藏⭐️ + 留言📝 ​ ⛱️万物从心起,心动则万物动🏄

    2024年02月14日
    浏览(38)
  • 【docker知识】如何从镜像返回它的Dockerfile文件

            Docker 镜像是通过构建 Dockerfiles 创建的。构建过程执行 Dockerfile 中的指令来创建构成最终镜像的文件系统层。 如果给出已有图像,您可以检索构建它的 Dockerfile 吗?在本文中,我们将研究两种可以实现此目的的方法。         当您构建自己的 Docker 映像时,您

    2024年02月04日
    浏览(33)
  • SpringCloud微服务(二)网关GateWay、Docker、Dockerfile、Linux操作超详细

    目录 统一网关GateWay  搭建网关服务的步骤 1、引入依赖 2、编写路由配置及nacos地址 路由断言工厂Route Oredicate Factory  路由过滤器配置  全局过滤器GlobalFilter 过滤器执行顺序 跨域问题处理   Docker  ​编辑 Docker与虚拟机  镜像和容器 Docker的安装 启动docker 配置镜像加速 Dock

    2024年02月11日
    浏览(48)
  • docker 系列之 Dockerfile 文件里 cmd命令与entrypoint命令区别

    cmd给出的是一个容器的默认的可执行体。也就是容器启动以后,默认的执行的命令。重点就是这个“默认”。意味着,如果docker run没有指定任何的执行命令或者dockerfile里面也没有entrypoint,那么,就会使用cmd指定的默认的执行命令执行。同时也从侧面说明了entrypoint的含义,它

    2024年02月05日
    浏览(51)
  • 四、Dockerfile应用案例教程(将一个或多个jar包部署到docker容器中运行)

    现有环境如下: 1.工作目录下有若干个可执行的jar包:在工作目录下打开命令行进行操作(Windows和Linux版的docker均可,这里以Windows为例) 2.拉取任意一个能运行jar包的jdk镜像:我的是adoptopenjdk/openjdk12 3.已具备jar包所需的基本组件:例如mysql、redis、nacos等 假如我只想将nettys

    2024年02月04日
    浏览(48)
  • Docker在windows下使用教程,通过Dockerfile创建镜像/容器,以YOLO系列为例

     通过可视化界面将极大的降低学习难度。  1.1、Docker Desktop下载  下载地址:Docker Desktop: The #1 Containerization Tool for Developers | Docker 应当是这个界面,选择下载即可 1.2、下载完成后需打开window自带的虚拟机       将Hyper-V勾选即打开,勾选后需重启。  1.3、下载WSL,由于是在

    2024年02月05日
    浏览(50)
  • Docker Compose映射卷的作用是什么,dockerfile这个文件有什么区别和联系?

    Docker Compose中映射卷(Volumes)的作用和Dockerfile之间既有区别也有联系。下面详细解释两者的作用、区别和联系: Docker Compose映射卷的作用 在Docker Compose中,卷(Volumes)用于数据持久化和数据共享: 数据持久化 :容器自身是易失的,当容器被删除时,存储在容器内部的数据也

    2024年02月21日
    浏览(43)
  • DockerFile文件部署Ubuntu18.04、Ros melodic、SLambook2的编译详细攻略

            最近,在Ubuntu系统上跑各种项目,经常出现环境配置干扰问题,导致项目常年无法运行。因此,查询各种方法,最终采用使用Docker来解决此问题,并成功运行 Rviz和Gazebo!!!         以下,为本人在配置、编写DockerFile文档时候遇到的一些问题。        

    2024年01月20日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包