How to Dockerize a Remix App
Remix is a React server and browser runtime that provides out of the box server side rendering, snappy page loads, and instant transitions with near-zero configuration.
In this blog post, we will demonstrate how to Dockerize Remix applications.
- Working Docker installation.
- Working Remix installation.
- Basic understanding of how a Node.js application is structured.
In order to dockerize your remix app, you need to create a
dockerfile at the root of your application.
Dockerfiles are simple text files containing instructions on how to create Docker images.
In a simple docker file we would:
- Install dependencies.
- Build our application.
- Setup environment variables.
- Setup the start command that will run the application.
Create an empty file called Dockerfile:
Let's start by defining what image we want to build from.
Here, we will use the
16-alpine version of
NodeJS that can be found on the Docker Hub
We will now create a directory for the application code inside the Docker image. This will be the working directory for your application
//directory WORKDIR /usr/server/app
npm are pre-installed in this image, the next step is to install the
remix app's dependencies.
COPY ./package.json ./ RUN npm install
Note that we are only copying the
package.json instead of copying the entire source code.
This enables you to take advantage of Docker layer caching. A good explanation of this can be found here.
The next step would be to copy the entire source code.
COPY ./ .
At this point, our source code and dependencies are all set in our Docker image. The next step is to build the remix app and set some environment variables.
RUN npm run build # will build remix app ENV NODE_ENV=production
Finally, we define the command to run our remix app using
CMD ["npm", “run” ,"start"]
In summary, this is what our
dockerfile looks like.
FROM node:16-alpine WORKDIR /usr/server/app COPY ./package.json ./ RUN npm install COPY ./ . RUN npm run build ENV NODE_ENV=production CMD ["npm", “run” ,"start"] # will launch the remix app when we run this Docker image.
.dockerignore file in the same directory as your
Add following content:
This prevents your local modules and debug logs from being copied onto your Docker image.
Building your image
Go to the directory that has your Dockerfile and run the following command to build the Docker image.
-t flag lets us tag our image so that it's easier to find in our list of docker images.
Your image will be listed by Docker now.
Run the image
When you run your image with
-d, the container runs in detached mode. It now runs in the background
-p flag redirects a public port (host's port) to a private port inside the container.
http://localhost:3000 and your application should be running.
View Running Containers
If you wish to see all running Docker containers, you can run
If you wish to see the logs of your application.
If you want to go inside your container.
I hope this blog post helped you with Dockerizing your
remix application. Feel free to give feedback here.