Skip to content

Commit 0fa6708

Browse files
Added example of .dockerignore and compose.yml files
1 parent c0ff24d commit 0fa6708

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

content/guides/ruby/containerize.md

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ Rails 7.1 generates multistage Dockerfile out of the box, below is an example of
3030

3131
> 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/)
3232
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.
3334

34-
```dockerfile {collapse=true,title=Dockerfile}
35+
36+
```dockerfile {title=Dockerfile}
3537
# syntax=docker/dockerfile:1
3638
# check=error=true
3739

@@ -49,9 +51,9 @@ FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
4951
WORKDIR /rails
5052

5153
# 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
5355
RUN apt-get update -qq && \
54-
apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \
56+
apt-get install --no-install-recommends -y curl libjemalloc2 libvips libpq-dev && \
5557
rm -rf /var/lib/apt/lists /var/cache/apt/archives
5658

5759
# Set production environment
@@ -125,6 +127,79 @@ EXPOSE 80
125127
CMD ["./bin/thrust", "./bin/rails", "server"]
126128
```
127129

130+
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+
128203
Now that you have an application, you can create the necessary Docker assets to
129204
containerize your application. You can use Docker Desktop's built-in Docker Init
130205
feature to help streamline the process, or you can manually create the assets.

0 commit comments

Comments
 (0)