Docker学习实践1-使用dockerfile构建镜像

针对开源网络打印机软件CUPS的容器化实践

创建一个目录,并在目录内建立一个文件名为Dockerfile的文件和文件名为cupsd.conf的配置文件

Dockerfile

#使用原始镜像
FROM centos:6
#作者
MAINTAINER shark1985
#使用阿里云yum源
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo && yum makecache
#安装cups及组件
RUN yum -y install cups cups-libs
#备份原始配置文件
RUN mv /etc/cups/cupsd.conf /etc/cups/cupsd.conf.bak
#将cupsd.conf文件复制到配置目录
COPY cupsd.conf /etc/cups/
#开放631端口
EXPOSE 631
#运行cups服务
CMD ["cupsd"]

cupsd.conf

  • 其中修改了"Listen *:631",允许任何地址访问
  • 如下增加"Allow all"配置
    Restrict access to the server...

    Order allow,deny
    Allow all

Restrict access to the admin pages...

Order allow,deny
Allow all

Restrict access to configuration files...

AuthType Default
Require user @SYSTEM
Order allow,deny
Allow all

cupsd.conf文件内容

MaxLogSize 0
#
# "$Id: cupsd.conf.in 8805 2009-08-31 16:34:06Z mike $"
#
# Sample configuration file for the CUPS scheduler.  See "man cupsd.conf" for a
# complete description of this file.
#

# Log general information in error_log - change "warn" to "debug"
# for troubleshooting...
LogLevel warn

# Administrator user group...
SystemGroup sys root


# Only listen for connections from the local machine.
Listen *:631
Listen /var/run/cups/cups.sock

# Show shared printers on the local network.
Browsing On
BrowseOrder allow,deny
BrowseAllow all
BrowseLocalProtocols CUPS dnssd

# Default authentication type, when authentication is required...
DefaultAuthType Basic

# Restrict access to the server...

  Order allow,deny
  Allow all


# Restrict access to the admin pages...

  Order allow,deny
  Allow all


# Restrict access to configuration files...

  AuthType Default
  Require user @SYSTEM
  Order allow,deny
  Allow all


# Set the default printer/job policies...

  # Job-related operations must be done by the owner or an administrator...
  
    Require user @OWNER @SYSTEM
    Order deny,allow
  

  # All administration operations require an administrator to authenticate...
  
    AuthType Default
    Require user @SYSTEM
    Order deny,allow
  

  # All printer operations require a printer operator to authenticate...
  
    AuthType Default
    Require user @SYSTEM
    Order deny,allow
  

  # Only the owner or an administrator can cancel or authenticate a job...
  
    Require user @OWNER @SYSTEM
    Order deny,allow
  

  
    Order deny,allow
  


# Set the authenticated printer/job policies...

  # Job-related operations must be done by the owner or an administrator...
  
    AuthType Default
    Order deny,allow
  

  
    AuthType Default
    Require user @OWNER @SYSTEM
    Order deny,allow
  

  # All administration operations require an administrator to authenticate...
  
    AuthType Default
    Require user @SYSTEM
    Order deny,allow
  

  # All printer operations require a printer operator to authenticate...
  
    AuthType Default
    Require user @SYSTEM
    Order deny,allow
  

  # Only the owner or an administrator can cancel or authenticate a job...
  
    AuthType Default
    Require user @OWNER @SYSTEM
    Order deny,allow
  

  
    Order deny,allow
  


构建镜像

docker build -t office-cups-centos6 .

构建过程

Sending build context to Docker daemon  6.656kB
Step 1/8 : FROM centos:6
 ---> d0957ffdf8a2
Step 2/8 : MAINTAINER shark1985
 ---> Using cache
 ---> 27ecd3caf516
Step 3/8 : RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo && yum makecache
 ---> Using cache
 ---> b1c6f3ba74d5
Step 4/8 : RUN yum -y install cups cups-libs
 ---> Using cache
 ---> 48e62c3cb9c7
Step 5/8 : RUN mv /etc/cups/cupsd.conf /etc/cups/cupsd.conf.bak
 ---> Running in b916430865f1
Removing intermediate container b916430865f1
 ---> 0bec467158d6
Step 6/8 : COPY cupsd.conf /etc/cups/
 ---> 16187084007f
Step 7/8 : EXPOSE 631
 ---> Running in e9644f736601
Removing intermediate container e9644f736601
 ---> 3322999c070b
Step 8/8 : CMD ["cupsd"]
 ---> Running in 9eec5c9fc7dd
Removing intermediate container 9eec5c9fc7dd
 ---> 354c91defd47
Successfully built 354c91defd47
Successfully tagged office-cups-centos6:latest

查看镜像

docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
office-cups-centos6          latest              354c91defd47        About an hour ago   487MB

使用镜像运行容器

docker run -d -p 631:631  office-cups:latest

docker ps -a
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS                      PORTS                                          NAMES
e63fc4ae54cc        office-cups-centos6:latest   "cupsd"                  About an hour ago   Up About an hour            0.0.0.0:631->631/tcp

进入容器为root添加密码,才能管理CUPS

docker exec -it e63fc4ae54cc /bin/bash
[root@e63fc4ae54cc /]# passwd

通过https访问CUPS管理页面
https://ip:631/admin
使用前面的root账号和密码登录

image.png

你可能感兴趣的:(Docker学习实践1-使用dockerfile构建镜像)