DEV Community

Akbar Nafisa
Akbar Nafisa

Posted on

Setup Docker Production Mode

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.

Image description

You can see that the production image is 35 MB smaller than the development image.

Image description

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.

Image description

We also add a new value to create a tag in our Docker image.

Image description

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.

Image description

Next, we add a new script to run Docker Compose in production mode.

Image description

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
Enter fullscreen mode Exit fullscreen mode

Then we need to push the image from our computer to DockerHub using this command:

yarn compose:prod push
Enter fullscreen mode Exit fullscreen mode

Top comments (0)