Dockerfile 示例一:创建一个MongoDB的镜像

来源:互联网 发布:2012中国网络购物规模 编辑:程序博客网 时间:2024/05/16 08:33

如何使用Dockerfiles

使用Dockerfiles和手工使用Docker Daemon运行命令一样简单。脚本运行后输出为新的镜像ID。

  1. # Build an image using the Dockerfile at current location
  2. # Example: sudo docker build -t [name] .
  3. sudo docker build -t my_mongodb . 

Dockerfile 示例一:创建一个MongoDB的镜像

在这部分中,我们讲一步一步创建一个Dockfile,这个Dockerfile可用于构建MongoDB镜像进而构建MongoDB容器。

创建一个Dockerfile

使用nano文本编辑器,让我们创建Dockerfile。

  1. sudo nano Dockerfile

定义文件和它的目的

让阅读者明确Dockerfile的目的永远是必要的。为此,我们通常从注释开始写Dockerfile。

  1. ############################################################
  2. # Dockerfile to build MongoDB container images
  3. # Based on Ubuntu
  4. ############################################################

设置基础镜像 

  1. # Set the base image to Ubuntu
  2. FROM ubuntu

定义作者

  1. # File Author / Maintainer
  2. MAINTAINER Example McAuthor

设置命令与参数下载MongoDB

  1. ################## BEGIN INSTALLATION ######################
  2. # Install MongoDB Following the Instructions at MongoDB Docs
  3. # Ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
  4. # Add the package verification key
  5. RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  6. # Add MongoDB to the repository sources list
  7. RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list
  8. # Update the repository sources list once more
  9. RUN apt-get update
  10. # Install MongoDB package (.deb)
  11. RUN apt-get install -y mongodb-10gen
  12. # Create the default data directory
  13. RUN mkdir -p /data/db
  14. ##################### INSTALLATION END #####################

设置MongoDB端口 

  1. # Expose the default port
  2. EXPOSE 27017
  3. # Default port to execute the entrypoint (MongoDB)
  4. CMD ["--port 27017"]
  5. # Set default container command
  6. ENTRYPOINT usr/bin/mongod

保存Dockerfile。

构建镜像

使用上述的Dockerfile,我们已经可以开始构建MongoDB镜像

  1. sudo docker build -t my_mongodb .
0 0
原创粉丝点击