Skip to content

Commit be6a00a

Browse files
authored
Update onbuild.md
1 parent b7eb1b3 commit be6a00a

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

beginners/dockerfile/onbuild.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,155 @@ FROM my-node
3939

4040
Yes, there is only one such line. When constructing a mirror with this one-line `Dockerfile` in each project directory, the three lines of the previous base image `ONBUILD` will start executing, successfully copy the current project code into the image, and execute for this project. `npm install`, generate an application image.
4141

42+
43+
# Lab
44+
```
45+
# Dockerfile
46+
FROM busybox
47+
ONBUILD RUN echo "You won't see me until later"
48+
```
49+
# Docker build
50+
```
51+
docker build -t me/no_echo_here .
52+
53+
Uploading context 2.56 kB
54+
Uploading context
55+
Step 0 : FROM busybox
56+
Pulling repository busybox
57+
769b9341d937: Download complete
58+
511136ea3c5a: Download complete
59+
bf747efa0e2f: Download complete
60+
48e5f45168b9: Download complete
61+
---> 769b9341d937
62+
Step 1 : ONBUILD RUN echo "You won't see me until later"
63+
---> Running in 6bf1e8f65f00
64+
---> f864c417cc99
65+
Successfully built f864c417cc9
66+
```
67+
Here the ONBUILD instruction is read, not run, but stored for later use.
68+
```
69+
# Dockerfile
70+
FROM me/no_echo_here
71+
```
72+
docker build -t me/echo_here .
73+
Uploading context 2.56 kB
74+
Uploading context
75+
Step 0 : FROM cpuguy83/no_echo_here
76+
77+
# Executing 1 build triggers
78+
```
79+
Step onbuild-0 : RUN echo "You won't see me until later"
80+
---> Running in ebfede7e39c8
81+
You won't see me until later
82+
---> ca6f025712d4
83+
---> ca6f025712d4
84+
Successfully built ca6f025712d4
85+
```
86+
87+
## Ubutu Rails
88+
89+
```
90+
FROM ubuntu:12.04
91+
92+
RUN apt-get update -qq && apt-get install -y ca-certificates sudo curl git-core
93+
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
94+
95+
RUN locale-gen en_US.UTF-8
96+
ENV LANG en_US.UTF-8
97+
ENV LANGUAGE en_US.UTF-8
98+
ENV LC_ALL en_US.UTF-8
99+
100+
RUN curl -L https://get.rvm.io | bash -s stable
101+
ENV PATH /usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
102+
RUN /bin/bash -l -c rvm requirements
103+
RUN source /usr/local/rvm/scripts/rvm && rvm install ruby
104+
RUN rvm all do gem install bundler
105+
106+
ONBUILD ADD . /opt/rails_demo
107+
ONBUILD WORKDIR /opt/rails_demo
108+
ONBUILD RUN rvm all do bundle install
109+
ONBUILD CMD rvm all do bundle exec rails server
110+
111+
```
112+
This Dockerfile is doing some initial setup of a base image. Installs Ruby and bundler. Pretty typical stuff. At the end are the ONBUILD instructions.
113+
114+
ONBUILD ADD . /opt/rails_demo Tells any child image to add everything in the current directory to /opt/railsdemo. Remember, this only gets run from a child image, that is when another image uses this one as a base (or FROM). And it just so happens if you look in the repo I have a skeleton rails app in railsdemo that has it's own Dockerfile in it, we'll take a look at this later.
115+
116+
ONBUILD WORKDIR /opt/rails_demo Tells any child image to set the working directory to /opt/rails_demo, which is where we told ADD to put any project files
117+
118+
ONBUILD RUN rvm all do bundle install Tells any child image to have bundler install all gem dependencies, because we are assuming a Rails app here.
119+
120+
ONBUILD CMD rvm all do bundle exec rails server Tells any child image to set the CMD to start the rails server
121+
122+
Ok, so let's see this image build, go ahead and do this for yourself so you can see the output.
123+
124+
```
125+
git clone [email protected]:sangam14/docker_onbuild.git
126+
cd docker_onbuild
127+
docker build -t sangam14/docker_onbuild .
128+
129+
Step 0 : FROM ubuntu:12.04
130+
---> 9cd978db300e
131+
Step 1 : RUN apt-get update -qq && apt-get install -y ca-certificates sudo curl git-core
132+
---> Running in b32a089b7d2d
133+
# output supressed
134+
ldconfig deferred processing now taking place
135+
---> d3fdefaed447
136+
Step 2 : RUN rm /bin/sh && ln -s /bin/bash /bin/sh
137+
---> Running in f218cafc54d7
138+
---> 21a59f8613e1
139+
Step 3 : RUN locale-gen en_US.UTF-8
140+
---> Running in 0fcd7672ddd5
141+
Generating locales...
142+
done
143+
Generation complete.
144+
---> aa1074531047
145+
Step 4 : ENV LANG en_US.UTF-8
146+
---> Running in dcf936d57f38
147+
---> b9326a787f78
148+
Step 5 : ENV LANGUAGE en_US.UTF-8
149+
---> Running in 2133c36335f5
150+
---> 3382c53f7f40
151+
Step 6 : ENV LC_ALL en_US.UTF-8
152+
---> Running in 83f353aba4c8
153+
---> f849fc6bd0cd
154+
Step 7 : RUN curl -L https://get.rvm.io | bash -s stable
155+
---> Running in b53cc257d59c
156+
# output supressed
157+
---> 482a9f7ac656
158+
Step 8 : ENV PATH /usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
159+
---> Running in c4666b639c70
160+
---> b5d5c3e25730
161+
Step 9 : RUN /bin/bash -l -c rvm requirements
162+
---> Running in 91469dbc25a6
163+
# output supressed
164+
Step 10 : RUN source /usr/local/rvm/scripts/rvm && rvm install ruby
165+
---> Running in cb4cdfcda68f
166+
# output supressed
167+
Step 11 : RUN rvm all do gem install bundler
168+
---> Running in 9571104b3b65
169+
Successfully installed bundler-1.5.3
170+
Parsing documentation for bundler-1.5.3
171+
Installing ri documentation for bundler-1.5.3
172+
Done installing documentation for bundler after 3 seconds
173+
1 gem installed
174+
---> e2ea33486d62
175+
Step 12 : ONBUILD ADD . /opt/rails_demo
176+
---> Running in 5bef85f266a4
177+
---> 4082e2a71c7e
178+
Step 13 : ONBUILD WORKDIR /opt/rails_demo
179+
---> Running in be1a06c7f9ab
180+
---> 23bec71dce21
181+
Step 14 : ONBUILD RUN rvm all do bundle install
182+
---> Running in 991da8dc7f61
183+
---> 1547bef18de8
184+
Step 15 : ONBUILD CMD rvm all do bundle exec rails server
185+
---> Running in c49139e13a0c
186+
---> 23c388fb84c1
187+
Successfully built 23c388fb84c1
188+
```
189+
190+
42191
## Contributor - [Sangam Biradar](https://www.linkedin.com/in/sangambiradar14/)
43192

44193
Next >> [healthcheck](https://dockerlabs.collabnix.com/beginners/dockerfile/healthcheck.html)

0 commit comments

Comments
 (0)