I want to have a Docker container with image of the Angular application to be served from inside. So user could pull the image, run the application and start developing conveniently.
My current Dockerfile is:
FROM node:20.11.0-alpine
COPY . /ng17/
WORKDIR /ng17
RUN npm install -g @angular/cli
RUN npm install
CMD ["ng", "serve", "--host", "0.0.0.0"]
Docker runs my app on 4201 port and it's fine. Still, the problem is that code is not being compiled realtime like if I would "ng serve" regularly w/o docker. I believe "ng serving" from inside the container resolves it.
My docker flow is basic:
docker build -t ng17:0.1 .
docker run -d -p 4201:4200 ng17:0.1
I suspect the problem somewhere around CMD ["ng", "serve", "--host", "0.0.0.0"] and docker run -d -p 4201:4200 ng17:0.1, but can't figure this out alone. My humble Docker experience tells me the answer connected with exec, bash etc.
ps. I'm learning Docker's key principles and specially to understand how handy it can be in my Frontend development. Next I am to go with docker-compose, fix some local backend etc. Any strategies, pros and cons, setups and just advices of this topic are very appreciated.
This is exactly expected in you current workflow as you are copying files into Docker image once during
docker build, exactly one lineCOPY . /ng17/I suggest you to build simple image by using
You need only angular CLI installed during build, rest will be provided during starting container as you want real-time update
Of course to build it use the same command that you have already:
docker build -t ng17:0.1 .To start a container use a little bit longer command
-p 4201:4200), I think(for development) simpler is using--net hostbut it's about preferences both will work.--rmis just to remove container when it exits as it's not needed, all our changes are mount using-v, so we don't care about other changes inside container and it keeps docker containers cleaner (if you typedocker ps -awithout cleaning old containers list might be long and just eat your disk space)ng serveas I tested doesn't work well with Ctrl+C to stop it, quick fix for it is use--init(it can used with any application that is not correctly handling signals)-tthis addedttyto running container, so whennpm installpackages you will see progress, without this you might think nothing going on as progress bar will not appear-das like to have open console open somewhere and see logs, without this when your angular app fails to compile you have to usedocker log $CONTAINER_NAMEto see what went wrong, it makes life harder. Also when you are done with work just pres Ctrl+C in that console and container exits and everything is cleaned up-v $PWD:/work:rwit will mount your current working directory into running container and this what makes changes in your IDE instantly visible by the container tong servecan reload app. I also added:rwas all changes made by the container are visible on the host, mostly because whennpm installinstall packages that we want to persist between container restarts-u $(id -u):$(id -g), by default most Docker images run asrootbut when you local user(on host machine) is in 99% cases different so, it will end up that some files are owned by your local user and rest(mostlynode_modules) by therootand this makes it harder to deleted when you have to, so force same user in the container as is used in the host machineAll this
docker runcan be changed todocker-compose, so you will be able to setup everything by shorter command:docker-compose upUnfortunately this version has harder to makes correct user id inside the container, so I default it to
1000that is in many cases correct, you can manually change it to correct value or startdocker-composeusingNote: If you have latest docker version you can use
docker composeinstead ofdocker-composecommand (space instead of dash(-))