An application serving up business contacts, via Node, Express, and React
- Locate the client folder directory.
- Create a new file named
Dockerfile
in 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 . /app
command 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-name
This 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
server
in your project directory. - Using a text editor, create a new file named
Dockerfile
in theserver
folder. - 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-image
docker build -t name-of-image
docker run -p matchPort:matchPort name-of-image
The 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 server
andnpm run start-dev
(runs in watch mode on port 3000) - In the other terminal, run
cd client
andnpm 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)