YouTip LogoYouTip

Docker Install Apache

* * * ### Method 1: docker pull httpd Search for the httpd image on (https://hub.docker.com/_/httpd?tab=tags): [!(#)](#) You can view other versions of httpd by using "Sort by". The default is the latest version **httpd:latest**. Additionally, we can use the `docker search httpd` command to see available versions: tutorial@tutorial:~/apache$ docker search httpd NAME DESCRIPTION STARS OFFICIAL AUTOMATED httpd The Apache HTTP Server .. 524 centos/httpd 7 rgielen/httpd-image-php5 Docker image for Apache... 1 microwebapps/httpd-frontend Httpd frontend allowing... 1 lolhens/httpd Apache httpd 2 Server 1 publici/httpd httpd:latest 0 publicisworldwide/httpd The Apache httpd webser... 0 rgielen/httpd-image-simple Docker image for simple... 0 solsson/httpd Derivatives of the offi... 0 rgielen/httpd-image-drush Apache HTTPD + Drupal S... 0 learninglayers/httpd 0 sohrabkhan/httpd Docker httpd + php5.6 (... 0 aintohvri/docker-httpd Apache HTTPD Docker ext... 0 alizarion/httpd httpd on centos with mo... 0 ... Here we pull the official image. tutorial@tutorial:~/apache$ docker pull httpd After the download is complete, you can find the image with REPOSITORY httpd in the local image list. tutorial@tutorial:~/apache$ docker images httpd REPOSITORY TAG IMAGE ID CREATED SIZE httpd latest da1536b4ef14 23 seconds ago 195.1 MB ### Method 2: Build via Dockerfile **Create Dockerfile** First, create a directory named `apache` to store the related files. tutorial@tutorial:~$ mkdir -p ~/apache/www ~/apache/logs ~/apache/conf The `www` directory will be mapped to the application directory configured in the Apache container. The `logs` directory will be mapped to the log directory of the Apache container. The configuration files in the `conf` directory will be mapped to the configuration files of the Apache container. Navigate to the created `apache` directory and create a Dockerfile. FROM debian:jessie # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added#RUN groupadd -r www-data && useradd -r --create-home -g www-data www-data ENV HTTPD_PREFIX /usr/local/apache2 ENV PATH $PATH:$HTTPD_PREFIX/bin RUN mkdir -p "$HTTPD_PREFIX" && chown www-data:www-data "$HTTPD_PREFIX" WORKDIR $HTTPD_PREFIX # install httpd runtime dependencies# https://httpd.apache.org/docs/2.4/install.html#requirements RUN apt-get update && apt-get install -y --no-install-recommends libapr1 libaprutil1 libaprutil1-ldap libapr1-dev libaprutil1-dev libpcre++0 libssl1.0.0 && rm -r /var/lib/apt/lists/* ENV HTTPD_VERSION 2.4.20 ENV HTTPD_BZ2_URL https://www.apache.org/dist/httpd/httpd-$HTTPD_VERSION.tar.bz2 RUN buildDeps=' ca-certificates curl bzip2 gcc libpcre++-dev libssl-dev make ' set -x && apt-get update && apt-get install -y --no-install-recommends $buildDeps && rm -r /var/lib/apt/lists/* && curl -fSL "$HTTPD_BZ2_URL" -o httpd.tar.bz2 && curl -fSL "$HTTPD_BZ2_URL.asc" -o httpd.tar.bz2.asc # see https://httpd.apache.org/download.cgi#verify && export GNUPGHOME="$(mktemp -d)" && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys A93D62ECC3C8EA12DB220EC934EA76E6791485A8 && gpg --batch --verify httpd.tar.bz2.asc httpd.tar.bz2 && rm -r "$GNUPGHOME" httpd.tar.bz2.asc && mkdir -p src && tar -xvf httpd.tar.bz2 -C src --strip-components=1 && rm httpd.tar.bz2 && cd src && ./configure --prefix="$HTTPD_PREFIX" --enable-mods-shared=reallyall && make -j"$(nproc)" && make install && cd .. && rm -r src && sed -ri -e 's!^(s*CustomLog)s+S+!1 /proc/self/fd/1!g' -e 's!^(s*ErrorLog)s+S+!1 /proc/self/fd/2!g' "$HTTPD_PREFIX/conf/httpd.conf" && apt-get purge -y --auto-remove $buildDeps COPY httpd-foreground /usr/local/bin/ EXPOSE 80 CMD The line `COPY httpd-foreground /usr/local/bin/` in the Dockerfile copies the `httpd-foreground` file from the current directory into the image to serve as the startup script for the httpd service. Therefore, we need to create a script file named `httpd-foreground` locally. #!/bin/bashset -e # Apache gets grumpy about PID files pre-existing rm -f /usr/local/apache2/logs/httpd.pid exec httpd -DFOREGROUND Grant executable permissions to the `httpd-foreground` file. tutorial@tutorial:~/apache$ chmod +x httpd-foreground Create an image using the Dockerfile, replacing it with your own name. tutorial@tutorial:~/apache$ docker build -t httpd . After creation, you can find the newly created image in the local image list. tutorial@tutorial:~/apache$ docker images httpd REPOSITORY TAG IMAGE ID CREATED SIZE httpd latest da1536b4ef14 23 seconds ago 195.1 MB * * * ## Using the Apache Image ### Run the Container docker run -p 80:80 -v $PWD/www/:/usr/local/apache2/htdocs/ -v $PWD/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf -v $PWD/logs/:/usr/local/apache2/logs/ -d httpd Command explanation: **-p 80:80:** The first `80` is the host port, and the second is the container port. This maps port 80 of the container to port 80 of the host. **-v $PWD/www/:/usr/local/apache2/htdocs/:** Mounts the `www` directory in the current directory of the host to `/usr/local/apache2/htdocs/` in the container. **-v $PWD/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf:** Mounts the `conf/httpd.conf` file in the current directory of the host to `/usr/local/apache2/conf/httpd.conf` in the container. **-v $PWD/logs/:/usr/local/apache2/logs/:** Mounts the `logs` directory in the current directory of the host to `/usr/local/apache2/logs/` in the container. For more detailed command reference: (#) Check the container startup status: tutorial@tutorial:~/apache$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES 79a97f2aac37 httpd "httpd-foreground" ... 0.0.0.0:80->80/tcp sharp_swanson Access via browser !(#)
← Misc Callbacks FireMisc Callbacks Disabled β†’