Dockerfile configuration tutorial generates Docker image and implements Docker deployment

Source: Internet
Author: User
Tags fpm gpg install mongodb unpack install wordpress docker ps dockerfile example docker run

Docker there's a dockerfile, why should we use him?

To put it simply: we used to Docker run to create a Docker container that sometimes comes with a lot of parameters.

Like what:

The code is as follows Copy Code
Docker run-d-P 22-p 8080:8080 Ruifengyun/ubunt-redis "Redis-server redis.conf &&/usr/sbin/sshd-d"


Will find it very troublesome. This time we can create an edit Dockerfile file, generate a new image, and then create a container from this new image, with the associated startup items and ports inside the container, and the directory is well defined in advance.

Cat Dockerfile

  code is as follows copy code
#配置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



From is the basis for mirroring

RUN can be understood to do some deployment of the environment in the from-down mirror.

CMD is the command that will run after the container is created

Expose is the exposed port.

Maintainer notification of messages

ADD is the equivalent of the host's start.sh script passed to the container inside.

VOLUME is a map of the local path

Workdir is the path of execution, which is the path that CMD entrypoint executes.

The code is as follows Copy Code
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 [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 [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 would be installed:
Libjemalloc1 Redis-tools
The following NEW packages would 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 would 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 isn't set, so the dialog frontend are 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 the 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#




When the toss is over, he will generate a mirror image. This mirror is made by our dockerfile.



This time we'll create the container without adding so many parameters.

The code is as follows Copy Code
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


Let's have a more complete dockerfile example:

Features are used to deploy LNMP and WordPress, the configuration looks more, in fact, or more standardized.

The code is as follows Copy Code
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 so 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 php 5-ps Php5-pspell php5-recode php5-sqlite php5-tidy php5-xmlrpc

# 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/;d aemonizes*=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"]




Another example of a mognodb:

Note: You can wrap lines with identities

  code is as follows copy code
from Dockerfile/ubuntu
 
# Install MongoDB.
RUN
  Apt-key adv--keyserver hkp://keyserver.ubuntu.com:80--recv 7f0ceb10 &&
  Echo ' Deb Htt P://downloads-distro.mongodb.org/repo/ubuntu-upstart Dist 10gen ' | Tee/etc/apt/sources.list.d/mongodb.list &&
  apt-get update &&
  Apt-get install-y m ongodb-org &&
  mkdir-p/data/db
 
VOLUME ["/data"]
 
Workdir/data
  ;
CMD ["Mongod"]
 
Expose 27017
expose 28017

The



website has a lot of examples, interested friends can go to Https://github.com/dockerfile view.


Use cmd in dockerfile, entrypoint need to note:


Cmd is a model that can be written as a shell, that is, we usually write statements like

cmd  redis-s erver redis.conf && Service sshd restart


Docker called when it was invoked by/bin/sh-c.   This time there are a few problems, we test the time, the best in their own machine also measured general.   Sh-c This thing is very strange, seemingly his parameters punctuation problem, some eggs pain.


Generally, when you start with cmd   use exec's mode more points, that is, [' Redis-server ', '/etc/redis/redis.conf '] he will use a space to form a command.  


A dockerfile can have only one cmd inside. It's no use writing too much.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.