ubuntu:docker:run_apache_server
Differences
This shows you the differences between two versions of the page.
ubuntu:docker:run_apache_server [2019/11/27 01:20] – created peter | ubuntu:docker:run_apache_server [2020/04/15 22:24] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Ubuntu - 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 | ||
- | </ | ||
- | |||
- | The **-t** and **-i** flags allocate a pseudo-tty and keep stdin open even if not attached. | ||
- | |||
- | Install Apache with | ||
- | |||
- | <code bash> | ||
- | apt-get update && apt-get 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. | ||
- | |||
- | **NOTE**: | ||
- | |||
- | ---- | ||
- | |||
- | ===== 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. | ||
- | |||
- | <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. | ||
- | |||
- | <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. | ||
- | |||
- | <code bash> | ||
- | docker run -d -p 80:80 sharewiz/ | ||
- | </ | ||
- | |||
- | You can now run this command on your CoreOS host to create the container. | ||
- | |||
- | ---- | ||
- | |||
- | ===== 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. | ||
- | |||
- | <code bash> | ||
- | docker push sharewiz/ | ||
- | </ | ||
- | |||
- | To push to a private repository the syntax is very similar. | ||
- | |||
- | <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. | ||
- | |||
- | To see how it's done take a peek at the Dockerfile used by the official image: | ||
- | |||
- | 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 ===== | ||
- | |||
- | Ensure files are accessible to root | ||
- | |||
- | <code bash> | ||
- | sudo chown -R root:root / | ||
- | </ | ||
- | |||
- | Host these files using official docker image | ||
- | |||
- | <code bash> | ||
- | docker run -d -p 80:80 --name apache -v / | ||
- | </ | ||
- | |||
- | Files are accessible on port 80. | ||
- | |||
- | ---- | ||
- | |||
- | ===== References ===== | ||
- | |||
- | http:// | ||
- | |||
- | http:// | ||
- | |||
- | |||
- | TODO check this next link | ||
- | https:// | ||
ubuntu/docker/run_apache_server.1574817641.txt.gz · Last modified: 2020/07/15 09:30 (external edit)