Quantcast
Channel: Active questions tagged dockerfile - DevOps Stack Exchange
Viewing all articles
Browse latest Browse all 136

Docker Compose: How do you build an image while running another container?

$
0
0

I'm trying to build a Nuxt 3 application using docker compose. However, I'm having trouble with docker compose and dockerfile configuration.

Context

To give background on the project that I'm working on, here are the stack that I'm using:

  • Nuxt 3 for SSG
  • Strapi for CMS
  • Nginx for serving static contents generated from Nuxt 3

So, in Nuxt 3 I can run npx nuxi generate to pre-render/generate static contents for the website. After generating the static HTML files, nginx will serve the static contents. Strapi will serve as headless CMS, providing contents for the website.

Problem

The problem is that, when pre-rendering a static contents in Nuxt 3, it requires Strapi backend to run so that it can fetch the contents from Strapi CMS and generate the html files.

So in terms of command line, I need to make sure that Strapi server is running (strapi start) and only then generate the static files (npx nuxi generate) or else there would be an error during build process because Nuxt application cannot fetch resource from the strapi server.

When using docker, I need to make sure that Strapi server container is running while building the Nuxt/node.js image. However, I don't know how I'm supposed to run a container (strapi server) while building an image (Nuxt SSG contents).

Files

Dockerfile.client

# Nuxt ImageFROM node:16-alpine as buildWORKDIR /appCOPY ./client .RUN npm installRUN npm run generate# Nginx ImageFROM nginx:1.15 as publishCOPY --from=build /app/.output/public/ /usr/share/nginx/htmlCOPY ./nginx/nginx.conf /etc/nginx/conf.d/default.confEXPOSE 80ENTRYPOINT ["nginx", "-g", "daemon off;"]

Dockerfile.server

# Strapi ImageFROM node:16-alpine# Installing libvips-dev for sharp CompatabilityRUN apk update && apk add  build-base gcc autoconf automake zlib-dev libpng-dev nasm bash vips-devARG NODE_ENV=productionENV NODE_ENV=${NODE_ENV}WORKDIR /opt/COPY ./server/package.json ./server/yarn.lock ./ENV PATH /opt/node_modules/.bin:$PATHRUN yarn config set network-timeout 600000 -g && yarn installWORKDIR /opt/appCOPY ./server .RUN yarn buildEXPOSE 1337CMD ["yarn", "start"]

docker-compose.yml

In docker-compose.yml file, I specified three services (client, server, database) and all of them belong to one network. I made sure that client depends_on server and database.


I just started learning docker few days ago and it's my first time doing DevOps so I might be missing the mark... Any help will be appreciated.


Edit 1:

I tried separating docker-compose.yml into two:

  1. docker-compose.yml for strapi server and database service
  2. docker-compose.client.yml for Nuxt service

I ran docker compose up for running Strapi server first. After Strapi container was running, I tried running docker compose -f docker-compose.client.yml up --build to build Nuxt image and hoping it would refer to the running strapi container. Unfortunately, Nuxt image was not referring to the running strapi container.


Viewing all articles
Browse latest Browse all 136

Trending Articles