Ausnahme gefangen: SSL certificate problem: certificate is not yet valid ๐Ÿ“Œ How To: Build and Run a Yarn Project with Docker

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š How To: Build and Run a Yarn Project with Docker


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

This article was originally published on the Shipyard Blog.

Are you new to Docker and Docker Compose? There probably isnโ€™t an easier place to start Dockerizing than with a single-service project that builds with Yarn. Whether you have a specific project in mind or just want to practice your Docker skills, this guide will give you a quick rundown on getting a Yarn app off the ground and into a container.

What is Yarn?

Yarn is a package manager for Node.js. Yarn is an acronym for โ€œYet another resource managerโ€. Itโ€™s a popular option for Javascript projects, second only in popularity to the seemingly-ubiquitous npm.

Fun (tangential) fact: according to npmโ€™s GitHub repo:

  • Contrary to popular belief, npm is not in fact an acronym for "Node Package Manager"; It is a recursive bacronymic abbreviation for "npm is not an acronym"

Running yarn init will get you set up with a pre-configured JSON to start building a Yarn project. You can add dependencies to your project via yarn add [dependency].

Like many package managers, Yarn generates and maintains a โ€œlockfileโ€ called yarn.lock, in which it keeps track of dependencies by version (and their dependencies by version). This way, if your project works at a certain point in time on a particular machine, this state will be preserved.

If you have an existing Yarn project with a package.json file, you can get all dependencies installed with yarn install and you can get it running in a development environment with yarn dev.

Why run a Yarn project in Docker?

One of the (countless) benefits of Docker is that it helps you develop your application to be highly portable (via version control). But doesnโ€™t Yarn essentially do the same thing? While Yarn is great for setting up and running an existing project in just two commands, Docker can take your project even further.

If you want to cloud-host your project, youโ€™ll typically need it containerized. The great thing about Docker containers is that they can be used throughout every stage of development, from local to production. You can also distribute your Docker image so others can run it instantly without any installation or building.

At the very least, if youโ€™re including a Dockerfile with your project, you can ensure it builds and runs identically every single time, on every system.

Writing the Dockerfile for a Yarn project

A simple project that builds with Yarn can be translated into Docker pretty easily. Youโ€™ll follow the classic Dockerfile structure (set the base image, set the working directory, etc.) and then modify it with the project-specific installation instructions, likely found in the README.

Thereโ€™s a great Yarn project on GitHub thatโ€™s an open source spin on a certain popular honeycomb-shaped word game with Vite. In this tutorial, Iโ€™ll be writing a Dockerfile for that particular project, but these instructions can be abstracted to work on other simple Yarn projects.

Step 1: Choosing a base image

Weโ€™ll start with finding a base image to build upon for this project. I chose Node Alpine because itโ€™s lightweight (Alpine Linux-based) but still includes Yarn and any necessary binaries for Node-based applications. This is highly preferable to using an untagged or latest base image, as you want to ensure youโ€™re using the same exact version every time.

FROM node:21-alpine3.19

Step 2: Setting a working directory

Here, we will define the working directory within the container. All paths mentioned after will be relative to this.

WORKDIR /app

Step 3: Installing dependencies

Yarn defines and manages dependencies with its lockfile and the projectโ€™s package.json. Weโ€™ll copy just these two files over to the containerโ€™s working directory and run yarn install.

COPY yarn.lock package.json ./

RUN yarn install

Step 4: Copying remaining contents

Now that all dependencies are installed, we can copy the remaining assets to the working directory. If you donโ€™t want certain files to copy over, include them in the .dockerignore file.

COPY . .

Step 5: Exposing the open port

In this example, Iโ€™m Dockerizing a Vite app, which defaults to port 3000. This is simply for documentation; it doesnโ€™t actually actively do anything.

EXPOSE 3000

Step 6: Executing the startup command

Here, Iโ€™m running the dev command. You can edit what this command does in the projectโ€™s package.json. Set the host option to 0.0.0.0 here (or alongside the dev command in package.json) to enable access to the app from outside the container.

