Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile for Ruby app - why is WORKDIR specified as /usr/src/app

Tags:

docker

ruby

Looking at the official Ruby image Docker page - the sample shows setting the WORKDIR /usr/src/app.

Does anyone know why this specific path?

Namely is it ok to just have my app at root or in other words leave out WORKDIR? I know its recommend as per the documentation but I have some issues in it not creating a directory.

Just want to ensure there are no issues in dropping it. I tested my image otherwise and it works as expected.

Thanks.

like image 232
userMod2 Avatar asked Feb 14 '19 08:02

userMod2


People also ask

What does Workdir mean in Dockerfile?

The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.

What is usr src docker app?

where /usr/src/app is the mount point within the container (different Docker images will assume the project's code is at different spots within the file system; be sure to verify).

Is Workdir necessary in Dockerfile?

According to the documentation: The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn't exist, it will be created even if it's not used in any subsequent Dockerfile instruction.

What is the default Workdir in docker?

The default is indeed / as stated elsewhere. It is worth mentioning, though, that you will almost never be running from an empty docker image ( FROM scratch ), so the WORKDIR is likely set by the base image you're using.


1 Answers

The Linux Documentation project says this about /usr/src

/usr is shareable, read-only data.

Large software packages must not use a direct subdirectory under the /usr hierarchy

The 'linux' sub-directory holds the Linux kernel sources, header-files and documentation.

I'm not sure why they used that directory, and it's probably not harmful, but it also isn't necessarily the best place for a Ruby/Rails app. At the same time the location you place it doesn't entirely matter, but you do probably want to observe some rules:

  • Avoid using your ~ (home) folder. This can cause problems if you have libraries that install by default to home, then you try and mount that directory and it overwrites things. Plus, you may end up with artifacts in there (.bash_profile, .config/, etc) that you don't want
  • Avoid actual / (root) so you don't risk overwriting a system directory

Creating an /app directory is the most common pattern I've seen. You'll see in some CI/CD tools that they put your data into an /app folder, so that's what I try to use for my Dockerfiles

like image 136
Jay Dorsey Avatar answered Oct 12 '22 08:10

Jay Dorsey