mirror of
https://github.com/docker/docs.git
synced 2026-04-04 18:28:58 +07:00
853 lines
30 KiB
HTML
853 lines
30 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<meta name="description" content="Getting started with Docker">
|
|
<meta name="keywords" content="[beginner, tutorial, Docker]">
|
|
<title>Build your own image </title>
|
|
<link rel="shortcut icon" href="https://docs.docker.com/images/favicon.png" type="image/x-icon">
|
|
<link rel="stylesheet" href="../../dist/assets/css/app.css" />
|
|
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.6/animate.min.css">
|
|
<link rel="stylesheet" href="../../css/custom.css">
|
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
|
|
<script src="../../dist/assets/js/modernizr.js"></script>
|
|
</head>
|
|
<body>
|
|
<div class="off-canvas-wrap" data-offcanvas>
|
|
<div class="inner-wrap">
|
|
|
|
<a class="left-off-canvas-toggle" href="#" >
|
|
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="35px" height="35px" viewBox="0 0 35 35" enable-background="new 0 0 35 35" xml:space="preserve">
|
|
<path fill="#3597D4" d="M30.583,9.328c0,0.752-0.539,1.362-1.203,1.362H5.113c-0.664,0-1.203-0.61-1.203-1.362l0,0
|
|
c0-0.752,0.539-1.362,1.203-1.362H29.38C30.045,7.966,30.583,8.576,30.583,9.328L30.583,9.328z"/>
|
|
<path fill="#3597D4" d="M30.583,17.09c0,0.752-0.539,1.362-1.203,1.362H5.113c-0.664,0-1.203-0.61-1.203-1.362l0,0
|
|
c0-0.752,0.539-1.362,1.203-1.362H29.38C30.045,15.728,30.583,16.338,30.583,17.09L30.583,17.09z"/>
|
|
<path fill="#3597D4" d="M30.583,24.387c0,0.752-0.539,1.362-1.203,1.362H5.113c-0.664,0-1.203-0.61-1.203-1.362l0,0
|
|
c0-0.752,0.539-1.362,1.203-1.362H29.38C30.045,23.025,30.583,23.635,30.583,24.387L30.583,24.387z"/>
|
|
</svg>
|
|
</a>
|
|
<a class="button secondary small get-started-cta">Get Started</a>
|
|
<header class="main-header">
|
|
<div class="row">
|
|
<div class="large-3 columns">
|
|
<a href="../../"><img class="logo" src="../../dist/assets/images/logo.png"></a>
|
|
</div>
|
|
<div class="large-9 columns">
|
|
<ul class="nav-global">
|
|
<li><a href="https://www.docker.com/support">Support</a></li>
|
|
<li><a href="https://training.docker.com/">Training</a></li>
|
|
<li><a href="https://docs.docker.com/">Docs</a></li>
|
|
<li><a href="http://blog.docker.com/">Blog</a></li>
|
|
<li><a href="https://hub.docker.com/">Docker Hub</a></li>
|
|
<li><a class="button" href="../../mac/started/">Get Started</a></li>
|
|
</ul>
|
|
<ul class="nav-main">
|
|
<li><a href="https://www.docker.com/products">Products</a>
|
|
<ul>
|
|
<li><a href="https://www.docker.com/pricing">Pricing</a></li>
|
|
<li><a href="https://www.docker.com/whatisdocker">What is Docker?</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="https://www.docker.com/customers">Customers</a></li>
|
|
<li><a href="https://www.docker.com/community">Community</a>
|
|
<ul>
|
|
<li><a href="https://www.docker.com/community/meetups">Meetups</a></li>
|
|
<li><a href="https://www.docker.com/community/events">Events</a></li>
|
|
<li><a href="https://forums.docker.com">Forums</a></li>
|
|
<li><a href="http://www.scoop.it/t/docker-by-docker">Community News</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="https://www.docker.com/partners">Partners</a>
|
|
<ul>
|
|
<li><a href="https://www.docker.com/partners/partner-programs">Partner Programs</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="https://www.docker.com/company">Company</a>
|
|
<ul>
|
|
<li><a href="https://www.docker.com/news-and-press">News & Press</a></li>
|
|
<li><a href="https://www.docker.com/work-docker">Work at Docker</a></li>
|
|
<li><a href="https://www.docker.com/company/management">Management</a></li>
|
|
<li><a href="https://www.docker.com/company/contact">Contact</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="https://www.docker.com/open-source">Open Source</a>
|
|
<ul>
|
|
<li><a href="https://www.docker.com/contribute">Contribute</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
|
|
|
|
<aside class="left-off-canvas-menu">
|
|
<ul class="off-canvas-list">
|
|
<li class="has-submenu"><a href="#">Products</a>
|
|
<ul class="left-submenu">
|
|
<li class="back"><a href="#">Back</a></li>
|
|
<li><a href="#">Pricing</a></li>
|
|
<li><a href="#">What Is Docker</a></li>
|
|
<li><a href="#">Products</a></li>
|
|
<li><a href="#">Docker Engine</a></li>
|
|
<li><a href="#">Docker Hub</a></li>
|
|
<li><a href="#">Docker Registry</a></li>
|
|
<li><a href="#">Docker Machine</a></li>
|
|
<li><a href="#">Docker Swarm</a></li>
|
|
<li><a href="#">Docker Compose</a></li>
|
|
<li><a href="#">Kitematic</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#">Customers</a></li>
|
|
<li class="has-submenu"><a href="#">Community</a>
|
|
<ul class="left-submenu">
|
|
<li class="back"><a href="#">Back</a></li>
|
|
<li><a href="#">Community</a></li>
|
|
<li><a href="#">Meetups</a></li>
|
|
<li><a href="https://www.docker.com/community/events">Events</a></li>
|
|
<li><a href="#">Forum</a></li>
|
|
<li><a href="#">Scoop.it</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="has-submenu"><a href="#">Partners</a>
|
|
<ul class="left-submenu">
|
|
<li class="back"><a href="#">Back</a></li>
|
|
<li><a href="#">Partners</a></li>
|
|
<li><a href="https://www.docker.com/partners/partner-programs">Partners Programs</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#">Company</a></li>
|
|
<li class="has-submenu"><a href="#">Open Source</a>
|
|
<ul class="left-submenu">
|
|
<li class="back"><a href="#">Back</a></li>
|
|
<li><a href="#">Open Source</a></li>
|
|
<li><a href="#">Contribute</a></li>
|
|
<li><a href="#">Governance</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<ul class="nav-global-off-canvas">
|
|
<li><a href="#">Support</a></li>
|
|
<li><a href="#">Training</a></li>
|
|
<li><a href="#">Docs</a></li>
|
|
<li><a href="#">Blog</a></li>
|
|
<li><a href="#">Sign in</a></li>
|
|
<li><a href="#">Sign up</a></li>
|
|
</ul>
|
|
</aside>
|
|
|
|
<a class="exit-off-canvas"></a>
|
|
<div class="row">
|
|
<div class="large-3 columns" class="tutmenu">
|
|
<section id="multiple" class="tutmenu" data-accordion-group>
|
|
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/started/" class=""> Get Started with Docker</a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_one/" class=""> Install Docker</a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_two/" class=""> Understand images & containers</a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_three/" class=""> Find & run the whalesay image </a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_four/" class=" active"> Build your own image</a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_five/" class=""> Create a Docker Hub account and repository </a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_six/" class=""> Tag & push your image</a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/last_page/" class=""> Learning More</a>
|
|
|
|
</section>
|
|
|
|
</section>
|
|
|
|
<script>
|
|
$(document).ready(function () {
|
|
var $activeLink = $('#multiple [data-link].active');
|
|
var $accordions = $activeLink.parents('article[data-accordion]');
|
|
$($accordions.get().reverse()).each(function (index, accordion) {
|
|
var $accordion = $(accordion);
|
|
var $content = $accordion.find('[data-content]');
|
|
$accordion.addClass('open');
|
|
$content.css({'max-height': '100%'});
|
|
});
|
|
});
|
|
</script>
|
|
|
|
</div>
|
|
<div class="large-9 columns">
|
|
<section id="main">
|
|
<article id="content" class="tutorial">
|
|
<div class="linux" >
|
|
|
|
|
|
<h1 id="build-your-own-image">Build your own image</h1>
|
|
|
|
<p>The <code>whalesay</code> image could be improved. It would be nice if you didn’t have to
|
|
think of something to say. And you type a lot to get <code>whalesay</code> to talk.</p>
|
|
|
|
<pre><code>docker run docker/whalesay cowsay boo-boo
|
|
</code></pre>
|
|
|
|
<p>In this next section, you will improve the <code>whalesay</code> image by building a new version that “talks on its own” and requires fewer words to run.</p>
|
|
|
|
<h2 id="step-1-write-a-dockerfile">Step 1: Write a Dockerfile</h2>
|
|
|
|
<p>In this step, you use your favorite text editor to write a short Dockerfile. A
|
|
Dockerfile describes the software that is “baked” into an image. It isn’t just
|
|
ingredients tho, it can tell the software what environment to use or what
|
|
commands to run. Your recipe is going to be very short.</p>
|
|
|
|
<ol>
|
|
<li><p>Go back to your terminal window.</p></li>
|
|
|
|
<li><p>Make a new directory by typing <code>mkdir mydockerbuild</code> and pressing RETURN.</p>
|
|
|
|
<pre><code>$ mkdir mydockerbuild
|
|
</code></pre>
|
|
|
|
<p>This directory serves as the “context” for your build. The context just means it contains all the things you need to build your image.</p></li>
|
|
|
|
<li><p>Change to your new directory.</p>
|
|
|
|
<pre><code>$ cd mydockerbuild
|
|
</code></pre>
|
|
|
|
<p>Right now the directory is empty.</p></li>
|
|
|
|
<li><p>Create a text file called <code>Dockerfile</code> in your current directory.</p>
|
|
|
|
<p>You can use any text editor for example, <code>vi</code> or <code>nano</code> to do this.</p></li>
|
|
|
|
<li><p>Open your Dockerfile file.</p></li>
|
|
|
|
<li><p>Add a line to the file like this:</p>
|
|
|
|
<pre><code>FROM docker/whalesay:latest
|
|
</code></pre>
|
|
|
|
<p>The <code>FROM</code> keyword tells Docker which image your image is based on. Whalesay is
|
|
cute and has the <code>cowsay</code> program already, so we’ll start there.</p></li>
|
|
|
|
<li><p>Now, add the <code>fortunes</code> program to the image.</p>
|
|
|
|
<pre><code>RUN apt-get -y update && apt-get install -y fortunes
|
|
</code></pre>
|
|
|
|
<p>The <code>fortunes</code> program has a command that prints out wise sayings for our
|
|
whale to say. So, the first step is to install it. This line installs the
|
|
software into the image.</p></li>
|
|
|
|
<li><p>Once the image has the software it needs, you instruct the software to run
|
|
when the image is loaded.</p>
|
|
|
|
<pre><code>CMD /usr/games/fortune -a | cowsay
|
|
</code></pre>
|
|
|
|
<p>This line tells the <code>fortune</code> program to pass a nifty quote to the <code>cowsay</code> program.</p></li>
|
|
|
|
<li><p>Check your work, your file should look like this:</p>
|
|
|
|
<pre><code>FROM docker/whalesay:latest
|
|
RUN apt-get -y update && apt-get install -y fortunes
|
|
CMD /usr/games/fortune -a | cowsay
|
|
</code></pre></li>
|
|
|
|
<li><p>Save and close your Dockerfile.</p>
|
|
|
|
<p>At this point, you have all your software ingredients and behaviors described in a Dockerfile. You are ready to build a new image.</p></li>
|
|
</ol>
|
|
|
|
<h2 id="step-2-build-an-image-from-your-dockerfile">Step 2: Build an image from your Dockerfile</h2>
|
|
|
|
<ol>
|
|
<li><p>Now, build your new image by typing the <code>docker build -t docker-whale .</code> command in your terminal (don’t forget the . period).</p>
|
|
|
|
<pre><code>$ docker build -t docker-whale .
|
|
Sending build context to Docker daemon 158.8 MB
|
|
...snip...
|
|
Removing intermediate container a8e6faa88df3
|
|
Successfully built 7d9495d03763
|
|
</code></pre>
|
|
|
|
<p>The command takes several seconds to run and reports its outcome. Before
|
|
you do anything with the new image, take a minute to learn about the
|
|
Dockerfile build process.</p></li>
|
|
</ol>
|
|
|
|
<h2 id="step-3-learn-about-the-build-process">Step 3: Learn about the build process</h2>
|
|
|
|
<p>The <code>docker build -t docker-whale .</code> command takes the <code>Dockerfile</code> in the
|
|
current directory, and builds an image called <code>docker-whale</code> on your local
|
|
machine. The command takes about a minute and its output looks really long and
|
|
complex. In this section, you learn what each message means.</p>
|
|
|
|
<p>First Docker checks to make sure it has everything it needs to build.</p>
|
|
|
|
<pre><code>Sending build context to Docker daemon 158.8 MB
|
|
</code></pre>
|
|
|
|
<p>Then, Docker loads with the <code>whalesay</code> image. It already has this image
|
|
locally as your might recall from the last page. So, Docker doesn’t need to
|
|
download it.</p>
|
|
|
|
<pre><code>Step 0 : FROM docker/whalesay:latest
|
|
---> fb434121fc77
|
|
</code></pre>
|
|
|
|
<p>Docker moves onto the next step which is to update the <code>apt-get</code> package
|
|
manager. This takes a lot of lines, no need to list them all again here.</p>
|
|
|
|
<pre><code>Step 1 : RUN apt-get -y update && apt-get install -y fortunes
|
|
---> Running in 27d224dfa5b2
|
|
Ign http://archive.ubuntu.com trusty InRelease
|
|
Ign http://archive.ubuntu.com trusty-updates InRelease
|
|
Ign http://archive.ubuntu.com trusty-security InRelease
|
|
Hit http://archive.ubuntu.com trusty Release.gpg
|
|
....snip...
|
|
Get:15 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [14.8 kB]
|
|
Get:16 http://archive.ubuntu.com trusty-security/universe amd64 Packages [134 kB]
|
|
Reading package lists...
|
|
---> eb06e47a01d2
|
|
</code></pre>
|
|
|
|
<p>Then, Docker installs the new <code>fortunes</code> software.</p>
|
|
|
|
<pre><code>Removing intermediate container e2a84b5f390f
|
|
Step 2 : RUN apt-get install -y fortunes
|
|
---> Running in 23aa52c1897c
|
|
Reading package lists...
|
|
Building dependency tree...
|
|
Reading state information...
|
|
The following extra packages will be installed:
|
|
fortune-mod fortunes-min librecode0
|
|
Suggested packages:
|
|
x11-utils bsdmainutils
|
|
The following NEW packages will be installed:
|
|
fortune-mod fortunes fortunes-min librecode0
|
|
0 upgraded, 4 newly installed, 0 to remove and 3 not upgraded.
|
|
Need to get 1961 kB of archives.
|
|
After this operation, 4817 kB of additional disk space will be used.
|
|
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main librecode0 amd64 3.6-21 [771 kB]
|
|
...snip......
|
|
Setting up fortunes (1:1.99.1-7) ...
|
|
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
|
|
---> c81071adeeb5
|
|
Removing intermediate container 23aa52c1897c
|
|
</code></pre>
|
|
|
|
<p>Finally, Docker finishes the build and reports its outcome.</p>
|
|
|
|
<pre><code>Step 3 : CMD /usr/games/fortune -a | cowsay
|
|
---> Running in a8e6faa88df3
|
|
---> 7d9495d03763
|
|
Removing intermediate container a8e6faa88df3
|
|
Successfully built 7d9495d03763
|
|
</code></pre>
|
|
|
|
<h2 id="step-4-run-your-new-docker-whale">Step 4: Run your new docker-whale</h2>
|
|
|
|
<p>In this step, you verify the new images is on your computer and then you run your new image.</p>
|
|
|
|
<ol>
|
|
<li><p>If it isn’t already there, place your cursor at the prompt in your terminal window.</p></li>
|
|
|
|
<li><p>Type <code>docker images</code> and press RETURN.</p>
|
|
|
|
<p>This command, you might remember, lists the images you have locally.</p>
|
|
|
|
<pre><code>$ docker images
|
|
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
|
|
docker-whale latest 7d9495d03763 4 minutes ago 273.7 MB
|
|
docker/whalesay latest fb434121fc77 4 hours ago 247 MB
|
|
hello-world latest 91c95931e552 5 weeks ago 910 B
|
|
</code></pre></li>
|
|
|
|
<li><p>Run your new image by typing <code>docker run docker-whale</code> and pressing RETURN.</p>
|
|
|
|
<pre><code>$ docker run docker-whale
|
|
_________________________________________
|
|
/ "He was a modest, good-humored boy. It \
|
|
\ was Oxford that made him insufferable." /
|
|
-----------------------------------------
|
|
\
|
|
\
|
|
\
|
|
## .
|
|
## ## ## ==
|
|
## ## ## ## ===
|
|
/""""""""""""""""___/ ===
|
|
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
|
|
\______ o __/
|
|
\ \ __/
|
|
\____\______/
|
|
</code></pre></li>
|
|
</ol>
|
|
|
|
<p>As you can see, you’ve made the whale a lot smarter. It finds its own
|
|
things to say and the command line is a lot shorter! You may also notice
|
|
that Docker didn’t have to download anything. That is because the image was
|
|
built locally and is already available.</p>
|
|
|
|
<h2 id="where-to-go-next">Where to go next</h2>
|
|
|
|
<p>On this page, you learned to build an image by writing your own Dockerfile.
|
|
You ran your image in a container. In the next section, you take the first step in sharing your image by
|
|
<a href="../../linux/step_five/">creating a Docker Hub account</a>.</p>
|
|
|
|
<p> </p>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<footer class="main-footer">
|
|
<div class="row">
|
|
</div>
|
|
<div class="row">
|
|
</div>
|
|
<div id="buildinfo">
|
|
Nov 3, 2015 at 7:59pm (PST)
|
|
{
|
|
"docker/compose": {
|
|
"ref": "docs",
|
|
"repos": [
|
|
"git@github.com:docker/compose.git"
|
|
],
|
|
"sha": "9c8173dbfda93baef214359991b6a8a54172f6ae"
|
|
},
|
|
"docker/docker-hub": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/hub2-demo.git"
|
|
],
|
|
"sha": "4b2e522c81c860d63b126342a6b981ac0ff1605c"
|
|
},
|
|
"docker/docker-trusted-registry": {
|
|
"ref": "docs",
|
|
"repos": [
|
|
"git@github.com:docker/dhe-deploy.git"
|
|
],
|
|
"sha": "b8988465878952f2e2c2472e8fc5fd35e5975fbf"
|
|
},
|
|
"docker/docs-base": {
|
|
"ref": "hugo-github-linking",
|
|
"repos": [
|
|
"git@github.com:docker/docs-base.git"
|
|
],
|
|
"sha": "dc98c0381a6cc311c9e3189dc78a3c7e62e5a205"
|
|
},
|
|
"docker/engine": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/docker.git"
|
|
],
|
|
"sha": "474b16af8ecfe94ec635dfac60025348d3186aa3"
|
|
},
|
|
"docker/machine": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/machine.git"
|
|
],
|
|
"sha": "786437901c0c883ecb59c1e0531654c1d89b326d"
|
|
},
|
|
"docker/opensource": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/opensource.git"
|
|
],
|
|
"sha": "0cd99bcdd876ca0293d8944980c79f32064b6354"
|
|
},
|
|
"docker/registry": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/distribution.git"
|
|
],
|
|
"sha": "a9da0e510032314910b5405acc50873ab2fa2e5a"
|
|
},
|
|
"docker/swarm": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/swarm.git"
|
|
],
|
|
"sha": "087e2452f3ec474f112b4e5b8c52b8dacb5751be"
|
|
},
|
|
"docker/tutorials": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/tutorials.git"
|
|
],
|
|
"sha": "cb55d4de0df55e22f443aac664d66f092f06c56b"
|
|
},
|
|
"docs.docker.com": {
|
|
"ref": "refs/heads/1-9-release",
|
|
"repos": [
|
|
"git@github.com:moxiegirl/docs.docker.com.git",
|
|
"git@github.com:docker/docs.docker.com.git"
|
|
],
|
|
"sha": "5878eae5de6f012c67a2a4772327c9948274c351"
|
|
},
|
|
"kitematic/kitematic": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:kitematic/kitematic.git"
|
|
],
|
|
"sha": "e533ed35d2eab31ce528675b0665f97516b4147b"
|
|
}
|
|
} </div>
|
|
</footer>
|
|
<link rel="stylesheet" href="../../highlight/styles/github.css">
|
|
<script src="../../highlight/highlight.pack.js"></script>
|
|
<script>hljs.initHighlightingOnLoad();</script>
|
|
|
|
<script src="../../dist/assets/js/all.js"></script>
|
|
<script>
|
|
$( 'nav li:has(ul)' ).doubleTapToGo();
|
|
</script>
|
|
<script>
|
|
|
|
;(function ( $, window, document, undefined ) {
|
|
|
|
var pluginName = 'accordion',
|
|
defaults = {
|
|
transitionSpeed: 300,
|
|
transitionEasing: 'ease',
|
|
controlElement: '[data-control]',
|
|
contentElement: '[data-content]',
|
|
groupElement: '[data-accordion-group]',
|
|
singleOpen: true
|
|
};
|
|
|
|
function Accordion(element, options) {
|
|
this.element = element;
|
|
this.options = $.extend({}, defaults, options);
|
|
this._defaults = defaults;
|
|
this._name = pluginName;
|
|
this.init();
|
|
}
|
|
|
|
Accordion.prototype.init = function () {
|
|
var self = this,
|
|
opts = self.options;
|
|
|
|
var $accordion = $(self.element),
|
|
$controls = $accordion.find('> ' + opts.controlElement),
|
|
$content = $accordion.find('> ' + opts.contentElement);
|
|
|
|
var accordionParentsQty = $accordion.parents('[data-accordion]').length,
|
|
accordionHasParent = accordionParentsQty > 0;
|
|
|
|
var closedCSS = { 'max-height': 0, 'overflow': 'hidden' };
|
|
|
|
var CSStransitions = supportsTransitions();
|
|
|
|
function debounce(func, threshold, execAsap) {
|
|
var timeout;
|
|
|
|
return function debounced() {
|
|
var obj = this,
|
|
args = arguments;
|
|
|
|
function delayed() {
|
|
if (!execAsap) func.apply(obj, args);
|
|
timeout = null;
|
|
};
|
|
|
|
if (timeout) clearTimeout(timeout);
|
|
else if (execAsap) func.apply(obj, args);
|
|
|
|
timeout = setTimeout(delayed, threshold || 100);
|
|
};
|
|
}
|
|
|
|
function supportsTransitions() {
|
|
var b = document.body || document.documentElement,
|
|
s = b.style,
|
|
p = 'transition';
|
|
|
|
if (typeof s[p] == 'string') {
|
|
return true;
|
|
}
|
|
|
|
var v = ['Moz', 'webkit', 'Webkit', 'Khtml', 'O', 'ms'];
|
|
|
|
p = 'Transition';
|
|
|
|
for (var i=0; i<v.length; i++) {
|
|
if (typeof s[v[i] + p] == 'string') {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function requestAnimFrame(cb) {
|
|
if(window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame) {
|
|
return requestAnimationFrame(cb) ||
|
|
webkitRequestAnimationFrame(cb) ||
|
|
mozRequestAnimationFrame(cb);
|
|
} else {
|
|
return setTimeout(cb, 1000 / 60);
|
|
}
|
|
}
|
|
|
|
function toggleTransition($el, remove) {
|
|
if(!remove) {
|
|
$content.css({
|
|
'-webkit-transition': 'max-height ' + opts.transitionSpeed + 'ms ' + opts.transitionEasing,
|
|
'transition': 'max-height ' + opts.transitionSpeed + 'ms ' + opts.transitionEasing
|
|
});
|
|
} else {
|
|
$content.css({
|
|
'-webkit-transition': '',
|
|
'transition': ''
|
|
});
|
|
}
|
|
}
|
|
|
|
function calculateHeight($el) {
|
|
var height = 0;
|
|
|
|
$el.children().each(function() {
|
|
height = height + $(this).outerHeight(true);
|
|
});
|
|
|
|
$el.data('oHeight', height);
|
|
}
|
|
|
|
function updateParentHeight($parentAccordion, $currentAccordion, qty, operation) {
|
|
var $content = $parentAccordion.filter('.open').find('> [data-content]'),
|
|
$childs = $content.find('[data-accordion].open > [data-content]'),
|
|
$matched;
|
|
|
|
if(!opts.singleOpen) {
|
|
$childs = $childs.not($currentAccordion.siblings('[data-accordion].open').find('> [data-content]'));
|
|
}
|
|
|
|
$matched = $content.add($childs);
|
|
|
|
if($parentAccordion.hasClass('open')) {
|
|
$matched.each(function() {
|
|
var currentHeight = $(this).data('oHeight');
|
|
|
|
switch (operation) {
|
|
case '+':
|
|
$(this).data('oHeight', currentHeight + qty);
|
|
break;
|
|
case '-':
|
|
$(this).data('oHeight', currentHeight - qty);
|
|
break;
|
|
default:
|
|
throw 'updateParentHeight method needs an operation';
|
|
}
|
|
|
|
$(this).css('max-height', $(this).data('oHeight'));
|
|
});
|
|
}
|
|
}
|
|
|
|
function refreshHeight($accordion) {
|
|
if($accordion.hasClass('open')) {
|
|
var $content = $accordion.find('> [data-content]'),
|
|
$childs = $content.find('[data-accordion].open > [data-content]'),
|
|
$matched = $content.add($childs);
|
|
|
|
calculateHeight($matched);
|
|
|
|
$matched.css('max-height', $matched.data('oHeight'));
|
|
}
|
|
}
|
|
|
|
function closeAccordion($accordion, $content) {
|
|
$accordion.trigger('accordion.close');
|
|
|
|
if(CSStransitions) {
|
|
if(accordionHasParent) {
|
|
var $parentAccordions = $accordion.parents('[data-accordion]');
|
|
|
|
updateParentHeight($parentAccordions, $accordion, $content.data('oHeight'), '-');
|
|
}
|
|
|
|
$content.css(closedCSS);
|
|
|
|
$accordion.removeClass('open');
|
|
} else {
|
|
$content.css('max-height', $content.data('oHeight'));
|
|
|
|
$content.animate(closedCSS, opts.transitionSpeed);
|
|
|
|
$accordion.removeClass('open');
|
|
}
|
|
}
|
|
|
|
function openAccordion($accordion, $content) {
|
|
$accordion.trigger('accordion.open');
|
|
if(CSStransitions) {
|
|
toggleTransition($content);
|
|
|
|
if(accordionHasParent) {
|
|
var $parentAccordions = $accordion.parents('[data-accordion]');
|
|
|
|
updateParentHeight($parentAccordions, $accordion, $content.data('oHeight'), '+');
|
|
}
|
|
|
|
requestAnimFrame(function() {
|
|
$content.css('max-height', $content.data('oHeight'));
|
|
});
|
|
|
|
$accordion.addClass('open');
|
|
} else {
|
|
$content.animate({
|
|
'max-height': $content.data('oHeight')
|
|
}, opts.transitionSpeed, function() {
|
|
$content.css({'max-height': 'none'});
|
|
});
|
|
|
|
$accordion.addClass('open');
|
|
}
|
|
}
|
|
|
|
function closeSiblingAccordions($accordion) {
|
|
var $accordionGroup = $accordion.closest(opts.groupElement);
|
|
|
|
var $siblings = $accordion.siblings('[data-accordion]').filter('.open'),
|
|
$siblingsChildren = $siblings.find('[data-accordion]').filter('.open');
|
|
|
|
var $otherAccordions = $siblings.add($siblingsChildren);
|
|
|
|
$otherAccordions.each(function() {
|
|
var $accordion = $(this),
|
|
$content = $accordion.find(opts.contentElement);
|
|
|
|
closeAccordion($accordion, $content);
|
|
});
|
|
|
|
$otherAccordions.removeClass('open');
|
|
}
|
|
|
|
function toggleAccordion() {
|
|
var isAccordionGroup = (opts.singleOpen) ? $accordion.parents(opts.groupElement).length > 0 : false;
|
|
|
|
calculateHeight($content);
|
|
|
|
if(isAccordionGroup) {
|
|
closeSiblingAccordions($accordion);
|
|
}
|
|
|
|
if($accordion.hasClass('open')) {
|
|
closeAccordion($accordion, $content);
|
|
} else {
|
|
openAccordion($accordion, $content);
|
|
}
|
|
}
|
|
|
|
function addEventListeners() {
|
|
$controls.on('click', toggleAccordion);
|
|
|
|
$controls.on('accordion.toggle', function() {
|
|
if(opts.singleOpen && $controls.length > 1) {
|
|
return false;
|
|
}
|
|
|
|
toggleAccordion();
|
|
});
|
|
|
|
$(window).on('resize', debounce(function() {
|
|
refreshHeight($accordion);
|
|
}));
|
|
}
|
|
|
|
function setup() {
|
|
$content.each(function() {
|
|
var $curr = $(this);
|
|
|
|
if($curr.css('max-height') != 0) {
|
|
if(!$curr.closest('[data-accordion]').hasClass('open')) {
|
|
$curr.css({ 'max-height': 0, 'overflow': 'hidden' });
|
|
} else {
|
|
toggleTransition($curr);
|
|
calculateHeight($curr);
|
|
|
|
$curr.css('max-height', $curr.data('oHeight'));
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
if(!$accordion.attr('data-accordion')) {
|
|
$accordion.attr('data-accordion', '');
|
|
$accordion.find(opts.controlElement).attr('data-control', '');
|
|
$accordion.find(opts.contentElement).attr('data-content', '');
|
|
}
|
|
}
|
|
|
|
setup();
|
|
addEventListeners();
|
|
};
|
|
|
|
$.fn[pluginName] = function ( options ) {
|
|
return this.each(function () {
|
|
if (!$.data(this, 'plugin_' + pluginName)) {
|
|
$.data(this, 'plugin_' + pluginName,
|
|
new Accordion( this, options ));
|
|
}
|
|
});
|
|
}
|
|
|
|
})( jQuery, window, document );
|
|
|
|
$(document).ready(function() {
|
|
$('#multiple [data-accordion]').accordion({
|
|
singleOpen: false
|
|
});
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
<noscript>
|
|
<iframe src="//www.googletagmanager.com/ns.html?id=GTM-PSVHG8"
|
|
height="0" width="0" style="display:none;visibility:hidden"></iframe>
|
|
</noscript>
|
|
<script>
|
|
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
|
|
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
|
|
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
|
|
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
|
|
})(window,document,'script','dataLayer','GTM-PSVHG8');
|
|
</script>
|
|
|