CMD ["yarn", "dev", "--host", "0.0.0.0"]

The resulting Dockerfile

And there we have it โ€” a pretty typical Dockerfile customized for a Yarn project. You may want to modify it to fit your specific project, or use it right out of the box with this repo.

FROM node:21-alpine3.19

WORKDIR /app

COPY yarn.lock package.json ./

RUN yarn install

COPY . .

EXPOSE 3000

CMD ["yarn", "dev", "--host", "0.0.0.0"]

Writing the Docker Compose file for a Yarn project

Any Docker project really isnโ€™t complete without a Compose file. This enables you to define your Docker config options so you can run docker compose up instead of copy/pasting complex docker build and docker run commands. Weโ€™ve already done the heavy lifting in the Dockerfile, so this part is quite simple and standard.

Step 1: Defining the service

After setting the Compose version, youโ€™ll want to define a single service with a name you find relevant to your project. Since this project only has one Dockerfile, we only need a single service.

version: '3.9'

services:
  spelling-bee:

Step 2: Setting build context

The build context is the path that defines what the build has access to. In this case, we want to include the entire repo within the build, so weโ€™ll set the context to . (which is also the default).

 build:
   context: .

Step 3: Setting ports

Here, weโ€™ll map the host port to the container port. In this case, theyโ€™re both 3000. This means youโ€™ll access your app via [localhost:3000](http://localhost:3000) and any requests made will be forwarded to the container at port 3000.

ports:
  - '3000:3000'

Step 4: Adding volumes

The volumes option shares data between the host machine and the containerized app. This will persist any data created while the container is running, so next time you spin it up, youโ€™ll see your app just as you left it. Any changes you make locally to files within these volumes will also be reflected in your container (eg. adding something to the database, editing a template file, etc.).

The syntax for a volume will be the directoryโ€™s local path in your project, followed by a colon, followed by where youโ€™re mounting the directory within the container.

volumes:
  - './src:/app/src'
  - './data:/app/data'

The resulting Compose file

And thatโ€™s all you need โ€” you now have a highly-portable Docker Compose project.

version: '3.9'

services:
  spelling-bee:
    build:
      context: .
    ports:
      - '3000:3000'
    volumes:
      - './src:/app/src'
      - './data:/app/data'

Developing the project with Docker Compose

Now that you have a Docker Compose file, you can launch Docker and run the project locally with the docker compose up command. As long as youโ€™re still defining your host port as 3000, you can access the application live at localhost:3000. Since we have the src and data directories mounted as volumes, any changes you make locally will be reflected in the container.

Bonus: Developing the project with ephemeral environments

If you want on-demand preview environments every time you open a PR on your project, thereโ€™s a quick and easy way to do that. Since this app is now defined with Compose, you can preview your code changes in full-stack, production-like environments with Shipyard. Hereโ€™s how you can do it:

  1. Kick off a 30 day free trial of Shipyard
  2. Connect your git provider
  3. Create a new application and set your base branch
  4. Every time you open a PR, a new environment will automatically spin up

Read this guide for more detailed instructions on deploying your app in a Shipyard ephemeral environment.

Tying it all together

Arguably, one of the greatest places to start Dockerizing is with an existing app that builds with a package manager. This way, the config is already taken care of, and you can just simply insert your specific install, build, and run commands into the classic Dockerfile format. Yarn is one of the greatest examples of how elegant and effortless Docker configurations can be. Now you can enjoy your Yarn app at every stage of the SDLC, courtesy of containers.

Want to chat Docker, Docker Compose, Yarn, or anything else dev-related? Join the Shipyard Community Slack.

...



๐Ÿ“Œ How To: Build and Run a Yarn Project with Docker


๐Ÿ“ˆ 49.74 Punkte

๐Ÿ“Œ Docker Learning for Beginners Part 5: Create Ubuntu Container Using Dockerfile : Docker build CMD RUN Example


๐Ÿ“ˆ 31.6 Punkte

๐Ÿ“Œ Docker Build VS Docker Run


๐Ÿ“ˆ 31.6 Punkte

๐Ÿ“Œ How the 8220 Gang Is Compromising YARN, Docker, Confluence, and Redis Servers for Cryptomining


๐Ÿ“ˆ 30.09 Punkte

๐Ÿ“Œ Docker users unhappy with latest forced login to download Docker and Docker Store images


๐Ÿ“ˆ 28.69 Punkte

๐Ÿ“Œ Hackers Exploit Misconfigured YARN, Docker, Confluence, Redis Servers for Crypto Mining


๐Ÿ“ˆ 28.3 Punkte

๐Ÿ“Œ You can run Fluent and Comsol on Opensuse inside Docker (CentOS7 based) without SystemD if you run license server on host


๐Ÿ“ˆ 27.26 Punkte

๐Ÿ“Œ Mehrere Probleme in containerd, docker-runc, go1.11, go1.12, golang-github-docker-libnetwork, go und docker (SUSE)


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Security: Mehrere Probleme in containerd, docker-runc, go1.11, go1.12, golang-github-docker-libnetwork, go und docker (SUSE)


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Mehrere Probleme in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Mangelnde Rechteprรผfung in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Preisgabe von Informationen in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Preisgabe von Informationen in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Denial of Service in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Preisgabe von Informationen in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Ausfรผhren von Code mit hรถheren Privilegien in docker-runc, golang-github-docker-libnetwork, docker und containerd (SUSE)


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Docker Stack Tutorial | Docker Stack Deploy Docker-Compose.yml


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Mehrere Probleme in docker-runc, golang-github-docker-libnetwork, docker und containerd (SUSE)


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Security: Mehrere Probleme in docker-runc, golang-github-docker-libnetwork, docker und containerd (SUSE)


๐Ÿ“ˆ 26.9 Punkte

๐Ÿ“Œ Fix Cannot Connect to the Docker Daemon at โ€˜unix:///var/run/docker.sockโ€™


๐Ÿ“ˆ 26.19 Punkte

๐Ÿ“Œ Run MacOS VMโ€™s in Docker on Windows & Linux with Docker-OSX


๐Ÿ“ˆ 26.19 Punkte

๐Ÿ“Œ npm vs yarn: Choose the Right Package Manager for Your Next Project


๐Ÿ“ˆ 25.32 Punkte

๐Ÿ“Œ Build a Docker Image for Jupyter Notebooks and run on Cloudโ€™s VertexAI


๐Ÿ“ˆ 24.42 Punkte

๐Ÿ“Œ Medium CVE-2020-7606: Docker-compose-remote-api project Docker-compose-remote-api


๐Ÿ“ˆ 23.92 Punkte

๐Ÿ“Œ High CVE-2021-34079: Docker-tester project Docker-tester


๐Ÿ“ˆ 23.92 Punkte

๐Ÿ“Œ Managing Dependencies in Node.js: An Overview of NPM and Yarn


๐Ÿ“ˆ 21.12 Punkte

๐Ÿ“Œ Implementing Passport Local With Yarn, Typescript, Express and PostgreSQL


๐Ÿ“ˆ 21.12 Punkte

๐Ÿ“Œ JavaScript Dependency Management with npm and Yarn


๐Ÿ“ˆ 21.12 Punkte

๐Ÿ“Œ Installing and Using Yarn on Ubuntu


๐Ÿ“ˆ 21.12 Punkte

๐Ÿ“Œ Run and test DynamoDB applications locally using Docker and Testcontainers


๐Ÿ“ˆ 20.79 Punkte

๐Ÿ“Œ Run and Test DynamoDB Applications locally Using Docker and Testcontainers


๐Ÿ“ˆ 20.79 Punkte

๐Ÿ“Œ Build Docker images faster using build cache


๐Ÿ“ˆ 19.78 Punkte

๐Ÿ“Œ Docker Build Cloud Helps Developers Speed Up Build Time


๐Ÿ“ˆ 19.78 Punkte











matomo