dockerfile配置教程 產生docker image並實現docker部署

來源:互聯網
上載者:User

docker有個dockerfile 是什麼東西,為什麼我們要用他?

簡單的說:  以前咱們docker run建立一個docker容器,有時候會附帶不少的參數。

比如:

 代碼如下 複製代碼
docker run -d -p 22 -p 8080:8080 ruifengyun/ubunt-redis "redis-server redis.conf && /usr/sbin/sshd -D"


會發現很是麻煩。  這個時候咱們可以建立編輯一個dockerfile檔案,產生新的image,然後從這個新的鏡像建立容器,容器裡面相關聯的啟動項和連接埠,目錄都是提前定義好的。

cat Dockerfile

 代碼如下 複製代碼
#配置redis
FROM ubuntu
MAINTAINER ruifengyun "ruifengyun@qq.com"
ADD ./start.sh  /root/start.sh
RUN apt-get update
RUN apt-get install -y redis-server
RUN apt-get install -y openssh-server
#CMD redis-server /etc/redis/redis.conf && /usr/sbin/sshd -D
CMD ["redis-server","/etc/redis/redis.conf"]
EXPOSE 6379
EXPOSE 22



FROM   是作為鏡像的基礎

RUN    可以理解為在FROM下來的鏡像做一些環境的部署。

CMD    是建立容器後,會啟動並執行命令

EXPOSE 是暴露的連接埠

MAINTAINER 通知的郵件

ADD    相當於把主機的start.sh指令碼傳遞給了容器裡面。

VOLUME  是本地的路徑的映射

WORKDIR 是執行的路徑,也就是cmd entrypoint執行的路徑。

 代碼如下 複製代碼
root@dev-ops:/var/4# docker build -t rui Dockerfile
Uploading context 2.048 kB
Uploading context
2014/08/16 09:50:59 Error: open /tmp/docker-build829651796/Dockerfile: not a directory
root@dev-ops:/var/4# docker build -t rui/redis .
Uploading context 2.56 kB
Uploading context
Step 0 : FROM ubuntu
 ---> c4ff7513909d
Step 1 : MAINTAINER Victor Coisne victor.coisne@dotcloud.com
 ---> Using cache
 ---> bbe0c91632f1
Step 2 : RUN apt-get update
 ---> Running in b10a1a60dcb3
Ign http://archive.ubuntu.com trusty InRelease
Ign http://archive.ubuntu.com trusty-updates InRelease
Ign http://archive.ubuntu.com trusty-security InRelease
Get:1 http://archive.ubuntu.com trusty Release.gpg [933 B]
Get:2 http://archive.ubuntu.com trusty-updates Release.gpg [933 B]
Get:3 http://archive.ubuntu.com trusty-security Release.gpg [933 B]
Get:4 http://archive.ubuntu.com trusty Release [58.5 kB]
Get:5 http://archive.ubuntu.com trusty-updates Release [59.7 kB]
Get:6 http://archive.ubuntu.com trusty-security Release [59.7 kB]
Get:7 http://archive.ubuntu.com trusty/main Sources [1335 kB]
Get:8 http://archive.ubuntu.com trusty/restricted Sources [5335 B]
Get:9 http://archive.ubuntu.com trusty/universe Sources [7926 kB]
Get:10 http://archive.ubuntu.com trusty/main amd64 Packages [1743 kB]
Get:11 http://archive.ubuntu.com trusty/restricted amd64 Packages [16.0 kB]
Get:12 http://archive.ubuntu.com trusty/universe amd64 Packages [7589 kB]
Get:13 http://archive.ubuntu.com trusty-updates/main Sources [138 kB]
Get:14 http://archive.ubuntu.com trusty-updates/restricted Sources [1250 B]
Get:15 http://archive.ubuntu.com trusty-updates/universe Sources [91.7 kB]
Get:16 http://archive.ubuntu.com trusty-updates/main amd64 Packages [375 kB]
Get:17 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [6341 B]
Get:18 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [235 kB]
Get:19 http://archive.ubuntu.com trusty-security/main Sources [47.4 kB]
Get:20 http://archive.ubuntu.com trusty-security/restricted Sources [40 B]
Get:21 http://archive.ubuntu.com trusty-security/universe Sources [11.9 kB]
Get:22 http://archive.ubuntu.com trusty-security/main amd64 Packages [167 kB]
Get:23 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [40 B]
Get:24 http://archive.ubuntu.com trusty-security/universe amd64 Packages [57.0 kB]
Fetched 19.9 MB in 11min 48s (28.1 kB/s)
Reading package lists...
 ---> 9ce87ae24eeb
