Create an CentOS Docker image with Apache services using Dockerfile

  • 2020-05-30 21:39:43
  • OfStack

Use Dockerfile to create an CentOS Docker image with Apache services

List of files prepared on the host:

Dockerfile


# Start the ssh and apache Service the corner book 
run.sh

The above files are placed in /root/apache_centos


mkdir -p /root/apache_centos
cd /root/apache_centos

Base mirror: a mirror of the open SSH service based on mirroring centos


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

1. Prepare run.sh documents

Create a new run.sh directory in /root/apache_centos


vim run.sh

It reads as follows:


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

2. Prepare Dockerfile

Create a new Dockerfile in /root/apache_centos


vim Dockerfile

The contents of the document are as follows:


# The newly generated image is based on sshd:dockerfile The mirror 
FROM sshd:dockerfile
MAINTAINER by Steven
# The installation 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 the installation 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 The 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 the service startup script and set the permissions 
ADD run.sh /usr/local/sbin/run.sh
RUN chmod 755 /usr/local/sbin/run.sh
# open 80 port 
EXPOSE 80
CMD ["/usr/local/sbin/run.sh"]

Note that you cannot use the "cd" command to change the current directory in the Dockerfile file. Instead, use "WORKDIR".

3. Mirror


docker build -t apache_dockerfile:centos .

View the generated image:


[root@localhost apache_centos]# docker images
REPOSITORY     TAG         IMAGE ID      CREATED       VIRTUAL SIZE
apache_dockerfile  centos       f8f30b4a0ee8    24 minutes ago   440 MB
apache       centos       f92c55dddd07    17 hours ago    423.9 MB
sshd        dockerfile     411d5fb16366    23 hours ago    278 MB
centos       latest       0f73ae75014f    5 weeks ago     172.3 MB

4. Create a container based on the image and test it

1. Generate a new container


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

Map port 22 and port 80 of the container to port 2222 and port 8000 on the host, respectively, and run the service script.

2. View the newly generated container:


mkdir -p /root/apache_centos
cd /root/apache_centos
0

3, test,

Test apache


mkdir -p /root/apache_centos
cd /root/apache_centos
1

Success!

Test ssh


mkdir -p /root/apache_centos
cd /root/apache_centos
2

Success!


Related articles: