Dockerize Yarn with Docker

Riyajath Ahamed
3 min readJun 11, 2023

What is Docker ?

Docker is an open-source platform that enables you to automate the deployment, scaling, and management of applications within containers. Containers are lightweight, isolated environments that package all the necessary dependencies and configurations required to run an application.

To containerize a yarn-based application using Docker

  1. Set up Docker: Install Docker on your machine and ensure it’s running properly.
  2. Create a Dockerfile: In the root directory of your yarn-based application, create a file named Dockerfile (with no file extension) with the same case used using a text editor.
  3. Specify the base image: In the Dockerfile, start by specifying the base image for your container. Typically, you can use a Node.js base image that matches the version of Node.js required by your application. For example, if your application requires Node.js version 14, you can use node:14 as the base image. Add the following line to your Dockerfile:
FROM node:14

4. Set the working directory: Specify the working directory inside the container where your application code will reside. Use the WORKDIR instruction in your Dockerfile, like this:

WORKDIR /app

This will create a directory named /app inside the container and set it as the working directory.

5. Copy the application files: Copy the necessary files from your local machine to the container. Use the COPY instruction in your Dockerfile to copy the package.json and yarn.lock files:

COPY package.json yarn.lock ./

This will copy these files to the /app directory inside the container.

6. Install dependencies: Run the yarn install command to install the dependencies of your application inside the container. Use the RUN instruction in your Dockerfile:

RUN yarn install

This will install the dependencies specified in the package.json file inside the container.

7. Copy the application code: Copy the rest of your application code to the container. Add the following line to your Dockerfile:

COPY . .

This will copy all the files and directories from your local machine to the /app directory inside the container.

8. Specify the startup command: Define the command to run your application when the container starts. Use the CMD instruction in your Dockerfile:

CMD ["yarn", "start"]

This command will execute the yarn start script defined in your package.json file.

9. Build the Docker image: Open a terminal or command prompt in the same directory as your Dockerfile and run the following command to build the Docker image:

docker build -t myapp .

This command will build the Docker image using the Dockerfile and tag it with the name “myapp” (you can change it to a name of your choice).

10. Run the Docker container: Once the Docker image is built, you can run a container using the following command:

docker run -d -p 8080:8080 myapp

This command will start a container using the “myapp” image and map port 8080 of the container to port 8080 on your local machine. Adjust the port mapping according to your application’s requirements.

Can access it by navigating to http://localhost:8080 in your web browser, assuming your application is serving content on port 8080.

--

--