Monday, June 14, 2021

docker compose

 For multi-container setup use docker-compose


We can use docker build and docker run in a single configuration file and some orchestration commands(build,start,stop,etc)


This doesn't not replace:

- dockerfiles for custom images.

- images or containers

Docker compose is not suited for managing multiple containers on different hosts


In docker compose file, we create a service and it internally create multiple connected containers.


install docker extension in vscode to get auto completion.


Diferent specifications are available in this link: docs.docker.com/compose/compose-file/


case and indentation is mattered in compose file.


to run the service:

docker-compose up

docker-compose up -d -->in detached mode


to delete the service:

docker-compose down

docker-compose down -v -->to delete volumes also


project root folder\docker-compose.yaml

--------------------

version: "3.8" #version of docker compose specification

services:

  mongodb: 

    image: 'mongo'

    volumes: 

      - data:/data/db

    #environment:

      #MONGO_INITDB_ROOT_USERNAME: max

      #- MONGO_INITDB_ROOT_USERNAME=max

      #MONGO_INITDB_ROOT_PASSWORD: secret

    env_file:

      - ./env/mongo.env

    #networks:

    # - goals-net

  backend:

    #build:./backend   #if the image is not available and we need to build it, provide the folder name of dockerfile(if any changes then builds)

    build:

      context: ./backend #to provide a root folder if we require other sub folders also

      dockerfile: Dockerfile

      #args:

        #some-arg: value

    ports:

      - '80:80'

    volumes:

      - logs:/app/logs

      - ./backend:/app

      - /app/node_modules

    env_file:

      - ./env/backend.env

    depends_on: 

      - mongodb #<service_name>

  frontend:

    build:./frontend

    ports:

      - '3000:3000'

    volumes:

      - ./frontend/src:/app/src

    #for -it we add below two,even we run in -d mode the compose file.

    stdin_open: true

    tty: true

    depends_on:

      - backend

volumes:

 #all the named volumes created above should be listed here,if same volume specified in other containers, they will share

 data:

 logs:



env/backend.env:

----

MONGODB_USERNAME=max

MONGODB_PASSWORD=secret


env/mongo.env

---

MONGO_INITDB_ROOT_USERNAME=max

MONGO_INITDB_ROOT_PASSWORD=secret


when we remove service, the containers will be removed.

network is not required separately as a network(foldername_default) is created for all the containers in the compose.

if specified, one more will be created and added to the containers.


The container name will de different as it contains foldername_service_incrementNumber and other paddings.

We can set the custom name to container using container_name parameter to service.

But you can refer to the internal urls for communication using just the service name.Like just http://mongodb



docker-compose up --build ---> force rebuild of all images before creating services

docker-compose build -->only build of images set in build section


utility containers:

---------

only environments and some custom parameters, we can use them as utilities/extra tools instead of having the main application

docker exec -it <container_name> npm init

docker run -it node npm init --->without interactive mode, the node container will quit immediately.


Dockerfile:

----

FROM node:14-alpine

WORKDIR /app

#CMD npm init


docker-complete build -t node-util .

docker run -it -v /users/max/docker-complete:/app node-util npm init --> this will be used to create the package.json file

docker run -it -v /users/max/docker-complete:/app node-util npm install


we can keep a default command to the dockerfile so if we don't pass the (npm install/npm init), it will pick the default


Dockerfile:

----

FROM node:14-alpine

WORKDIR /app

#CMD ["executable"]

ENTRYPOINT["npm"] -->this will be added before the commands we pass.


docker run -it -v /users/max/docker-complete:/app mynpm init

docker run -it -v /users/max/docker-complete:/app mynpm install express --save


using utility containers using docker-compose:

-------

docker-compose.yaml

-----

version: "3.8"

services:

  npm:

    build: ./

    stdin_open: true

    tty: true

    volumes:

    - ./:/app

    

#docker-compose up -->fails as it requires entry commands.

docker-compose run npm init

docker-compose run --rm npm init -->to remove contaienrs created using run instead of up.



No comments:

Post a Comment