An application serving up business contacts, via Node, Express, and React
- Locate the client folder directory.
- Create a new file named
Dockerfilein the client directory. - Copy the following instructions into the
Dockerfile:
FROM node
WORKDIR /app
COPY . .
RUN npm install && npm run build
EXPOSE 1234
CMD ["node", "server.js"]
-
Make sure that all the project files are inside the directory, otherwise the
COPY . /appcommand will not work correctly. -
In the terminal, navigate to the project directory and run the following command:
docker build -t project-name This command will build the Docker image and tag it as "project-name".
- To run the container, use the following command:
docker run -p 1234:1234 project-nameThis command will run the container and map the host port 1234 to the container's port 1234.
- The service can be accessed via http://localhost:1234
- Create a new folder named
serverin your project directory. - Using a text editor, create a new file named
Dockerfilein theserverfolder. - Copy the following instructions into the
Dockerfile:
FROM node
ENV NODE_ENV=development
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "server.js" ]- In the terminal, navigate to the server directory and run the following command:
docker build -t server-image This command will build the Docker image and tag it as "server-image".
- To run the container, use the following command:
docker run -p 3000:3000 server-imagedocker build -t name-of-image
docker run -p matchPort:matchPort name-of-imageThe point of this example is not to run the apps independently, but if you prefer, or if you're developing/updating them, you can run them independently:
- Open 2 terminals
- In one terminal, run
cd serverandnpm run start-dev(runs in watch mode on port 3000) - In the other terminal, run
cd clientandnpm start(runs in watch mode on port 1234) - Visit the app at localhost:1234
🚨 NOTE 🚨 If you have run the app locally first via above commands, you'll need to delete the node_modules directory in at least the client project. (this is because of a bug in one of the dependencies of Parcel, and a different version of Parcel is used in a Linux environment than in a Mac environment)
Once you have created the docker-compose.yml:
- If not done already, install Docker Desktop
- run
docker-compose up - Visit the app at localhost:1234
- To stop the app:
docker-compose down --rmi all(this also removes all images)