Add individual docker run commands

This commit is contained in:
sid palas
2023-01-30 16:51:09 -05:00
parent 6ff07906a5
commit d02d3f8342
9 changed files with 230 additions and 37 deletions

View File

@ -0,0 +1,112 @@
### DOCKER COMPOSE COMMANDS
.PHONY: compose-build
compose-build:
docker compose build
.PHONY: compose-up
compose-up:
docker compose up
.PHONY: compose-up-build
compose-up-build:
docker compose up --build
.PHONY: compose-down
compose-down:
docker compose down
### DOCKER CLI COMMANDS
DOCKERCONTEXT_DIR:=../05-example-web-application/
DOCKERFILE_DIR:=../06-building-container-images/
.PHONY: docker-build-all
docker-build-all:
docker build -t client-react-vite -f ${DOCKERFILE_DIR}/client-react/Dockerfile.3 ${DOCKERCONTEXT_DIR}/client-react/
docker build -t client-react-ngnix -f ${DOCKERFILE_DIR}/client-react/Dockerfile.5 ${DOCKERCONTEXT_DIR}/client-react/
docker build -t api-node -f ${DOCKERFILE_DIR}/api-node/Dockerfile.7 ${DOCKERCONTEXT_DIR}/api-node/
docker build -t api-golang -f ${DOCKERFILE_DIR}/api-golang/Dockerfile.6 ${DOCKERCONTEXT_DIR}/api-golang/
.PHONY: docker-run-all
docker-run-all:
echo "$$DOCKER_COMPOSE_NOTE"
# Stop and remove all running containers to avoid name conflicts
$(MAKE) docker-stop
$(MAKE) docker-remove
docker run -d \
--name db \
-e POSTGRES_PASSWORD=foobarbaz \
-p 5432:5432 \
--restart unless-stopped \
postgres:15.1-alpine
docker run -d \
--name api-node \
-e DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres \
-p 3000:3000 \
--restart unless-stopped \
--link=db \
api-node
docker run -d \
--name api-golang \
-e DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres \
-p 8080:8080 \
--restart unless-stopped \
--link=db \
api-golang
docker run -d \
--name client-react-vite \
-v ${PWD}/client-react/vite.config.js:/usr/src/app/vite.config.js \
-p 5173:5173 \
--restart unless-stopped \
--link=api-node \
--link=api-golang \
client-react-vite
docker run -d \
--name client-react-nginx \
-p 5174:80 \
--restart unless-stopped \
--link=api-node \
--link=api-golang \
client-react-ngnix
docker-stop:
-docker stop db
-docker stop api-node
-docker stop api-golang
-docker stop client-react-vite
-docker stop client-react-nginx
docker-remove:
-docker container rm db
-docker container rm api-node
-docker container rm api-golang
-docker container rm client-react-vite
-docker container rm client-react-nginx
define DOCKER_COMPOSE_NOTE
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
NOTE:
This command runs the example app with a bunch
of individual docker run commands. This is much
easier to manage with docker-compose (see
docker-compose.yml and compose make targets above)
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
endef
export DOCKER_COMPOSE_NOTE

View File

@ -0,0 +1,65 @@
# Running Containers (with Docker)
Documentation: https://docs.docker.com/engine/reference/run/
Options everyone should know:
```
-d
--entrypoint
--env, -e, --env-file
--init
--interactive, -i
--mount, --volume, -v
--name
--network, --net
--platform
--publish, -p
--restart
--rm
--tty, -t
```
Less commonly used by worth knowing:
```bash
--cap-add, --cap-drop
--cgroup-parent
--cpu-shares
--cpuset-cpus (pin execution to specific CPU cores)
--device-cgroup-rule,
--device-read-bps, --device-read-iops, --device-write-bps, --device-write-iops
--gpus (NVIDIA Only)
--health-cmd, --health-interval, --health-retries, --health-start-period, --health-timeout
--memory , -m
--pid, --pids-limit
--privileged
--read-only
--security-opt
--userns
```
## Example web app
### individual docker run commands
See Makefile:
```bash
make docker-build-all
make docker-run-all
```
- Uses the default docker bridge network
- Uses `--link` to enable easy host name for network connections
- Publishing ports useful to connect to each service individually from host, but only necessary to connect to the frontend
- Named containers make it easier to reference (e.g. with link), but does require removing them to avoid naming conflict
- Restart policy allows docker to restart the container (for example if database weren't up yet causing one of the api servers to crash)
### docker compose
See Makefile:
```bash
make compose-build
make compose-up
```
Using docker compose allows encoding all of the logic from the `docker build` and `docker run` commands into a single file. Docker compose also manages naming of the container images and containers, attaching to logs from all the containers at runtime, etc...

View File

@ -0,0 +1,28 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';
import dns from 'dns';
dns.setDefaultResultOrder('verbatim');
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
proxy: {
'/api/golang': {
// TODO: Make this inside and outside of docker (e.g. localhost vs api-golang)
target: 'http://api-golang:8080',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api\/golang/, ''),
secure: false,
},
'/api/node': {
// TODO: Make this inside and outside of docker (e.g. localhost vs api-node)
target: 'http://api-node:3000',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api\/node/, ''),
secure: false,
},
},
},
});

View File

@ -0,0 +1,48 @@
services:
client-react-vite:
build:
context: ../05-example-web-application/client-react/
dockerfile: ../../06-building-container-images/client-react/Dockerfile.3
ports:
- 5173:5173
volumes:
- ${PWD}/client-react/vite.config.js:/usr/src/app/vite.config.js
restart: unless-stopped
client-react-nginx:
build:
context: ../05-example-web-application/client-react/
dockerfile: ../../06-building-container-images/client-react/Dockerfile.5
init: true
ports:
- 5174:80
restart: unless-stopped
api-node:
build:
context: ../05-example-web-application/api-node/
dockerfile: ../../06-building-container-images/api-node/Dockerfile.7
init: true
depends_on:
- db
environment:
- DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres
ports:
- 3000:3000
restart: unless-stopped
api-golang:
build:
context: ../05-example-web-application/api-golang/
dockerfile: ../../06-building-container-images/api-golang/Dockerfile.6
init: true
depends_on:
- db
environment:
- DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres
ports:
- 8080:8080
restart: unless-stopped
db:
image: postgres:15.1-alpine
environment:
- POSTGRES_PASSWORD=foobarbaz
ports:
- 5432:5432