Step 3 : RUN apt-get install -y redis-server
 ---> Running in b28a88665c3f
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  libjemalloc1 redis-tools
The following NEW packages will be installed:
  libjemalloc1 redis-server redis-tools
0 upgraded, 3 newly installed, 0 to remove and 3 not upgraded.
Need to get 410 kB of archives.
After this operation, 1272 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/universe libjemalloc1 amd64 3.5.1-2 [76.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/universe redis-tools amd64 2:2.8.4-2 [65.7 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/universe redis-server amd64 2:2.8.4-2 [267 kB]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin:
Fetched 410 kB in 4s (91.4 kB/s)
Selecting previously unselected package libjemalloc1.
(Reading database ... 11518 files and directories currently installed.)
Preparing to unpack .../libjemalloc1_3.5.1-2_amd64.deb ...
Unpacking libjemalloc1 (3.5.1-2) ...
Selecting previously unselected package redis-tools.
Preparing to unpack .../redis-tools_2%3a2.8.4-2_amd64.deb ...
Unpacking redis-tools (2:2.8.4-2) ...
Selecting previously unselected package redis-server.
Preparing to unpack .../redis-server_2%3a2.8.4-2_amd64.deb ...
Unpacking redis-server (2:2.8.4-2) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up libjemalloc1 (3.5.1-2) ...
Setting up redis-tools (2:2.8.4-2) ...
Setting up redis-server (2:2.8.4-2) ...
invoke-rc.d: policy-rc.d denied execution of start.
Processing triggers for libc-bin (2.19-0ubuntu6.1) ...
Processing triggers for ureadahead (0.100.0-16) ...
 ---> d37fb2bbe0b5
Step 4 : ENTRYPOINT redis-server /etc/redis/redis.conf && /usr/sbin/sshd -D
 ---> Running in f6c027ac643d
 ---> ec7fe19bdfed
Step 5 : USER daemon
 ---> Running in 0e3b10d07a16
 ---> d16398d08a4a
Step 6 : EXPOSE 6379
 ---> Running in c8ca52dde189
 ---> e0a9bcb25972
Step 7 : EXPOSE 22
 ---> Running in 22845a6abd90
 ---> 54bb130c7a44
Successfully built 54bb130c7a44
Removing intermediate container b10a1a60dcb3
Removing intermediate container b28a88665c3f
Removing intermediate container f6c027ac643d
Removing intermediate container 0e3b10d07a16
Removing intermediate container c8ca52dde189
Removing intermediate container 22845a6abd90
root@dev-ops:/var/4#




等折騰完了後,他會產生一個鏡像 。 這個鏡像是由咱們的dockerfile搞的。



這次咱們再建立容器,不用再加那麼多參數了。

 代碼如下 複製代碼
root@dev-ops:~# docker run -d -P rui
116b30b056493237caca158849ae687c9beb4f8656be485c2a3cc71a27d8e951
root@dev-ops:~#
root@dev-ops:~#
root@dev-ops:~# docker ps -a
CONTAINER ID        IMAGE                           COMMAND                CREATED             STATUS              PORTS                      NAMES
116b30b05649        rui:latest                      redis-server /etc/redis/redis.conf    4 seconds ago       Up 3 seconds        0.0.0.0:49153->6379/tcp   nostalgic_lumiere


咱們再來一個比較全的dockerfile例子:

功能是用來部署lnmp和wordpress  ,配置看起來多 ,其實還是比較規範的。

 代碼如下 複製代碼
FROM ubuntu:14.04
MAINTAINER liudehua <liudehua@xxx.com>
 
# Keep upstart from complaining
RUN dpkg-divert --local --rename --add /sbin/initctl
RUN ln -sf /bin/true /sbin/initctl
 
# Let the conatiner know that there is no tty
ENV DEBIAN_FRONTEND noninteractive
 
RUN apt-get update
RUN apt-get -y upgrade
 
# Basic Requirements
RUN apt-get -y install mysql-server mysql-client nginx php5-fpm php5-mysql php-apc pwgen python-setuptools curl git unzip
 
# Wordpress Requirements
RUN apt-get -y install php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-sqlite php5-tidy php5-xmlrpc php5-xsl
 
# mysql config
RUN sed -i -e"s/^bind-addresss*=s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
 
# nginx config
RUN sed -i -e"s/keepalive_timeouts*65/keepalive_timeout 2/" /etc/nginx/nginx.conf
RUN sed -i -e"s/keepalive_timeout 2/keepalive_timeout 2;ntclient_max_body_size 100m/" /etc/nginx/nginx.conf
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
 
# php-fpm config
RUN sed -i -e "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g" /etc/php5/fpm/php.ini
RUN sed -i -e "s/upload_max_filesizes*=s*2M/upload_max_filesize = 100M/g" /etc/php5/fpm/php.ini
RUN sed -i -e "s/post_max_sizes*=s*8M/post_max_size = 100M/g" /etc/php5/fpm/php.ini
RUN sed -i -e "s/;daemonizes*=s*yes/daemonize = no/g" /etc/php5/fpm/php-fpm.conf
RUN sed -i -e "s/;catch_workers_outputs*=s*yes/catch_workers_output = yes/g" /etc/php5/fpm/pool.d/www.conf
RUN find /etc/php5/cli/conf.d/ -name "*.ini" -exec sed -i -re 's/^(s*)#(.*)/1;2/g' {} ;
 
# nginx site conf
ADD ./nginx-site.conf /etc/nginx/sites-available/default
 
# Supervisor Config
RUN /usr/bin/easy_install supervisor
RUN /usr/bin/easy_install supervisor-stdout
ADD ./supervisord.conf /etc/supervisord.conf
 
# Install Wordpress
ADD http://wordpress.org/latest.tar.gz /usr/share/nginx/latest.tar.gz
RUN cd /usr/share/nginx/ && tar xvf latest.tar.gz && rm latest.tar.gz
RUN mv /usr/share/nginx/html/5* /usr/share/nginx/wordpress
RUN rm -rf /usr/share/nginx/www
RUN mv /usr/share/nginx/wordpress /usr/share/nginx/www
RUN chown -R www-data:www-data /usr/share/nginx/www
 
# Wordpress Initialization and Startup Script
ADD ./start.sh /start.sh
RUN chmod 755 /start.sh
 
# private expose
EXPOSE 3306
EXPOSE 80
 
CMD ["/bin/bash", "/start.sh"]




再來一個mognodb的例子:

註: 可以用   標識換行

 代碼如下 複製代碼
FROM dockerfile/ubuntu
 
# Install MongoDB.
RUN
  apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 &&
  echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list &&
  apt-get update &&
  apt-get install -y mongodb-org &&
  mkdir -p /data/db
 
VOLUME ["/data"]
 
WORKDIR /data
 
CMD ["mongod"]
 
EXPOSE 27017
EXPOSE 28017



官網有不少的例子,有興趣的朋友可以到 https://github.com/dockerfile 查看下。


在dockerfile使用cmd、entrypoint 需要注意:


cmd 是可以寫成shell的模式, 也就是 咱們平時寫語句那樣

CMD  redis-server redis.conf && service sshd restart


docker調用它的時候是用/bin/sh -c 調用的。  這個時候有些少許的問題,大家再測試的時候,最好在自己的本機也測一般。  sh -c 這東西挺奇妙的 ,貌似他的參數斷句有問題,有些蛋疼。


一般來說,在用cmd啟動的時候  用exec的模式多點 ,也就是 ['redis-server','/etc/redis/redis.conf'] 他自己會用空格組成一條命令。 


一個dockerfile裡面只能有一個CMD。 寫多了沒用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.