User Tools

Site Tools


docker:run_apache_server

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
docker:run_apache_server [2016/10/14 15:41] peterdocker:run_apache_server [2020/07/15 09:30] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Docker - Run Apache server ====== ====== Docker - Run Apache server ======
 +
 +===== Install Apache inside a container =====
 +
 +Let’s launch an Ubuntu container and install Apache inside of it using the bash prompt:
 +
 +<code bash>
 +docker run -t -i ubuntu /bin/bash
 +</code>
 +
 +The **-t** and **-i** flags allocate a pseudo-tty and keep stdin open even if not attached.  This will allow you to use the container like a traditional VM as long as the bash prompt is running.  
 +
 +Install Apache with 
 +
 +<code bash>
 +apt update && apt install apache2
 +</code>
 +
 +You’re probably wondering what address you can connect to in order to test that Apache was correctly installed…we’ll get to that after we commit the container.
 +
 +<WRAP info>
 +**NOTE**:  Launching a container is simple as docker run + the image name you would like to run + the command to run within the container.
 +
 +If the image doesn’t exist on your local machine, Docker will attempt to fetch it from the public image registry
 +</WRAP>
 +
 +----
 +
 +===== Committing a container =====
 +
 +After that completes, we need to **commit** these changes to our container with the container ID and the image name.
 +
 +To find the container ID, open another shell (so the container is still running) and read the ID using **docker ps**.
 +
 +The image name is in the format of **username/name**.  We’re going to use sharewiz as our username in this example but you should [[https://hub.docker.com/account/signup/|sign up for a Docker.IO user account]] and use that instead.
 +
 +It’s important to note that you can commit using any username and image name locally, but to push an image to the public registry, the username must be a valid [[https://hub.docker.com/account/signup/|Docker.IO user account]].
 +
 +Commit the container with the container ID, your username, and the name apache:
 +
 +<code bash>
 +docker commit 72d468f455ea sharewiz/apache
 +</code>
 +
 +The overlay filesystem works similar to git: our image now builds off of the **ubuntu** base and adds another layer with Apache on top.  These layers get cached separately so that you won’t have to pull down the ubuntu base more than once.
 +
 +----
 +
 +===== Keeping the Apache container running =====
 +
 +Now we have our Ubuntu container with Apache running in one shell and an image of that container sitting on disk.
 +
 +Let’s launch a new container based on that image but set it up to keep running indefinitely.
 +
 +The basic syntax looks like this, but we need to configure a few additional options that we’ll fill in as we go:
 +
 +<code bash>
 +docker run [options] [image] [process]
 +</code>
 +
 +The first step is to tell Docker that we want to run our sharewiz/apache image:
 +
 +<code bash>
 +docker run [options] sharewiz/apache [process]
 +</code>
 +
 +----
 +
 +===== Run container detached =====
 +
 +When running Docker containers manually, the most important option is to run the container in detached mode with the **-d** flag.
 +
 +This will output the container ID to show that the command was successful, but nothing else.
 +
 +At any time you can run docker ps in the other shell to view a list of the running containers.
 +
 +Our command now looks like:
 +
 +<code bash>
 +docker run -d sharewiz/apache [process]
 +</code>
 +
 +After you are comfortable with the mechanics of running containers by hand, it’s recommended to use [[https://coreos.com/docs/launching-containers/launching/getting-started-with-systemd|systemd units]] and/or [[https://coreos.com/docs/launching-containers/launching/launching-containers-fleet|fleet]] to run your containers on a cluster of ShareWiz machines.
 +
 +Do not run containers with detached mode inside of systemd unit files.  Detached mode prevents your init system, in our case systemd, from monitoring the process that owns the container because detached mode forks it into the background.  To prevent this issue, just omit the **-d** flag if you aren’t running something manually.
 +
 +----
 +
 +===== Run Apache in foreground =====
 +
 +We need to run the apache process in the foreground, since our container will stop when the process specified in the **docker run** command stops.
 +
 +We can do this with a flag **-D** when starting the apache2 process:
 +
 +<code bash>
 +/usr/sbin/apache2ctl -D FOREGROUND
 +</code>
 +
 +Let’s add that to our command:
 +
 +<code bash>
 +docker run -d sharewiz/apache /usr/sbin/apache2ctl -D FOREGROUND
 +</code>
 +
 +----
 +
 +===== Permanently running a container =====
 +
 +While the sections above explained how to run a container when configuring it, for a production setup, you should not manually start and babysit containers.
 +
 +Instead, create a systemd unit file to make systemd keep that container running. See the [[https://coreos.com/docs/launching-containers/launching/getting-started-with-systemd|Getting Started with systemd]] for details.
 +
 +----
 +
 +===== Network access to 80 =====
 +
 +The default apache install will be running on port 80.
 +
 +To give our container access to traffic over port 80, we use the -p flag and specify the port on the host that maps to the port inside the container.
 +
 +In our case we want 80 for each, so we include **-p 80:80** in our command:
 +
 +<code bash>
 +docker run -d -p 80:80 sharewiz/apache /usr/sbin/apache2ctl -D FOREGROUND
 +</code>
 +
 +You can now run this command on your host to create the container.
 +
 +You should see the default apache webpage when you load either **localhost:80** or the IP of your remote server.
 +
 +Be sure that any firewall or EC2 Security Group allows traffic to port 80.
 +
 +----
 +
 +===== Using the Docker registry =====
 +
 +Earlier we downloaded the ubuntu image remotely from the Docker public registry because it didn’t exist on our local machine.
 +
 +We can also push local images to the public registry (or a private registry) very easily with the push command:
 +
 +<code bash>
 +docker push sharewiz/apache
 +</code>
 +
 +To push to a private repository the syntax is very similar.
 +
 +First, we must prefix our image with the host running our private registry instead of our username.
 +
 +List images by running **docker images** and insert the correct ID into the tag command:
 +
 +<code bash>
 +docker tag f455ea72d468 registry.example.com:5000/apache
 +</code>
 +
 +After tagging, the image needs to be pushed to the registry:
 +
 +<code bash>
 +docker push registry.example.com:5000/apache
 +</code>
 +
 +Once the image is done uploading, you should be able to start the exact same container on a different ShareWiz host by running:
 +
 +<code bash>
 +docker run -d -p 80:80 registry.example.com:5000/apache /usr/sbin/apache2ctl -D FOREGROUND
 +</code>
 +
 +
 +----
 +
 +===== Another approach =====
 +
 +See https://docs.docker.com/articles/using_supervisord/.
 +
 +FROM ubuntu:14.04
 +
 +<code bash>
 +RUN apt-get update && apt-get upgrade
 +RUN apt-get install -y openssh-server apache2 supervisor
 +RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor
 +COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
 +EXPOSE 22 80
 +CMD ["/usr/bin/supervisord"]
 +</code>
 +
 +and
 +
 +<file bash /etc/supervisor/conf.d/supervisord.conf>
 +[supervisord]
 +nodaemon=true
 +
 +[program:sshd]
 +command=/usr/sbin/sshd -D
 +
 +[program:apache2]
 +command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
 +</file>
 +
 +Build and run:
 +
 +<code bash>
 +sudo docker build -t <yourname>/supervisord .
 +sudo docker run -p 80:80 -t -i <yourname>/supervisord
 +</code>
 +
 +
 +----
 +
 +===== Office Apache Image =====
  
 There is an official image for apache. The image documentation contains instructions in how you can use this official images as a base for a custom image. There is an official image for apache. The image documentation contains instructions in how you can use this official images as a base for a custom image.
Line 7: Line 214:
 https://github.com/docker-library/httpd/blob/master/2.4/Dockerfile https://github.com/docker-library/httpd/blob/master/2.4/Dockerfile
  
 +<code bash>
 +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 $HTTPD_PREFIX/bin:$PATH
 +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.23
 +ENV HTTPD_SHA1 5101be34ac4a509b245adb70a56690a84fcc4e7f
 +
 +# https://issues.apache.org/jira/browse/INFRA-8753?focusedCommentId=14735394#comment-14735394
 +ENV HTTPD_BZ2_URL https://www.apache.org/dyn/closer.cgi?action=download&filename=httpd/httpd-$HTTPD_VERSION.tar.bz2
 +# not all the mirrors actually carry the .asc files :'(
 +ENV HTTPD_ASC_URL https://www.apache.org/dist/httpd/httpd-$HTTPD_VERSION.tar.bz2.asc
 +
 +# see https://httpd.apache.org/docs/2.4/install.html#requirements
 +RUN set -x \
 + && buildDeps=' \
 + bzip2 \
 + ca-certificates \
 + gcc \
 + libpcre++-dev \
 + libssl-dev \
 + make \
 + wget \
 + ' \
 + && apt-get update \
 + && apt-get install -y --no-install-recommends $buildDeps \
 + && rm -r /var/lib/apt/lists/* \
 + \
 + && wget -O httpd.tar.bz2 "$HTTPD_BZ2_URL" \
 + && echo "$HTTPD_SHA1 *httpd.tar.bz2" | sha1sum -c - \
 +# see https://httpd.apache.org/download.cgi#verify
 + && wget -O httpd.tar.bz2.asc "$HTTPD_ASC_URL" \
 + && 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 ["httpd-foreground"]
 +</code>
 +
 +----
  
 ===== Example ===== ===== Example =====
Line 24: Line 316:
 Files are accessible on port 80. Files are accessible on port 80.
  
 +----
  
 ===== References ===== ===== References =====
Line 30: Line 323:
  
 http://slopjong.de/2014/09/17/install-and-run-a-web-server-in-a-docker-container/ http://slopjong.de/2014/09/17/install-and-run-a-web-server-in-a-docker-container/
 +
 +
 +TODO check this next link
 +https://github.com/jacksoncage/apache-docker
  
docker/run_apache_server.1476459702.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki