A simple example of a Node App using Microservices, Docker and Nginx.
-
Installation for Windows:
- https://hub.docker.com/editions/community/docker-ce-desktop-windows/
- You may need the Linux Kernel update package: https://docs.microsoft.com/en-us/windows/wsl/wsl2-kernel
$ docker version- You can also run
$ docker run hello-worldto verify that Docker can pull and run images.
-
Configuration:
- Dockerfile is an environment file to specify how Docker should package your application, so it can be reproduced anywhere.
FROMbase container;COPY . /{sourceFolder}copy everything in the current directory to the source folder;WORKDIR /{sourceFolder}all the commands will run inside this sourced folder;RUNrun the main commands/scripts, such as installing all the dependencies:npm install --production;EXPOSE {port}expose the port of this container to be available for others containers;CMDcommand that will run when the container starts running. e.g.:npm start.
- docker-compose.yml is a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration:
$ docker-compose up
- Dockerfile is an environment file to specify how Docker should package your application, so it can be reproduced anywhere.
-
Main Commands:
-
Run a new Container:
$ docker run {IMAGE}- Flags:
--name {CONTAINER}assign a name-p {HOSTPORT}:{CONTAINERPORT}map a port-itinteractive terminal (what's going on)-d {IMAGE}start container in background--hostname {HOSTNAME}assign a hostname--add-host {HOSTNAME}:{IP}add a DNS entry
- Example of running mongo in a Docker container:
$ docker run -d -p 27017:27017 mongo
- Flags:
-
Manage Containers:
$ docker pslist of running containers$ docker ps -alist of all containers$ docker stop {CONTAINER}stop a running container. You can use just the first few characters of it:$ docker stop e8$ docker stop $(docker ps -aq)stop all the containers.$ docker start {CONTAINER}start a stopped container$ docker rm {CONTAINER}delete a container$ docker rm -f {CONTAINER}delete a running container$ docker container prunedelete stopped containers
-
Manage Images:
$ docker imagesshow a list of all images$ docker pull {IMAGE}download an image$ docker rmi {IMAGE}delete an image$ docker images prunedelete dangling images$ docker images prune -adelete all unused images
-
Info & Stats:
$ docker logs {CONTAINER}show the logs of a container$ docker statsshow stats of running containers
-
NGINX is open source software for web serving, reverse proxying, caching, load balancing,...NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.
-
default.conf set the basic configuration for web serving:
-
try_files will look for files in the root (srv/www/static), and if it's not found it will use the @web location/microservice.
root /srv/www/static; location / { # Get static files from nginx first try_files $uri $uri/ @web; } location @web { proxy_pass http://web:3000; }localhost:3000 < --- > localhost:8080
-
