mirror of https://github.com/docker/docs.git
1094 lines
46 KiB
XML
1094 lines
46 KiB
XML
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
|
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
|
<channel>
|
|
<title>Examples on Docker Docs</title>
|
|
<link>http://localhost/examples/</link>
|
|
<description>Recent content in Examples on Docker Docs</description>
|
|
<generator>Hugo -- gohugo.io</generator>
|
|
<language>en-us</language>
|
|
<atom:link href="http://localhost/examples/index.xml" rel="self" type="application/rss+xml" />
|
|
|
|
<item>
|
|
<title>Dockerizing MongoDB</title>
|
|
<link>http://localhost/examples/mongodb/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>http://localhost/examples/mongodb/</guid>
|
|
<description>
|
|
|
|
<h1 id="dockerizing-mongodb">Dockerizing MongoDB</h1>
|
|
|
|
<h2 id="introduction">Introduction</h2>
|
|
|
|
<p>In this example, we are going to learn how to build a Docker image with
|
|
MongoDB pre-installed. We&rsquo;ll also see how to <code>push</code> that image to the
|
|
<a href="https://hub.docker.com">Docker Hub registry</a> and share it with others!</p>
|
|
|
|
<blockquote>
|
|
<p><strong>Note:</strong></p>
|
|
|
|
<p>This guide will show the mechanics of building a MongoDB container, but
|
|
you will probably want to use the official image on <a href="https://registry.hub.docker.com/_/mongo/">Docker Hub</a></p>
|
|
</blockquote>
|
|
|
|
<p>Using Docker and containers for deploying <a href="https://www.mongodb.org/">MongoDB</a>
|
|
instances will bring several benefits, such as:</p>
|
|
|
|
<ul>
|
|
<li>Easy to maintain, highly configurable MongoDB instances;</li>
|
|
<li>Ready to run and start working within milliseconds;</li>
|
|
<li>Based on globally accessible and shareable images.</li>
|
|
</ul>
|
|
|
|
<blockquote>
|
|
<p><strong>Note:</strong></p>
|
|
|
|
<p>If you do <strong><em>not</em></strong> like <code>sudo</code>, you might want to check out:
|
|
<a href="http://localhost/examples/examples/installation/binaries/#giving-non-root-access"><em>Giving non-root access</em></a>.</p>
|
|
</blockquote>
|
|
|
|
<h2 id="creating-a-dockerfile-for-mongodb">Creating a Dockerfile for MongoDB</h2>
|
|
|
|
<p>Let&rsquo;s create our <code>Dockerfile</code> and start building it:</p>
|
|
|
|
<pre><code>$ nano Dockerfile
|
|
</code></pre>
|
|
|
|
<p>Although optional, it is handy to have comments at the beginning of a
|
|
<code>Dockerfile</code> explaining its purpose:</p>
|
|
|
|
<pre><code># Dockerizing MongoDB: Dockerfile for building MongoDB images
|
|
# Based on ubuntu:latest, installs MongoDB following the instructions from:
|
|
# http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
|
|
</code></pre>
|
|
|
|
<blockquote>
|
|
<p><strong>Tip:</strong> <code>Dockerfile</code>s are flexible. However, they need to follow a certain
|
|
format. The first item to be defined is the name of an image, which becomes
|
|
the <em>parent</em> of your <em>Dockerized MongoDB</em> image.</p>
|
|
</blockquote>
|
|
|
|
<p>We will build our image using the latest version of Ubuntu from the
|
|
<a href="https://registry.hub.docker.com/_/ubuntu/">Docker Hub Ubuntu</a> repository.</p>
|
|
|
|
<pre><code># Format: FROM repository[:version]
|
|
FROM ubuntu:latest
|
|
</code></pre>
|
|
|
|
<p>Continuing, we will declare the <code>MAINTAINER</code> of the <code>Dockerfile</code>:</p>
|
|
|
|
<pre><code># Format: MAINTAINER Name &lt;email@addr.ess&gt;
|
|
MAINTAINER M.Y. Name &lt;myname@addr.ess&gt;
|
|
</code></pre>
|
|
|
|
<blockquote>
|
|
<p><strong>Note:</strong> Although Ubuntu systems have MongoDB packages, they are likely to
|
|
be outdated. Therefore in this example, we will use the official MongoDB
|
|
packages.</p>
|
|
</blockquote>
|
|
|
|
<p>We will begin with importing the MongoDB public GPG key. We will also create
|
|
a MongoDB repository file for the package manager.</p>
|
|
|
|
<pre><code># Installation:
|
|
# Import MongoDB public GPG key AND create a MongoDB list file
|
|
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
|
|
RUN echo &quot;deb http://repo.mongodb.org/apt/ubuntu &quot;$(lsb_release -sc)&quot;/mongodb-org/3.0 multiverse&quot; | tee /etc/apt/sources.list.d/mongodb-org-3.0.list
|
|
</code></pre>
|
|
|
|
<p>After this initial preparation we can update our packages and install MongoDB.</p>
|
|
|
|
<pre><code># Update apt-get sources AND install MongoDB
|
|
RUN apt-get update &amp;&amp; apt-get install -y mongodb-org
|
|
</code></pre>
|
|
|
|
<blockquote>
|
|
<p><strong>Tip:</strong> You can install a specific version of MongoDB by using a list
|
|
of required packages with versions, e.g.:</p>
|
|
|
|
<pre><code>RUN apt-get update &amp;&amp; apt-get install -y mongodb-org=3.0.1 mongodb-org-server=3.0.1 mongodb-org-shell=3.0.1 mongodb-org-mongos=3.0.1 mongodb-org-tools=3.0.1
|
|
</code></pre>
|
|
</blockquote>
|
|
|
|
<p>MongoDB requires a data directory. Let&rsquo;s create it as the final step of our
|
|
installation instructions.</p>
|
|
|
|
<pre><code># Create the MongoDB data directory
|
|
RUN mkdir -p /data/db
|
|
</code></pre>
|
|
|
|
<p>Lastly we set the <code>ENTRYPOINT</code> which will tell Docker to run <code>mongod</code> inside
|
|
the containers launched from our MongoDB image. And for ports, we will use
|
|
the <code>EXPOSE</code> instruction.</p>
|
|
|
|
<pre><code># Expose port 27017 from the container to the host
|
|
EXPOSE 27017
|
|
|
|
# Set usr/bin/mongod as the dockerized entry-point application
|
|
ENTRYPOINT [&quot;/usr/bin/mongod&quot;]
|
|
</code></pre>
|
|
|
|
<p>Now save the file and let&rsquo;s build our image.</p>
|
|
|
|
<blockquote>
|
|
<p><strong>Note:</strong></p>
|
|
|
|
<p>The full version of this <code>Dockerfile</code> can be found <a href="http://localhost/examples/examples/examples/mongodb/Dockerfile">here</a>.</p>
|
|
</blockquote>
|
|
|
|
<h2 id="building-the-mongodb-docker-image">Building the MongoDB Docker image</h2>
|
|
|
|
<p>With our <code>Dockerfile</code>, we can now build the MongoDB image using Docker. Unless
|
|
experimenting, it is always a good practice to tag Docker images by passing the
|
|
<code>--tag</code> option to <code>docker build</code> command.</p>
|
|
|
|
<pre><code># Format: docker build --tag/-t &lt;user-name&gt;/&lt;repository&gt; .
|
|
# Example:
|
|
$ docker build --tag my/repo .
|
|
</code></pre>
|
|
|
|
<p>Once this command is issued, Docker will go through the <code>Dockerfile</code> and build
|
|
the image. The final image will be tagged <code>my/repo</code>.</p>
|
|
|
|
<h2 id="pushing-the-mongodb-image-to-docker-hub">Pushing the MongoDB image to Docker Hub</h2>
|
|
|
|
<p>All Docker image repositories can be hosted and shared on
|
|
<a href="https://hub.docker.com">Docker Hub</a> with the <code>docker push</code> command. For this,
|
|
you need to be logged-in.</p>
|
|
|
|
<pre><code># Log-in
|
|
$ docker login
|
|
Username:
|
|
..
|
|
|
|
# Push the image
|
|
# Format: docker push &lt;user-name&gt;/&lt;repository&gt;
|
|
$ docker push my/repo
|
|
The push refers to a repository [my/repo] (len: 1)
|
|
Sending image list
|
|
Pushing repository my/repo (1 tags)
|
|
..
|
|
</code></pre>
|
|
|
|
<h2 id="using-the-mongodb-image">Using the MongoDB image</h2>
|
|
|
|
<p>Using the MongoDB image we created, we can run one or more MongoDB instances
|
|
as daemon process(es).</p>
|
|
|
|
<pre><code># Basic way
|
|
# Usage: docker run --name &lt;name for container&gt; -d &lt;user-name&gt;/&lt;repository&gt;
|
|
$ docker run -p 27017:27017 --name mongo_instance_001 -d my/repo
|
|
|
|
# Dockerized MongoDB, lean and mean!
|
|
# Usage: docker run --name &lt;name for container&gt; -d &lt;user-name&gt;/&lt;repository&gt; --noprealloc --smallfiles
|
|
$ docker run -p 27017:27017 --name mongo_instance_001 -d my/repo --noprealloc --smallfiles
|
|
|
|
# Checking out the logs of a MongoDB container
|
|
# Usage: docker logs &lt;name for container&gt;
|
|
$ docker logs mongo_instance_001
|
|
|
|
# Playing with MongoDB
|
|
# Usage: mongo --port &lt;port you get from `docker ps`&gt;
|
|
$ mongo --port 27017
|
|
|
|
# If using boot2docker
|
|
# Usage: mongo --port &lt;port you get from `docker ps`&gt; --host &lt;ip address from `boot2docker ip`&gt;
|
|
$ mongo --port 27017 --host 192.168.59.103
|
|
</code></pre>
|
|
|
|
<blockquote>
|
|
<p><strong>Tip:</strong>
|
|
If you want to run two containers on the same engine, then you will need to map
|
|
the exposed port to two different ports on the host</p>
|
|
</blockquote>
|
|
|
|
<pre><code># Start two containers and map the ports
|
|
$ docker run -p 28001:27017 --name mongo_instance_001 -d my/repo
|
|
$ docker run -p 28002:27017 --name mongo_instance_002 -d my/repo
|
|
|
|
# Now you can connect to each MongoDB instance on the two ports
|
|
$ mongo --port 28001
|
|
$ mongo --port 28002
|
|
</code></pre>
|
|
|
|
<ul>
|
|
<li><a href="http://localhost/examples/examples/userguide/dockerlinks">Linking containers</a></li>
|
|
<li><a href="http://localhost/examples/examples/articles/ambassador_pattern_linking/">Cross-host linking containers</a></li>
|
|
<li><a href="http://localhost/examples/examples/docker-io/builds/#automated-builds">Creating an Automated Build</a></li>
|
|
</ul>
|
|
</description>
|
|
</item>
|
|
|
|
<item>
|
|
<title>Dockerizing PostgreSQL</title>
|
|
<link>http://localhost/examples/postgresql_service/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>http://localhost/examples/postgresql_service/</guid>
|
|
<description>
|
|
|
|
<h1 id="dockerizing-postgresql">Dockerizing PostgreSQL</h1>
|
|
|
|
<blockquote>
|
|
<p><strong>Note</strong>:
|
|
- <strong>If you don&rsquo;t like sudo</strong> then see <a href="http://localhost/examples/examples/installation/binaries/#giving-non-root-access"><em>Giving non-root
|
|
access</em></a></p>
|
|
</blockquote>
|
|
|
|
<h2 id="installing-postgresql-on-docker">Installing PostgreSQL on Docker</h2>
|
|
|
|
<p>Assuming there is no Docker image that suits your needs on the <a href="http://hub.docker.com">Docker
|
|
Hub</a>, you can create one yourself.</p>
|
|
|
|
<p>Start by creating a new <code>Dockerfile</code>:</p>
|
|
|
|
<blockquote>
|
|
<p><strong>Note</strong>:
|
|
This PostgreSQL setup is for development-only purposes. Refer to the
|
|
PostgreSQL documentation to fine-tune these settings so that it is
|
|
suitably secure.</p>
|
|
</blockquote>
|
|
|
|
<pre><code>#
|
|
# example Dockerfile for https://docs.docker.com/examples/postgresql_service/
|
|
#
|
|
|
|
FROM ubuntu
|
|
MAINTAINER SvenDowideit@docker.com
|
|
|
|
# Add the PostgreSQL PGP key to verify their Debian packages.
|
|
# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
|
|
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
|
|
|
|
# Add PostgreSQL's repository. It contains the most recent stable release
|
|
# of PostgreSQL, ``9.3``.
|
|
RUN echo &quot;deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main&quot; &gt; /etc/apt/sources.list.d/pgdg.list
|
|
|
|
# Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3
|
|
# There are some warnings (in red) that show up during the build. You can hide
|
|
# them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
|
|
RUN apt-get update &amp;&amp; apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
|
|
|
|
# Note: The official Debian and Ubuntu images automatically ``apt-get clean``
|
|
# after each ``apt-get``
|
|
|
|
# Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
|
|
USER postgres
|
|
|
|
# Create a PostgreSQL role named ``docker`` with ``docker`` as the password and
|
|
# then create a database `docker` owned by the ``docker`` role.
|
|
# Note: here we use ``&amp;&amp;\`` to run commands one after the other - the ``\``
|
|
# allows the RUN command to span multiple lines.
|
|
RUN /etc/init.d/postgresql start &amp;&amp;\
|
|
psql --command &quot;CREATE USER docker WITH SUPERUSER PASSWORD 'docker';&quot; &amp;&amp;\
|
|
createdb -O docker docker
|
|
|
|
# Adjust PostgreSQL configuration so that remote connections to the
|
|
# database are possible.
|
|
RUN echo &quot;host all all 0.0.0.0/0 md5&quot; &gt;&gt; /etc/postgresql/9.3/main/pg_hba.conf
|
|
|
|
# And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
|
|
RUN echo &quot;listen_addresses='*'&quot; &gt;&gt; /etc/postgresql/9.3/main/postgresql.conf
|
|
|
|
# Expose the PostgreSQL port
|
|
EXPOSE 5432
|
|
|
|
# Add VOLUMEs to allow backup of config, logs and databases
|
|
VOLUME [&quot;/etc/postgresql&quot;, &quot;/var/log/postgresql&quot;, &quot;/var/lib/postgresql&quot;]
|
|
|
|
# Set the default command to run when starting the container
|
|
CMD [&quot;/usr/lib/postgresql/9.3/bin/postgres&quot;, &quot;-D&quot;, &quot;/var/lib/postgresql/9.3/main&quot;, &quot;-c&quot;, &quot;config_file=/etc/postgresql/9.3/main/postgresql.conf&quot;]
|
|
</code></pre>
|
|
|
|
<p>Build an image from the Dockerfile assign it a name.</p>
|
|
|
|
<pre><code>$ docker build -t eg_postgresql .
|
|
</code></pre>
|
|
|
|
<p>And run the PostgreSQL server container (in the foreground):</p>
|
|
|
|
<pre><code>$ docker run --rm -P --name pg_test eg_postgresql
|
|
</code></pre>
|
|
|
|
<p>There are 2 ways to connect to the PostgreSQL server. We can use <a href="http://localhost/examples/examples/userguide/dockerlinks"><em>Link
|
|
Containers</em></a>, or we can access it from our host
|
|
(or the network).</p>
|
|
|
|
<blockquote>
|
|
<p><strong>Note</strong>:
|
|
The <code>--rm</code> removes the container and its image when
|
|
the container exits successfully.</p>
|
|
</blockquote>
|
|
|
|
<h3 id="using-container-linking">Using container linking</h3>
|
|
|
|
<p>Containers can be linked to another container&rsquo;s ports directly using
|
|
<code>-link remote_name:local_alias</code> in the client&rsquo;s
|
|
<code>docker run</code>. This will set a number of environment
|
|
variables that can then be used to connect:</p>
|
|
|
|
<pre><code>$ docker run --rm -t -i --link pg_test:pg eg_postgresql bash
|
|
|
|
postgres@7ef98b1b7243:/$ psql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d docker -U docker --password
|
|
</code></pre>
|
|
|
|
<h3 id="connecting-from-your-host-system">Connecting from your host system</h3>
|
|
|
|
<p>Assuming you have the postgresql-client installed, you can use the
|
|
host-mapped port to test as well. You need to use <code>docker ps</code>
|
|
to find out what local host port the container is mapped to
|
|
first:</p>
|
|
|
|
<pre><code>$ docker ps
|
|
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
|
5e24362f27f6 eg_postgresql:latest /usr/lib/postgresql/ About an hour ago Up About an hour 0.0.0.0:49153-&gt;5432/tcp pg_test
|
|
$ psql -h localhost -p 49153 -d docker -U docker --password
|
|
</code></pre>
|
|
|
|
<h3 id="testing-the-database">Testing the database</h3>
|
|
|
|
<p>Once you have authenticated and have a <code>docker =#</code>
|
|
prompt, you can create a table and populate it.</p>
|
|
|
|
<pre><code>psql (9.3.1)
|
|
Type &quot;help&quot; for help.
|
|
|
|
$ docker=# CREATE TABLE cities (
|
|
docker(# name varchar(80),
|
|
docker(# location point
|
|
docker(# );
|
|
CREATE TABLE
|
|
$ docker=# INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
|
|
INSERT 0 1
|
|
$ docker=# select * from cities;
|
|
name | location
|
|
---------------+-----------
|
|
San Francisco | (-194,53)
|
|
(1 row)
|
|
</code></pre>
|
|
|
|
<h3 id="using-the-container-volumes">Using the container volumes</h3>
|
|
|
|
<p>You can use the defined volumes to inspect the PostgreSQL log files and
|
|
to backup your configuration and data:</p>
|
|
|
|
<pre><code>$ docker run --rm --volumes-from pg_test -t -i busybox sh
|
|
|
|
/ # ls
|
|
bin etc lib linuxrc mnt proc run sys usr
|
|
dev home lib64 media opt root sbin tmp var
|
|
/ # ls /etc/postgresql/9.3/main/
|
|
environment pg_hba.conf postgresql.conf
|
|
pg_ctl.conf pg_ident.conf start.conf
|
|
/tmp # ls /var/log
|
|
ldconfig postgresql
|
|
</code></pre>
|
|
</description>
|
|
</item>
|
|
|
|
<item>
|
|
<title>Dockerizing a CouchDB service</title>
|
|
<link>http://localhost/examples/couchdb_data_volumes/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>http://localhost/examples/couchdb_data_volumes/</guid>
|
|
<description>
|
|
|
|
<h1 id="dockerizing-a-couchdb-service">Dockerizing a CouchDB service</h1>
|
|
|
|
<blockquote>
|
|
<p><strong>Note</strong>:
|
|
- <strong>If you don&rsquo;t like sudo</strong> then see <a href="http://localhost/examples/examples/installation/binaries/#giving-non-root-access"><em>Giving non-root
|
|
access</em></a></p>
|
|
</blockquote>
|
|
|
|
<p>Here&rsquo;s an example of using data volumes to share the same data between
|
|
two CouchDB containers. This could be used for hot upgrades, testing
|
|
different versions of CouchDB on the same data, etc.</p>
|
|
|
|
<h2 id="create-first-database">Create first database</h2>
|
|
|
|
<p>Note that we&rsquo;re marking <code>/var/lib/couchdb</code> as a data volume.</p>
|
|
|
|
<pre><code>$ COUCH1=$(docker run -d -p 5984 -v /var/lib/couchdb shykes/couchdb:2013-05-03)
|
|
</code></pre>
|
|
|
|
<h2 id="add-data-to-the-first-database">Add data to the first database</h2>
|
|
|
|
<p>We&rsquo;re assuming your Docker host is reachable at <code>localhost</code>. If not,
|
|
replace <code>localhost</code> with the public IP of your Docker host.</p>
|
|
|
|
<pre><code>$ HOST=localhost
|
|
$ URL=&quot;http://$HOST:$(docker port $COUCH1 5984 | grep -o '[1-9][0-9]*$')/_utils/&quot;
|
|
$ echo &quot;Navigate to $URL in your browser, and use the couch interface to add data&quot;
|
|
</code></pre>
|
|
|
|
<h2 id="create-second-database">Create second database</h2>
|
|
|
|
<p>This time, we&rsquo;re requesting shared access to <code>$COUCH1</code>&rsquo;s volumes.</p>
|
|
|
|
<pre><code>$ COUCH2=$(docker run -d -p 5984 --volumes-from $COUCH1 shykes/couchdb:2013-05-03)
|
|
</code></pre>
|
|
|
|
<h2 id="browse-data-on-the-second-database">Browse data on the second database</h2>
|
|
|
|
<pre><code>$ HOST=localhost
|
|
$ URL=&quot;http://$HOST:$(docker port $COUCH2 5984 | grep -o '[1-9][0-9]*$')/_utils/&quot;
|
|
$ echo &quot;Navigate to $URL in your browser. You should see the same data as in the first database&quot;'!'
|
|
</code></pre>
|
|
|
|
<p>Congratulations, you are now running two Couchdb containers, completely
|
|
isolated from each other <em>except</em> for their data.</p>
|
|
</description>
|
|
</item>
|
|
|
|
<item>
|
|
<title>Dockerizing a Node.js web app</title>
|
|
<link>http://localhost/examples/nodejs_web_app/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>http://localhost/examples/nodejs_web_app/</guid>
|
|
<description>
|
|
|
|
<h1 id="dockerizing-a-node-js-web-app">Dockerizing a Node.js web app</h1>
|
|
|
|
<blockquote>
|
|
<p><strong>Note</strong>:
|
|
- <strong>If you don&rsquo;t like sudo</strong> then see <a href="http://localhost/examples/examples/installation/binaries/#giving-non-root-access"><em>Giving non-root
|
|
access</em></a></p>
|
|
</blockquote>
|
|
|
|
<p>The goal of this example is to show you how you can build your own
|
|
Docker images from a parent image using a <code>Dockerfile</code>
|
|
. We will do that by making a simple Node.js hello world web
|
|
application running on CentOS. You can get the full source code at
|
|
<a href="https://github.com/enokd/docker-node-hello/">https://github.com/enokd/docker-node-hello/</a>.</p>
|
|
|
|
<h2 id="create-node-js-app">Create Node.js app</h2>
|
|
|
|
<p>First, create a directory <code>src</code> where all the files
|
|
would live. Then create a <code>package.json</code> file that
|
|
describes your app and its dependencies:</p>
|
|
|
|
<pre><code>{
|
|
&quot;name&quot;: &quot;docker-centos-hello&quot;,
|
|
&quot;private&quot;: true,
|
|
&quot;version&quot;: &quot;0.0.1&quot;,
|
|
&quot;description&quot;: &quot;Node.js Hello world app on CentOS using docker&quot;,
|
|
&quot;author&quot;: &quot;Daniel Gasienica &lt;daniel@gasienica.ch&gt;&quot;,
|
|
&quot;dependencies&quot;: {
|
|
&quot;express&quot;: &quot;3.2.4&quot;
|
|
}
|
|
}
|
|
</code></pre>
|
|
|
|
<p>Then, create an <code>index.js</code> file that defines a web
|
|
app using the <a href="http://expressjs.com/">Express.js</a> framework:</p>
|
|
|
|
<pre><code>var express = require('express');
|
|
|
|
// Constants
|
|
var PORT = 8080;
|
|
|
|
// App
|
|
var app = express();
|
|
app.get('/', function (req, res) {
|
|
res.send('Hello world\n');
|
|
});
|
|
|
|
app.listen(PORT);
|
|
console.log('Running on http://localhost:' + PORT);
|
|
</code></pre>
|
|
|
|
<p>In the next steps, we&rsquo;ll look at how you can run this app inside a
|
|
CentOS container using Docker. First, you&rsquo;ll need to build a Docker
|
|
image of your app.</p>
|
|
|
|
<h2 id="creating-a-dockerfile">Creating a Dockerfile</h2>
|
|
|
|
<p>Create an empty file called <code>Dockerfile</code>:</p>
|
|
|
|
<pre><code>touch Dockerfile
|
|
</code></pre>
|
|
|
|
<p>Open the <code>Dockerfile</code> in your favorite text editor</p>
|
|
|
|
<p>Define the parent image you want to use to build your own image on
|
|
top of. Here, we&rsquo;ll use
|
|
<a href="https://registry.hub.docker.com/_/centos/">CentOS</a> (tag: <code>centos6</code>)
|
|
available on the <a href="https://hub.docker.com/">Docker Hub</a>:</p>
|
|
|
|
<pre><code>FROM centos:centos6
|
|
</code></pre>
|
|
|
|
<p>Since we&rsquo;re building a Node.js app, you&rsquo;ll have to install Node.js as
|
|
well as npm on your CentOS image. Node.js is required to run your app
|
|
and npm to install your app&rsquo;s dependencies defined in
|
|
<code>package.json</code>. To install the right package for
|
|
CentOS, we&rsquo;ll use the instructions from the <a href="https://github.com/joyent/node/wiki/Installing-Node.js-
|
|
via-package-manager#rhelcentosscientific-linux-6">Node.js wiki</a>:</p>
|
|
|
|
<pre><code># Enable EPEL for Node.js
|
|
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
|
|
# Install Node.js and npm
|
|
RUN yum install -y npm
|
|
</code></pre>
|
|
|
|
<p>To bundle your app&rsquo;s source code inside the Docker image, use the <code>COPY</code>
|
|
instruction:</p>
|
|
|
|
<pre><code># Bundle app source
|
|
COPY . /src
|
|
</code></pre>
|
|
|
|
<p>Install your app dependencies using the <code>npm</code> binary:</p>
|
|
|
|
<pre><code># Install app dependencies
|
|
RUN cd /src; npm install
|
|
</code></pre>
|
|
|
|
<p>Your app binds to port <code>8080</code> so you&rsquo;ll use the<code>EXPOSE</code> instruction to have
|
|
it mapped by the <code>docker</code> daemon:</p>
|
|
|
|
<pre><code>EXPOSE 8080
|
|
</code></pre>
|
|
|
|
<p>Last but not least, define the command to run your app using <code>CMD</code> which
|
|
defines your runtime, i.e. <code>node</code>, and the path to our app, i.e. <code>src/index.js</code>
|
|
(see the step where we added the source to the container):</p>
|
|
|
|
<pre><code>CMD [&quot;node&quot;, &quot;/src/index.js&quot;]
|
|
</code></pre>
|
|
|
|
<p>Your <code>Dockerfile</code> should now look like this:</p>
|
|
|
|
<pre><code>FROM centos:centos6
|
|
|
|
# Enable EPEL for Node.js
|
|
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
|
|
# Install Node.js and npm
|
|
RUN yum install -y npm
|
|
|
|
# Bundle app source
|
|
COPY . /src
|
|
# Install app dependencies
|
|
RUN cd /src; npm install
|
|
|
|
EXPOSE 8080
|
|
CMD [&quot;node&quot;, &quot;/src/index.js&quot;]
|
|
</code></pre>
|
|
|
|
<h2 id="building-your-image">Building your image</h2>
|
|
|
|
<p>Go to the directory that has your <code>Dockerfile</code> and run the following command
|
|
to build a Docker image. The <code>-t</code> flag lets you tag your image so it&rsquo;s easier
|
|
to find later using the <code>docker images</code> command:</p>
|
|
|
|
<pre><code>$ docker build -t &lt;your username&gt;/centos-node-hello .
|
|
</code></pre>
|
|
|
|
<p>Your image will now be listed by Docker:</p>
|
|
|
|
<pre><code>$ docker images
|
|
|
|
# Example
|
|
REPOSITORY TAG ID CREATED
|
|
centos centos6 539c0211cd76 8 weeks ago
|
|
&lt;your username&gt;/centos-node-hello latest d64d3505b0d2 2 hours ago
|
|
</code></pre>
|
|
|
|
<h2 id="run-the-image">Run the image</h2>
|
|
|
|
<p>Running your image with <code>-d</code> runs the container in detached mode, leaving the
|
|
container running in the background. The <code>-p</code> flag redirects a public port to
|
|
a private port in the container. Run the image you previously built:</p>
|
|
|
|
<pre><code>$ docker run -p 49160:8080 -d &lt;your username&gt;/centos-node-hello
|
|
</code></pre>
|
|
|
|
<p>Print the output of your app:</p>
|
|
|
|
<pre><code># Get container ID
|
|
$ docker ps
|
|
|
|
# Print app output
|
|
$ docker logs &lt;container id&gt;
|
|
|
|
# Example
|
|
Running on http://localhost:8080
|
|
</code></pre>
|
|
|
|
<h2 id="test">Test</h2>
|
|
|
|
<p>To test your app, get the port of your app that Docker mapped:</p>
|
|
|
|
<pre><code>$ docker ps
|
|
|
|
# Example
|
|
ID IMAGE COMMAND ... PORTS
|
|
ecce33b30ebf &lt;your username&gt;/centos-node-hello:latest node /src/index.js 49160-&gt;8080
|
|
</code></pre>
|
|
|
|
<p>In the example above, Docker mapped the <code>8080</code> port of the container to <code>49160</code>.</p>
|
|
|
|
<p>Now you can call your app using <code>curl</code> (install if needed via:
|
|
<code>sudo apt-get install curl</code>):</p>
|
|
|
|
<pre><code>$ curl -i localhost:49160
|
|
|
|
HTTP/1.1 200 OK
|
|
X-Powered-By: Express
|
|
Content-Type: text/html; charset=utf-8
|
|
Content-Length: 12
|
|
Date: Sun, 02 Jun 2013 03:53:22 GMT
|
|
Connection: keep-alive
|
|
|
|
Hello world
|
|
</code></pre>
|
|
|
|
<p>If you use Boot2docker on OS X, the port is actually mapped to the Docker host VM,
|
|
and you should use the following command:</p>
|
|
|
|
<pre><code>$ curl $(boot2docker ip):49160
|
|
</code></pre>
|
|
|
|
<p>We hope this tutorial helped you get up and running with Node.js and
|
|
CentOS on Docker. You can get the full source code at
|
|
<a href="https://github.com/enokd/docker-node-hello/">https://github.com/enokd/docker-node-hello/</a>.</p>
|
|
</description>
|
|
</item>
|
|
|
|
<item>
|
|
<title>Dockerizing a Redis service</title>
|
|
<link>http://localhost/examples/running_redis_service/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>http://localhost/examples/running_redis_service/</guid>
|
|
<description>
|
|
|
|
<h1 id="dockerizing-a-redis-service">Dockerizing a Redis service</h1>
|
|
|
|
<p>Very simple, no frills, Redis service attached to a web application
|
|
using a link.</p>
|
|
|
|
<h2 id="create-a-docker-container-for-redis">Create a Docker container for Redis</h2>
|
|
|
|
<p>Firstly, we create a <code>Dockerfile</code> for our new Redis
|
|
image.</p>
|
|
|
|
<pre><code>FROM ubuntu:14.04
|
|
RUN apt-get update &amp;&amp; apt-get install -y redis-server
|
|
EXPOSE 6379
|
|
ENTRYPOINT [&quot;/usr/bin/redis-server&quot;]
|
|
</code></pre>
|
|
|
|
<p>Next we build an image from our <code>Dockerfile</code>.
|
|
Replace <code>&lt;your username&gt;</code> with your own user name.</p>
|
|
|
|
<pre><code>$ docker build -t &lt;your username&gt;/redis .
|
|
</code></pre>
|
|
|
|
<h2 id="run-the-service">Run the service</h2>
|
|
|
|
<p>Use the image we&rsquo;ve just created and name your container <code>redis</code>.</p>
|
|
|
|
<p>Running the service with <code>-d</code> runs the container in detached mode, leaving
|
|
the container running in the background.</p>
|
|
|
|
<p>Importantly, we&rsquo;re not exposing any ports on our container. Instead
|
|
we&rsquo;re going to use a container link to provide access to our Redis
|
|
database.</p>
|
|
|
|
<pre><code>$ docker run --name redis -d &lt;your username&gt;/redis
|
|
</code></pre>
|
|
|
|
<h2 id="create-your-web-application-container">Create your web application container</h2>
|
|
|
|
<p>Next we can create a container for our application. We&rsquo;re going to use
|
|
the <code>-link</code> flag to create a link to the <code>redis</code> container we&rsquo;ve just
|
|
created with an alias of <code>db</code>. This will create a secure tunnel to the
|
|
<code>redis</code> container and expose the Redis instance running inside that
|
|
container to only this container.</p>
|
|
|
|
<pre><code>$ docker run --link redis:db -i -t ubuntu:14.04 /bin/bash
|
|
</code></pre>
|
|
|
|
<p>Once inside our freshly created container we need to install Redis to
|
|
get the <code>redis-cli</code> binary to test our connection.</p>
|
|
|
|
<pre><code>$ sudo apt-get update
|
|
$ sudo apt-get install redis-server
|
|
$ sudo service redis-server stop
|
|
</code></pre>
|
|
|
|
<p>As we&rsquo;ve used the <code>--link redis:db</code> option, Docker
|
|
has created some environment variables in our web application container.</p>
|
|
|
|
<pre><code>$ env | grep DB_
|
|
|
|
# Should return something similar to this with your values
|
|
DB_NAME=/violet_wolf/db
|
|
DB_PORT_6379_TCP_PORT=6379
|
|
DB_PORT=tcp://172.17.0.33:6379
|
|
DB_PORT_6379_TCP=tcp://172.17.0.33:6379
|
|
DB_PORT_6379_TCP_ADDR=172.17.0.33
|
|
DB_PORT_6379_TCP_PROTO=tcp
|
|
</code></pre>
|
|
|
|
<p>We can see that we&rsquo;ve got a small list of environment variables prefixed
|
|
with <code>DB</code>. The <code>DB</code> comes from the link alias specified when we launched
|
|
the container. Let&rsquo;s use the <code>DB_PORT_6379_TCP_ADDR</code> variable to connect to
|
|
our Redis container.</p>
|
|
|
|
<pre><code>$ redis-cli -h $DB_PORT_6379_TCP_ADDR
|
|
$ redis 172.17.0.33:6379&gt;
|
|
$ redis 172.17.0.33:6379&gt; set docker awesome
|
|
OK
|
|
$ redis 172.17.0.33:6379&gt; get docker
|
|
&quot;awesome&quot;
|
|
$ redis 172.17.0.33:6379&gt; exit
|
|
</code></pre>
|
|
|
|
<p>We could easily use this or other environment variables in our web
|
|
application to make a connection to our <code>redis</code>
|
|
container.</p>
|
|
</description>
|
|
</item>
|
|
|
|
<item>
|
|
<title>Dockerizing a Riak service</title>
|
|
<link>http://localhost/examples/running_riak_service/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>http://localhost/examples/running_riak_service/</guid>
|
|
<description>
|
|
|
|
<h1 id="dockerizing-a-riak-service">Dockerizing a Riak service</h1>
|
|
|
|
<p>The goal of this example is to show you how to build a Docker image with
|
|
Riak pre-installed.</p>
|
|
|
|
<h2 id="creating-a-dockerfile">Creating a Dockerfile</h2>
|
|
|
|
<p>Create an empty file called <code>Dockerfile</code>:</p>
|
|
|
|
<pre><code>$ touch Dockerfile
|
|
</code></pre>
|
|
|
|
<p>Next, define the parent image you want to use to build your image on top
|
|
of. We&rsquo;ll use <a href="https://registry.hub.docker.com/_/ubuntu/">Ubuntu</a> (tag:
|
|
<code>trusty</code>), which is available on <a href="https://hub.docker.com">Docker Hub</a>:</p>
|
|
|
|
<pre><code># Riak
|
|
#
|
|
# VERSION 0.1.1
|
|
|
|
# Use the Ubuntu base image provided by dotCloud
|
|
FROM ubuntu:trusty
|
|
MAINTAINER Hector Castro hector@basho.com
|
|
</code></pre>
|
|
|
|
<p>After that, we install the curl which is used to download the repository setup
|
|
script and we download the setup script and run it.</p>
|
|
|
|
<pre><code># Install Riak repository before we do apt-get update, so that update happens
|
|
# in a single step
|
|
RUN apt-get install -q -y curl &amp;&amp; \
|
|
curl -sSL https://packagecloud.io/install/repositories/basho/riak/script.deb | sudo bash
|
|
</code></pre>
|
|
|
|
<p>Then we install and setup a few dependencies:</p>
|
|
|
|
<ul>
|
|
<li><code>supervisor</code> is used manage the Riak processes</li>
|
|
<li><code>riak=2.0.5-1</code> is the Riak package coded to version 2.0.5</li>
|
|
</ul>
|
|
|
|
<!-- -->
|
|
|
|
<pre><code># Install and setup project dependencies
|
|
RUN apt-get update &amp;&amp; \
|
|
apt-get install -y supervisor riak=2.0.5-1
|
|
|
|
RUN mkdir -p /var/log/supervisor
|
|
|
|
RUN locale-gen en_US en_US.UTF-8
|
|
|
|
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
|
|
</code></pre>
|
|
|
|
<p>After that, we modify Riak&rsquo;s configuration:</p>
|
|
|
|
<pre><code># Configure Riak to accept connections from any host
|
|
RUN sed -i &quot;s|listener.http.internal = 127.0.0.1:8098|listener.http.internal = 0.0.0.0:8098|&quot; /etc/riak/riak.conf
|
|
RUN sed -i &quot;s|listener.protobuf.internal = 127.0.0.1:8087|listener.protobuf.internal = 0.0.0.0:8087|&quot; /etc/riak/riak.conf
|
|
</code></pre>
|
|
|
|
<p>Then, we expose the Riak Protocol Buffers and HTTP interfaces:</p>
|
|
|
|
<pre><code># Expose Riak Protocol Buffers and HTTP interfaces
|
|
EXPOSE 8087 8098
|
|
</code></pre>
|
|
|
|
<p>Finally, run <code>supervisord</code> so that Riak is started:</p>
|
|
|
|
<pre><code>CMD [&quot;/usr/bin/supervisord&quot;]
|
|
</code></pre>
|
|
|
|
<h2 id="create-a-supervisord-configuration-file">Create a supervisord configuration file</h2>
|
|
|
|
<p>Create an empty file called <code>supervisord.conf</code>. Make
|
|
sure it&rsquo;s at the same directory level as your <code>Dockerfile</code>:</p>
|
|
|
|
<pre><code>touch supervisord.conf
|
|
</code></pre>
|
|
|
|
<p>Populate it with the following program definitions:</p>
|
|
|
|
<pre><code>[supervisord]
|
|
nodaemon=true
|
|
|
|
[program:riak]
|
|
command=bash -c &quot;/usr/sbin/riak console&quot;
|
|
numprocs=1
|
|
autostart=true
|
|
autorestart=true
|
|
user=riak
|
|
environment=HOME=&quot;/var/lib/riak&quot;
|
|
stdout_logfile=/var/log/supervisor/%(program_name)s.log
|
|
stderr_logfile=/var/log/supervisor/%(program_name)s.log
|
|
</code></pre>
|
|
|
|
<h2 id="build-the-docker-image-for-riak">Build the Docker image for Riak</h2>
|
|
|
|
<p>Now you should be able to build a Docker image for Riak:</p>
|
|
|
|
<pre><code>$ docker build -t &quot;&lt;yourname&gt;/riak&quot; .
|
|
</code></pre>
|
|
|
|
<h2 id="next-steps">Next steps</h2>
|
|
|
|
<p>Riak is a distributed database. Many production deployments consist of
|
|
<a href="http://basho.com/why-your-riak-cluster-should-have-at-least-five-nodes/">at least five nodes</a>.
|
|
See the <a href="https://github.com/hectcastro/docker-riak">docker-riak</a> project
|
|
details on how to deploy a Riak cluster using Docker and Pipework.</p>
|
|
</description>
|
|
</item>
|
|
|
|
<item>
|
|
<title>Dockerizing an SSH service</title>
|
|
<link>http://localhost/examples/running_ssh_service/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>http://localhost/examples/running_ssh_service/</guid>
|
|
<description>
|
|
|
|
<h1 id="dockerizing-an-ssh-daemon-service">Dockerizing an SSH daemon service</h1>
|
|
|
|
<h2 id="build-an-eg-sshd-image">Build an <code>eg_sshd</code> image</h2>
|
|
|
|
<p>The following <code>Dockerfile</code> sets up an SSHd service in a container that you
|
|
can use to connect to and inspect other container&rsquo;s volumes, or to get
|
|
quick access to a test container.</p>
|
|
|
|
<pre><code># sshd
|
|
#
|
|
# VERSION 0.0.2
|
|
|
|
FROM ubuntu:14.04
|
|
MAINTAINER Sven Dowideit &lt;SvenDowideit@docker.com&gt;
|
|
|
|
RUN apt-get update &amp;&amp; apt-get install -y openssh-server
|
|
RUN mkdir /var/run/sshd
|
|
RUN echo 'root:screencast' | chpasswd
|
|
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
|
|
|
|
# SSH login fix. Otherwise user is kicked off after login
|
|
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
|
|
|
|
ENV NOTVISIBLE &quot;in users profile&quot;
|
|
RUN echo &quot;export VISIBLE=now&quot; &gt;&gt; /etc/profile
|
|
|
|
EXPOSE 22
|
|
CMD [&quot;/usr/sbin/sshd&quot;, &quot;-D&quot;]
|
|
</code></pre>
|
|
|
|
<p>Build the image using:</p>
|
|
|
|
<pre><code>$ docker build -t eg_sshd .
|
|
</code></pre>
|
|
|
|
<h2 id="run-a-test-sshd-container">Run a <code>test_sshd</code> container</h2>
|
|
|
|
<p>Then run it. You can then use <code>docker port</code> to find out what host port
|
|
the container&rsquo;s port 22 is mapped to:</p>
|
|
|
|
<pre><code>$ docker run -d -P --name test_sshd eg_sshd
|
|
$ docker port test_sshd 22
|
|
0.0.0.0:49154
|
|
</code></pre>
|
|
|
|
<p>And now you can ssh as <code>root</code> on the container&rsquo;s IP address (you can find it
|
|
with <code>docker inspect</code>) or on port <code>49154</code> of the Docker daemon&rsquo;s host IP address
|
|
(<code>ip address</code> or <code>ifconfig</code> can tell you that) or <code>localhost</code> if on the
|
|
Docker daemon host:</p>
|
|
|
|
<pre><code>$ ssh root@192.168.1.2 -p 49154
|
|
# The password is ``screencast``.
|
|
$$
|
|
</code></pre>
|
|
|
|
<h2 id="environment-variables">Environment variables</h2>
|
|
|
|
<p>Using the <code>sshd</code> daemon to spawn shells makes it complicated to pass environment
|
|
variables to the user&rsquo;s shell via the normal Docker mechanisms, as <code>sshd</code> scrubs
|
|
the environment before it starts the shell.</p>
|
|
|
|
<p>If you&rsquo;re setting values in the <code>Dockerfile</code> using <code>ENV</code>, you&rsquo;ll need to push them
|
|
to a shell initialization file like the <code>/etc/profile</code> example in the <code>Dockerfile</code>
|
|
above.</p>
|
|
|
|
<p>If you need to pass<code>docker run -e ENV=value</code> values, you will need to write a
|
|
short script to do the same before you start <code>sshd -D</code> and then replace the
|
|
<code>CMD</code> with that script.</p>
|
|
|
|
<h2 id="clean-up">Clean up</h2>
|
|
|
|
<p>Finally, clean up after your test by stopping and removing the
|
|
container, and then removing the image.</p>
|
|
|
|
<pre><code>$ docker stop test_sshd
|
|
$ docker rm test_sshd
|
|
$ docker rmi eg_sshd
|
|
</code></pre>
|
|
</description>
|
|
</item>
|
|
|
|
<item>
|
|
<title>Dockerizing an apt-cacher-ng service</title>
|
|
<link>http://localhost/examples/apt-cacher-ng/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>http://localhost/examples/apt-cacher-ng/</guid>
|
|
<description>
|
|
|
|
<h1 id="dockerizing-an-apt-cacher-ng-service">Dockerizing an apt-cacher-ng service</h1>
|
|
|
|
<blockquote>
|
|
<p><strong>Note</strong>:
|
|
- <strong>If you don&rsquo;t like sudo</strong> then see <a href="http://localhost/examples/examples/installation/binaries/#giving-non-root-access"><em>Giving non-root
|
|
access</em></a>.
|
|
- <strong>If you&rsquo;re using OS X or docker via TCP</strong> then you shouldn&rsquo;t use
|
|
sudo.</p>
|
|
</blockquote>
|
|
|
|
<p>When you have multiple Docker servers, or build unrelated Docker
|
|
containers which can&rsquo;t make use of the Docker build cache, it can be
|
|
useful to have a caching proxy for your packages. This container makes
|
|
the second download of any package almost instant.</p>
|
|
|
|
<p>Use the following Dockerfile:</p>
|
|
|
|
<pre><code>#
|
|
# Build: docker build -t apt-cacher .
|
|
# Run: docker run -d -p 3142:3142 --name apt-cacher-run apt-cacher
|
|
#
|
|
# and then you can run containers with:
|
|
# docker run -t -i --rm -e http_proxy http://dockerhost:3142/ debian bash
|
|
#
|
|
FROM ubuntu
|
|
MAINTAINER SvenDowideit@docker.com
|
|
|
|
VOLUME [&quot;/var/cache/apt-cacher-ng&quot;]
|
|
RUN apt-get update &amp;&amp; apt-get install -y apt-cacher-ng
|
|
|
|
EXPOSE 3142
|
|
CMD chmod 777 /var/cache/apt-cacher-ng &amp;&amp; /etc/init.d/apt-cacher-ng start &amp;&amp; tail -f /var/log/apt-cacher-ng/*
|
|
</code></pre>
|
|
|
|
<p>To build the image using:</p>
|
|
|
|
<pre><code>$ docker build -t eg_apt_cacher_ng .
|
|
</code></pre>
|
|
|
|
<p>Then run it, mapping the exposed port to one on the host</p>
|
|
|
|
<pre><code>$ docker run -d -p 3142:3142 --name test_apt_cacher_ng eg_apt_cacher_ng
|
|
</code></pre>
|
|
|
|
<p>To see the logfiles that are <code>tailed</code> in the default command, you can
|
|
use:</p>
|
|
|
|
<pre><code>$ docker logs -f test_apt_cacher_ng
|
|
</code></pre>
|
|
|
|
<p>To get your Debian-based containers to use the proxy, you can do one of
|
|
three things</p>
|
|
|
|
<ol>
|
|
<li>Add an apt Proxy setting
|
|
<code>echo 'Acquire::http { Proxy &quot;http://dockerhost:3142&quot;; };' &gt;&gt; /etc/apt/conf.d/01proxy</code></li>
|
|
<li>Set an environment variable:
|
|
<code>http_proxy=http://dockerhost:3142/</code></li>
|
|
<li>Change your <code>sources.list</code> entries to start with
|
|
<code>http://dockerhost:3142/</code></li>
|
|
</ol>
|
|
|
|
<p><strong>Option 1</strong> injects the settings safely into your apt configuration in
|
|
a local version of a common base:</p>
|
|
|
|
<pre><code>FROM ubuntu
|
|
RUN echo 'Acquire::http { Proxy &quot;http://dockerhost:3142&quot;; };' &gt;&gt; /etc/apt/apt.conf.d/01proxy
|
|
RUN apt-get update &amp;&amp; apt-get install -y vim git
|
|
|
|
# docker build -t my_ubuntu .
|
|
</code></pre>
|
|
|
|
<p><strong>Option 2</strong> is good for testing, but will break other HTTP clients
|
|
which obey <code>http_proxy</code>, such as <code>curl</code>, <code>wget</code> and others:</p>
|
|
|
|
<pre><code>$ docker run --rm -t -i -e http_proxy=http://dockerhost:3142/ debian bash
|
|
</code></pre>
|
|
|
|
<p><strong>Option 3</strong> is the least portable, but there will be times when you
|
|
might need to do it and you can do it from your <code>Dockerfile</code>
|
|
too.</p>
|
|
|
|
<p>Apt-cacher-ng has some tools that allow you to manage the repository,
|
|
and they can be used by leveraging the <code>VOLUME</code>
|
|
instruction, and the image we built to run the service:</p>
|
|
|
|
<pre><code>$ docker run --rm -t -i --volumes-from test_apt_cacher_ng eg_apt_cacher_ng bash
|
|
|
|
$$ /usr/lib/apt-cacher-ng/distkill.pl
|
|
Scanning /var/cache/apt-cacher-ng, please wait...
|
|
Found distributions:
|
|
bla, taggedcount: 0
|
|
1. precise-security (36 index files)
|
|
2. wheezy (25 index files)
|
|
3. precise-updates (36 index files)
|
|
4. precise (36 index files)
|
|
5. wheezy-updates (18 index files)
|
|
|
|
Found architectures:
|
|
6. amd64 (36 index files)
|
|
7. i386 (24 index files)
|
|
|
|
WARNING: The removal action may wipe out whole directories containing
|
|
index files. Select d to see detailed list.
|
|
|
|
(Number nn: tag distribution or architecture nn; 0: exit; d: show details; r: remove tagged; q: quit): q
|
|
</code></pre>
|
|
|
|
<p>Finally, clean up after your test by stopping and removing the
|
|
container, and then removing the image.</p>
|
|
|
|
<pre><code>$ docker stop test_apt_cacher_ng
|
|
$ docker rm test_apt_cacher_ng
|
|
$ docker rmi eg_apt_cacher_ng
|
|
</code></pre>
|
|
</description>
|
|
</item>
|
|
|
|
</channel>
|
|
</rss> |