如何使用Dockerfiles
使用Dockerfiles和手工使用Docker Daemon运行命令一样简单。脚本运行后输出为新的镜像ID。
# Build an image using the Dockerfile at current location# Example: sudo docker build -t [name] .sudo docker build -t my_mongodb .
Dockerfile 示例一:创建一个MongoDB的镜像
在这部分中,我们讲一步一步创建一个Dockfile,这个Dockerfile可用于构建MongoDB镜像进而构建MongoDB容器。
创建一个Dockerfile
使用nano文本编辑器,让我们创建Dockerfile。
sudo nano Dockerfile
定义文件和它的目的
让阅读者明确Dockerfile的目的永远是必要的。为此,我们通常从注释开始写Dockerfile。
############################################################# Dockerfile to build MongoDB container images# Based on Ubuntu############################################################
设置基础镜像
# Set the base image to UbuntuFROM ubuntu
定义作者
# File Author / MaintainerMAINTAINER Example McAuthor
设置命令与参数下载MongoDB
################## BEGIN INSTALLATION ####################### Install MongoDB Following the Instructions at MongoDB Docs# Ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/# Add the package verification keyRUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10# Add MongoDB to the repository sources listRUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list# Update the repository sources list once moreRUN apt-get update# Install MongoDB package (.deb)RUN apt-get install -y mongodb-10gen# Create the default data directoryRUN mkdir -p /data/db##################### INSTALLATION END #####################
设置MongoDB端口
# Expose the default portEXPOSE 27017# Default port to execute the entrypoint (MongoDB)CMD ["--port 27017"]# Set default container commandENTRYPOINT usr/bin/mongod
保存Dockerfile。
构建镜像
使用上述的Dockerfile,我们已经可以开始构建MongoDB镜像
sudo docker build -t my_mongodb .
本文介绍如何使用Dockerfile创建MongoDB容器镜像,包括设置基础镜像、安装MongoDB及配置端口等步骤。
940

被折叠的 条评论
为什么被折叠?



