wtsang11 / TechExplore

MIT License
0 stars 0 forks source link

Introduction #186

Open wtsang11 opened 2 years ago

wtsang11 commented 2 years ago

http://localhost/TechNotes/wp-admin/post.php?post=1451&action=edit

VSC: http://localhost/lab/python/utilities/study_codes/opencodes.php?f=/Users/wtsang/Lab/docker/stephen_docker/

Running a gatsby site in docker

https://typeofnan.dev/how-to-serve-a-gatsby-app-with-nginx-in-docker/ -- why running nginx daemon off https://stackoverflow.com/questions/25970711/what-is-the-difference-between-nginx-daemon-on-off-option -- nginx directives http://nginx.org/en/docs/switches.html

-- shutdown all containers docker kill $(docker ps -q)

Running a CRM in docker

-- running a crm container using a local mysql https://hub.docker.com/r/spantree/sugarcrm

Running lemp stack in docker

https://github.com/stevenliebregt/docker-compose-lemp-stack

wtsang11 commented 2 years ago

Getting Started Docker-Compose example

https://docs.docker.com/compose/gettingstarted/

Using Docker-Compose to make a node container and a redis container work together

Redis server name in docker-compose.yml is redis-server The default port of a redis server is 6379 In app/index.js, a redis client to server:

const client = redis.createClient({
  host: 'redis-server',
  port: 6379,
});

Running docker-compose:

-- like: docker run docker-compose up -- like: docker build. -- and -- docker run image docker-compose up --build

-- stop all containers started by compose docker-compose down

wtsang11 commented 2 years ago

Build a React App in a container

Generate the project: npx create-react-app Create a docker file Build an image from the docker file. Note that a non-standard name of a docker file can be used. For example: docker build -f Dockerfile.dev . To avoid duplication of node_modules, delete the folder in project folder if it exists. But, node_modules may be mapped to the container to avoid duplication.

Simple docker file

FROM node:alpine

WORKDIR '/app'

COPY package.json .
RUN npm install

COPY . .

CMD ["npm", "run", "start"]

Using volume for code development changes

docker run -p 3000:3000 -v /app/node_modules -v $(pwd):/app

Simplify run command by mapping folders in docker compose file

version: '3'
services:
  web:
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - '3000:3000'
    volumes:
      - /app/node_modules
      - .:/app
wtsang11 commented 2 years ago

Running tests on a container

docker exec -it npm run test

Put test command in docker compose

-- create a second service just for test

version: '3'
services:
  web:
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - '3000:3000'
    volumes:
      - /app/node_modules
      - .:/app
  tests:
    stdin_open: true
    build:
      context: .
      dockerfile: Dockerfile.dev
    volumes:
      - /app/node_modules
      - .:/app
    command: ['npm', 'run', 'test']

-- docker attach to the test container docker attach -- by default, it attaches to process of pid=1 (primary process) -- which is usually the test process. -- To attach to a container and start a shell docker exec -it "container name" sh

wtsang11 commented 2 years ago

Replace the development server with Nginx for production

-- Dockerfile: two stages

FROM node:alpine as builder
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
RUN npm run build

FROM nginx
COPY --from=builder /app/build /usr/share/nginx/html

-- Running the image docker run -p 8080:80