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
+109-2Lines changed: 109 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,12 +26,18 @@ If you have an existing Rails application, you will need to create the Docker as
26
26
27
27
## 1. Initialize Docker assets
28
28
29
-
Rails 7.1 generates multistage Dockerfile out of the box, below is an example of such file generated from a Rails template.
29
+
Rails 7.1 and newer generates multistage Dockerfile out of the box. Following are two versions of such a file: one using Docker Hardened Images (DHI) and another using the official Docker image.
30
+
31
+
> [Docker Hardened Images (DHIs)](https://docs.docker.com/dhi/) are minimal, secure, and production-ready container base and application images maintained by Docker.
32
+
33
+
DHI images are recommended whenever it is possible for better security. They are designed to reduce vulnerabilities and simplify compliance.
30
34
31
35
> 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 in the [Multi-stage builds guide](/get-started/docker-concepts/building-images/multi-stage-builds/).
32
36
33
37
Although the Dockerfile is generated automatically, understanding its purpose and functionality is important. Reviewing the following example is highly recommended.
34
38
39
+
{{< tabs >}}
40
+
{{< tab name="Using Docker Hardened Images" >}}
35
41
36
42
```dockerfile {title=Dockerfile}
37
43
# syntax=docker/dockerfile:1
@@ -44,7 +50,104 @@ Although the Dockerfile is generated automatically, understanding its purpose an
44
50
# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
45
51
46
52
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
47
-
ARG RUBY_VERSION=3.3.6
53
+
ARG RUBY_VERSION=3.4.7
54
+
FROM <your-namespace>/dhi-ruby:$RUBY_VERSION-dev AS base
55
+
56
+
# Rails app lives here
57
+
WORKDIR /rails
58
+
59
+
# Install base packages
60
+
# Replace libpq-dev with sqlite3 if using SQLite, or libmysqlclient-dev if using MySQL
# Start server via Thruster by default, this can be overwritten at runtime
132
+
EXPOSE 80
133
+
CMD ["./bin/thrust", "./bin/rails", "server"]
134
+
```
135
+
136
+
{{< /tab >}}
137
+
{{< tab name="Using the official Docker image" >}}
138
+
139
+
```dockerfile {title=Dockerfile}
140
+
# syntax=docker/dockerfile:1
141
+
# check=error=true
142
+
143
+
# This Dockerfile is designed for production, not development.
144
+
# docker build -t app .
145
+
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name app app
146
+
147
+
# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
148
+
149
+
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
150
+
ARG RUBY_VERSION=3.4.7
48
151
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
49
152
50
153
# Rails app lives here
@@ -127,6 +230,9 @@ EXPOSE 80
127
230
CMD ["./bin/thrust", "./bin/rails", "server"]
128
231
```
129
232
233
+
{{< /tab >}}
234
+
{{< /tabs >}}
235
+
130
236
The Dockerfile above assumes you are using Thruster together with Puma as an application server. In case you are using any other server, you can replace the last three lines with the following:
131
237
132
238
```dockerfile
@@ -279,3 +385,4 @@ Related information:
279
385
## Next steps
280
386
281
387
In the next section, you'll take a look at how to set up a CI/CD pipeline using GitHub Actions.
0 commit comments