Dockerfile using Dockerfile to create a CentOS Docker image with Apache service

Source: Internet
Author: User
Tags prepare ssh centos docker ps docker run
List of files prepared on the host:
Dockerfile
#Start the ssh and apache service corner book
run.sh


The above files are placed in the/root/apache_centos directory


Mkdir-p/root/apache_centos
Cd/root/apache_centos


Base Image: An image of an open SSH service based on a mirrored CentOS


[Root@localhost apache_centos]# Docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
sshd                dockerfile          411d5fb16366        hours ago        278 MB
centos              latest              0f73ae75014f        5 Weeks ago         172.3 MB


First, prepare run.sh file
New run.sh in/root/apache_centos directory


Vim run.sh


The contents are as follows:


#!/bin/bash
/usr/sbin/sshd &
/usr/local/apache2/bin/httpd-d FOREGROUND


Second, prepare Dockerfile
New Dockerfile in/root/apache_centos directory


Vim Dockerfile


The contents of the file are as follows:


#The newly generated image is based on the sshd: dockerfile image
FROM sshd: dockerfile
MAINTAINER by Steven
#Install wget
RUN yum install -y wget
WORKDIR / usr / local / src
#Download and unzip the source package
RUN wget http://apache.fayea.com/httpd/httpd-2.4.17.tar.gz
RUN tar -zxvf httpd-2.4.17.tar.gz
WORKDIR httpd-2.4.17
#Compile and install apache
RUN yum install -y gcc make apr-devel apr apr-util apr-util-devel pcre-devel
RUN ./configure --prefix = / usr / local / apache2 --enable-mods-shared = most --enable-so
RUN make
RUN make install
#Modify the apache configuration file
RUN sed -i 's / # ServerName www.example.com:80/ServerName localhost: 80 / g' /usr/local/apache2/conf/httpd.conf
#Start the apache service
RUN / usr / local / apache2 / bin / httpd
#Copy service startup script and set permissions
ADD run.sh /usr/local/sbin/run.sh
RUN chmod 755 /usr/local/sbin/run.sh
#Open port 80
EXPOSE 80
CMD ["/usr/local/sbin/run.sh"]


Note that the "CD" command should not be used to replace the current directory in the Dockerfile file, but instead use "Workdir".
Third, create the image


Docker build-t Apache_dockerfile:centos.


To view the generated image:


[Root@localhost apache_centos]# Docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
Apache_dockerfile   CentOS              f8f30b4a0ee8        minutes ago-      MB
Apache              CentOS              F92c55dddd07        hours ago        423.9 MB
sshd                dockerfile 411d5fb16366 all        hours ago        278 MB
centos              latest              0f73ae75014f        5 weeks ago         172.3 MB


Four, according to the container generated by the image and test
1. Create a new container


Docker run-d-P 2222:22-p 8000:80 apache_dockerfile:centos/usr/local/sbin/run.sh


Map the container's 22 ports and 80 ports to 2222 ports and 8000 ports on the host, and run the service scripts.



2. View the newly generated container:


[Root@localhost apache_centos]# Docker ps-a
CONTAINER ID        IMAGE                      COMMAND                CREATED             STATUS                      PORTS                                        NAMES
ae560e497f39        apache_dockerfile:centos   "/usr/local/sbin/run seconds   ago               up seconds 0.0.0.0:2222->22/tcp, 0.0.0.0:8000->80/tcp   condescending_bardeen
6490CD244C10        apache:centos              "/usr/local/apache2/   hours ago        Exited (0) + hours ago                                                  loving_wright
673e946b57e4        sshd:dockerfile            "/usr/local/sbin/run   hours ago        Exited (137) hours ago                                                Reverent_bell


3. Testing
Test Apache


[root@localhost apache_centos]# Curl localhost:8000


Success.
Test SSH


[root@localhost apache_centos]# ssh localhost-p 2222
root@localhost ' s password:


Success.








Related Article

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.