You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: beginners/dockerfile/onbuild.md
+149Lines changed: 149 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,155 @@ FROM my-node
39
39
40
40
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.
41
41
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"
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.
0 commit comments