Setup Docker Production Mode
Akbar Nafisa
Posted on December 18, 2023
For production mode, we only ship the server app, and for the client app, we will upload it to S3. To accomplish this, you need to have an account on DockerHub because we will upload the image and later download it in the production environment. You can view the full code for this part here.
Add Production Dockerfile
In the Dockerfile, we separate the process into multi stage builds. The first build involves the builder
, where we install the package, build it for production mode, and finally, run yarn install
with the --production
flag. This flag removes libraries from npm that are not listed in the dependencies in package.json. The second build is the runner
, where we copy our apps
, package
, and node_modules
from the builder to the runner
.
You can see that the production image is 35 MB smaller than the development image.
Setup Environment Variables
We will create a .env.prod
file to handle variables in production. The values are similar to .env.dev
, with the only change being the database configuration to use the production database.
We also add a new value to create a tag in our Docker image.
Add Production Docker Compose File
In the compose file, we add the image
property in the server services. The image will be pulled from our DockerHub with a configurable tag set in the .env
file.
Next, we add a new script to run Docker Compose in production mode.
Running Docker Compose Production
To run the app, execute the command yarn compose:prod up
. This command will build and create a new container for us to run. Lastly, we need to upload the image to DockerHub. The first step is to log in to Docker Hub; you can run this command:
docker login
Then we need to push the image from our computer to DockerHub using this command:
yarn compose:prod push
Posted on December 18, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.