Skip to content
Prev Previous commit
Next Next commit
feat: TT-511 create build and docker image for production
  • Loading branch information
mikevillarruel committed Feb 9, 2022
commit 40a7d83c8172f9a9fa4992f560249359c588ac0d
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ dist
coverage
Makefile
.gitignore
.env
src/environments/keys.ts
src/environments/.keys.json
33 changes: 22 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ ENV HOME /home/${USERNAME}
RUN useradd -ms /bin/bash ${USERNAME}

WORKDIR ${HOME}/time-tracker-ui
COPY [^.env]* . .
RUN chown ${USERNAME}:${USERNAME} -R ${HOME}
COPY --chown=${USERNAME}:${USERNAME} . .

USER ${USERNAME}
RUN npm cache clean --force && npm install
Expand All @@ -17,18 +17,29 @@ CMD npm run config && ${HOME}/time-tracker-ui/node_modules/.bin/ng serve --host



FROM node:14 AS production
FROM node:14 AS build

ENV USERNAME timetracker
ENV HOME /home/${USERNAME}
WORKDIR /usr/local/app
COPY ./ /usr/local/app/

RUN useradd -m -d ${HOME} -s /bin/bash ${USERNAME}
RUN npm install
RUN npm run build

WORKDIR ${HOME}/time-tracker-ui
RUN chown ${USERNAME}:${USERNAME} -R ${HOME}
COPY --chown=${USERNAME}:${USERNAME} . .


FROM nginx:1.21 AS production

ENV USERNAME nginx

COPY nginx.conf /etc/nginx/conf.d/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? because the default.conf one is being copied after.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I removed that line.

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /usr/local/app/dist/time-tracker /usr/share/nginx/html

RUN chown -R ${USERNAME}:${USERNAME} /var/cache/nginx && \
chown -R ${USERNAME}:${USERNAME} /var/log/nginx && \
chown -R ${USERNAME}:${USERNAME} /etc/nginx/conf.d
RUN touch /var/run/nginx.pid && chown -R ${USERNAME}:${USERNAME} /var/run/nginx.pid

USER ${USERNAME}
RUN npm cache clean --force && npm install
EXPOSE 4200
CMD npm run config && /time-tracker-ui/node_modules/.bin/ng serve --host 0.0.0.0 --disableHostCheck --prod

EXPOSE 4200
49 changes: 38 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
.PHONY: help
help:
@echo "---------------HELP-----------------"
@echo "- make build --> Create docker image with dependencies needed"

@echo "- make build --> Create docker image with dependencies needed for development"
@echo "- make run --> Execute timetracker_ui docker container"
@echo "- make stop --> Stop container"
@echo "- make remove --> Restart container"
@echo "- make remove --> Delete container"
@echo "- make test --> Run all tests on docker container"
@echo "- make stop --> Stop container timetracker_ui"
@echo "- make restart --> Restart container timetracker_ui"
@echo "- make remove --> Delete container timetracker_ui"
@echo "- make test --> Run all tests on docker container timetracker_ui"
@echo "- make publish --> Publish the container image timetracker_ui"

@echo "- make build_prod --> Create docker image with dependencies needed for production"
@echo "- make run_prod --> Execute timetracker_ui_prod docker container"
@echo "- make remove_prod --> Delete container timetracker_ui_prod"
@echo "- make publish_prod --> Publish the container image timetracker_ui_prod"

@echo "- make login --> Login in respository of docker images"
@echo "- make publish --> Publish the container image"
@echo "------------------------------------"

.PHONY: build
Expand Down Expand Up @@ -38,13 +45,33 @@ remove:
.PHONY: test
test:
docker-compose --env-file ./.env up -d
docker exec -it timetracker_ui bash -c "npm test"

.PHONY: login
login:
az acr login --name timetrackerdevregistry
docker exec -it timetracker_ui bash -c "npm run test"

.PHONY: publish
publish:
docker tag timetracker_ui:latest timetrackerdevregistry.azurecr.io/timetracker_ui:latest
docker push timetrackerdevregistry.azurecr.io/timetracker_ui:latest

.PHONY: build_prod
build_prod:
-docker rmi timetracker_ui_prod
docker build --target production -t timetracker_ui_prod -f Dockerfile .

.PHONY: run_prod
run_prod:
docker run -d -p 4200:4200 --name timetracker_ui_prod timetracker_ui_prod
docker logs -f timetracker_ui_prod

.PHONY: remove_prod
remove_prod:
docker stop timetracker_ui_prod
docker rm timetracker_ui_prod

.PHONY: publish_prod
publish_prod:
docker tag timetracker_ui_prod:latest timetrackerdevregistry.azurecr.io/timetracker_ui_prod:latest
docker push timetrackerdevregistry.azurecr.io/timetracker_ui_prod:latest

.PHONY: login
login:
az acr login --name timetrackerdevregistry
12 changes: 12 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server {
listen 4200;

root /usr/share/nginx/html;
index index.html;

server_name _;

location / {
try_files $uri /index.html;
}
}