docker:run_apache_server
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
docker:run_apache_server [2016/10/17 08:56] – peter | docker: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: | Let’s launch an Ubuntu container and install Apache inside of it using the bash prompt: | ||
Line 8: | Line 9: | ||
</ | </ | ||
- | The -t and -i flags allocate a pseudo-tty and keep stdin open even if not attached. | + | The **-t** and **-i** flags allocate a pseudo-tty and keep stdin open even if not attached. |
+ | Install Apache with | ||
- | ====================================== | + | <code bash> |
+ | apt update && apt install apache2 | ||
+ | </ | ||
+ | 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**: | ||
+ | If the image doesn’t exist on your local machine, Docker will attempt to fetch it from the public image registry | ||
+ | </ | ||
+ | ---- | ||
+ | ===== 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/ | ||
+ | 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:// | ||
+ | Commit the container with the container ID, your username, and the name apache: | ||
+ | |||
+ | <code bash> | ||
+ | docker commit 72d468f455ea sharewiz/ | ||
+ | </ | ||
+ | |||
+ | 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] | ||
+ | </ | ||
+ | |||
+ | The first step is to tell Docker that we want to run our sharewiz/ | ||
+ | |||
+ | <code bash> | ||
+ | docker run [options] sharewiz/ | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== 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/ | ||
+ | </ | ||
+ | |||
+ | After you are comfortable with the mechanics of running containers by hand, it’s recommended to use [[https:// | ||
+ | |||
+ | Do not run containers with detached mode inside of systemd unit files. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== 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> | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | Let’s add that to our command: | ||
+ | |||
+ | <code bash> | ||
+ | docker run -d sharewiz/ | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== 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:// | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== 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/ | ||
+ | </ | ||
+ | |||
+ | 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: | ||
+ | |||
+ | 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/ | ||
+ | </ | ||
+ | |||
+ | 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: | ||
+ | </ | ||
+ | |||
+ | After tagging, the image needs to be pushed to the registry: | ||
+ | |||
+ | <code bash> | ||
+ | docker push registry.example.com: | ||
+ | </ | ||
+ | |||
+ | 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: | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Another approach ===== | ||
+ | |||
+ | See https:// | ||
+ | |||
+ | FROM ubuntu: | ||
+ | |||
+ | <code bash> | ||
+ | RUN apt-get update && apt-get upgrade | ||
+ | RUN apt-get install -y openssh-server apache2 supervisor | ||
+ | RUN mkdir -p / | ||
+ | COPY supervisord.conf / | ||
+ | EXPOSE 22 80 | ||
+ | CMD ["/ | ||
+ | </ | ||
+ | |||
+ | and | ||
+ | |||
+ | <file bash / | ||
+ | [supervisord] | ||
+ | nodaemon=true | ||
+ | |||
+ | [program: | ||
+ | command=/ | ||
+ | |||
+ | [program: | ||
+ | command=/ | ||
+ | </ | ||
+ | |||
+ | Build and run: | ||
+ | |||
+ | <code bash> | ||
+ | sudo docker build -t < | ||
+ | sudo docker run -p 80:80 -t -i < | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== 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 29: | Line 214: | ||
https:// | https:// | ||
+ | <code bash> | ||
+ | FROM debian: | ||
+ | |||
+ | # add our user and group first to make sure their IDs get assigned consistently, | ||
+ | #RUN groupadd -r www-data && useradd -r --create-home -g www-data www-data | ||
+ | |||
+ | ENV HTTPD_PREFIX / | ||
+ | ENV PATH $HTTPD_PREFIX/ | ||
+ | RUN mkdir -p " | ||
+ | && chown www-data: | ||
+ | WORKDIR $HTTPD_PREFIX | ||
+ | |||
+ | # install httpd runtime dependencies | ||
+ | # https:// | ||
+ | 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 / | ||
+ | |||
+ | ENV HTTPD_VERSION 2.4.23 | ||
+ | ENV HTTPD_SHA1 5101be34ac4a509b245adb70a56690a84fcc4e7f | ||
+ | |||
+ | # https:// | ||
+ | ENV HTTPD_BZ2_URL https:// | ||
+ | # not all the mirrors actually carry the .asc files :'( | ||
+ | ENV HTTPD_ASC_URL https:// | ||
+ | |||
+ | # see https:// | ||
+ | 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 / | ||
+ | \ | ||
+ | && wget -O httpd.tar.bz2 " | ||
+ | && echo " | ||
+ | # see https:// | ||
+ | && wget -O httpd.tar.bz2.asc " | ||
+ | && export GNUPGHOME=" | ||
+ | && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys A93D62ECC3C8EA12DB220EC934EA76E6791485A8 \ | ||
+ | && gpg --batch --verify httpd.tar.bz2.asc httpd.tar.bz2 \ | ||
+ | && rm -r " | ||
+ | \ | ||
+ | && mkdir -p src \ | ||
+ | && tar -xvf httpd.tar.bz2 -C src --strip-components=1 \ | ||
+ | && rm httpd.tar.bz2 \ | ||
+ | && cd src \ | ||
+ | \ | ||
+ | && ./configure \ | ||
+ | --prefix=" | ||
+ | --enable-mods-shared=reallyall \ | ||
+ | && make -j" | ||
+ | && make install \ | ||
+ | \ | ||
+ | && cd .. \ | ||
+ | && rm -r src \ | ||
+ | \ | ||
+ | && sed -ri \ | ||
+ | -e ' | ||
+ | -e ' | ||
+ | " | ||
+ | \ | ||
+ | && apt-get purge -y --auto-remove $buildDeps | ||
+ | |||
+ | COPY httpd-foreground / | ||
+ | |||
+ | EXPOSE 80 | ||
+ | CMD [" | ||
+ | </ | ||
+ | |||
+ | ---- | ||
===== Example ===== | ===== Example ===== | ||
Line 46: | Line 316: | ||
Files are accessible on port 80. | Files are accessible on port 80. | ||
+ | ---- | ||
===== References ===== | ===== References ===== | ||
Line 52: | Line 323: | ||
http:// | http:// | ||
+ | |||
+ | |||
+ | TODO check this next link | ||
+ | https:// | ||
docker/run_apache_server.1476694584.txt.gz · Last modified: 2020/07/15 09:30 (external edit)