Skip to content

Instantly share code, notes, and snippets.

@ariesmcrae
Last active October 29, 2017 23:09
Show Gist options
  • Save ariesmcrae/ac3e9d4dd7ab2f45758e67072485a2cb to your computer and use it in GitHub Desktop.
Save ariesmcrae/ac3e9d4dd7ab2f45758e67072485a2cb to your computer and use it in GitHub Desktop.
Dockerfile sample for React and yarn. Modifying a React file from your laptop would sync them into the running container.
FROM node:8.8.1-alpine
# Make less message noise during docker build
#ENV NPM_CONFIG_LOGLEVEL warn
ARG app_env
ENV APP_ENV $app_env
# All subsequent commands will now be run from inside of this folder
RUN mkdir -p /usr/app/src
WORKDIR /usr/app
# Copy from laptop's current dir to this image's /usr/app
# Note: everything in your .dockerignore will not be copied across
COPY . .
# Install all dependencies from package.json into /node_modules.
RUN yarn install
CMD if [ ${APP_ENV} = production ]; \
then \
yarn global add http-server \
&& yarn build \
&& cd build \
&& hs -p 3000; \
else \
yarn start; \
fi
# The create-react-app runs on port 3000 by default.
# We've also set the http-server above to run on port 300
EXPOSE 3000
# Build:
# docker build . -t ariesmcrae/react-storybook:$(date +"%Y-%m-%d-%H_%M_%S")
# `.` Tells docker to use the current dir as the build context
# -t Tags this image as it would appear in Docker Hub
# Run:
# docker run --rm -it -v "$(pwd)"/src:/usr/app/src -p 3000:3000 ariesmcrae/react-storybook:2017-10-30-00_27_22
# --rm: Kills the container when we cancel out of this command
# -it: Enables interactive mode, for seeing console output
# -v: Sync the laptop's src folder to the container's src folder. This way our dev server can have live update every time your save a react file.
# -p Binds the container port 3000 to our laptop's localhost 3000:
# Note: docker run gives `sh: production: unknown operand` but looks benign
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment