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: content/guides/ruby/containerize.md
+78-3Lines changed: 78 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,8 +30,10 @@ Rails 7.1 generates multistage Dockerfile out of the box, below is an example of
30
30
31
31
> Multistage Dockerfiles help create smaller, more efficient images by separating build and runtime dependencies, ensuring only necessary components are included in the final image. Read more about multi-stage builds [in a dedicated article](/get-started/docker-concepts/building-images/multi-stage-builds/)
32
32
33
+
Even though the Dockerfile is generated for you, it's a good idea to understand what it does and how it works, so we recommend reading the following example.
33
34
34
-
```dockerfile {collapse=true,title=Dockerfile}
35
+
36
+
```dockerfile {title=Dockerfile}
35
37
# syntax=docker/dockerfile:1
36
38
# check=error=true
37
39
@@ -49,9 +51,9 @@ FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
49
51
WORKDIR /rails
50
52
51
53
# Install base packages
52
-
# Replace sqlite3 with libpq-dev if using PostgreSQL, or libmysqlclient-dev if using MySQL
54
+
# Replace libpq-dev with sqlite3 if using SQLite, or libmysqlclient-dev if using MySQL
The Dockerfile above assumes you are using Thruster and Puma as the application server. In case you are using any other server, you can replace the last three lines with the following:
131
+
132
+
```dockerfile
133
+
# Start the application server
134
+
EXPOSE 3000
135
+
CMD ["./bin/rails", "server"]
136
+
```
137
+
138
+
Besides the Dockerfile you will also need a `.dockerignore` file. This file is used to exclude files and directories from the context of the build. Below is an example of a `.dockerignore` file.
139
+
140
+
```text {collapse=true,title=".dockerignore"}
141
+
# See https://docs.docker.com/engine/reference/builder/#dockerignore-file for more about ignoring files.
142
+
143
+
# Ignore git directory.
144
+
/.git/
145
+
/.gitignore
146
+
147
+
# Ignore bundler config.
148
+
/.bundle
149
+
150
+
# Ignore all environment files.
151
+
/.env*
152
+
153
+
# Ignore all default key files.
154
+
/config/master.key
155
+
/config/credentials/*.key
156
+
157
+
# Ignore all logfiles and tempfiles.
158
+
/log/*
159
+
/tmp/*
160
+
!/log/.keep
161
+
!/tmp/.keep
162
+
163
+
# Ignore pidfiles, but keep the directory.
164
+
/tmp/pids/*
165
+
!/tmp/pids/.keep
166
+
167
+
# Ignore storage (uploaded files in development and any SQLite databases).
168
+
/storage/*
169
+
!/storage/.keep
170
+
/tmp/storage/*
171
+
!/tmp/storage/.keep
172
+
173
+
# Ignore assets.
174
+
/node_modules/
175
+
/app/assets/builds/*
176
+
!/app/assets/builds/.keep
177
+
/public/assets
178
+
179
+
# Ignore CI service files.
180
+
/.github
181
+
182
+
# Ignore development files
183
+
/.devcontainer
184
+
185
+
# Ignore Docker-related files
186
+
/.dockerignore
187
+
/Dockerfile*
188
+
```
189
+
190
+
The last thing that may be necessary, but not always required is `compose.yml` file, used by Docker Compose to define the services that make up your application. Since we are using SQLite as the database, we don't need to define a separate service for the database, and the only service we need is the Rails application itself.
191
+
192
+
```yaml {collapse=true,title=compose.yaml}
193
+
services:
194
+
web:
195
+
build: .
196
+
volumes:
197
+
- .:/myapp
198
+
ports:
199
+
- "3000:80"
200
+
```
201
+
202
+
128
203
Now that you have an application, you can create the necessary Docker assets to
129
204
containerize your application. You can use Docker Desktop's built-in Docker Init
130
205
feature to help streamline the process, or you can manually create the assets.
0 commit comments