Docker 02

Photo by Ian Taylor on Unsplash

Docker 02

How to containerize an application where more than one container is separated. First, we need to place these containers on one network, And to manage the network some of the commands

  • docker network connect [To connect a container to anetwork]

  • docker network create [To create a new network]

  • docker network disconnect [To disconnect a container from the network]

  • docker network inspect [To display detailed information about the network]

  • docker network ls [To list the networks]

  • docker network prune [To remove all the networks]

  • docker network rm [To remove one or more networks]

  1. Create a docker network mongo network

2. start the container mongo in a detached mode in port 27017 and with the environment variable of mongo in the mongo-network with the name MongoDB

  1. starting mongo-express on port 8081 with environment variables in the network mongo-network

we can access the mongo-express on our local host which is connected to mongo internally via container port 27017

what if we have to connect and run 100 containers....?

then enters docker-compose where we can mention all our containers and on one go we can start 100 containers

Docker Compose

docker-compose

  • build [Build or rebuild services]

  • config [Validate and view the Compose file]

  • create [Create services ]

  • down [Stop and remove resources]

  • events [Receive real-time events from containers ]

  • exec [Execute a command in a running container]

  • help [Get help on a command ]

  • images [List images ]

  • kill [Kill containers ]

  • logs [View output from containers ]

  • pause [Pause services ]

  • port [Print the public port for a port binding ]

  • ps [List containers ]

  • pull [Pull service images push Push service images ]

  • restart [Restart services ]

  • rm [Remove stopped containers ]

  • run [Run a one-off command ]

  • scale [Set number of containers for a service ]

  • start [Start services ]

  • stop [Stop services ]

  • top [Display the running processes ]

  • unpause [Unpause services ]

  • up [Create and start containers ]

we have created the YAML file with the below content which says we are running mongo and mongo-express containers with specified instructions.

version: '3'

services:

  mongodb:
    image: mongo
    ports: 
        -27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: password

  mongo-express:
    image: mongo-express
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: admin
      ME_CONFIG_MONGODB_ADMINPASSWORD: password
      ME_CONFIG_MONGODB_URL: mongodb

Now to start the containers we have the command

docker-compose -f file_name.yaml up

Docker Volumes

The data is lost when we restart the application, and we cannot access previously saved information. so we have a problem to solve here: we provide a physical storage system attached to the containers so that even if the container is down, the data inside is lost, but the data outside is recorded in external storage. restored when the container is started

  1. docker run -v /home/mount/data:/var/lib/mysql/data [where on the file system the reference is made]

  2. docker run -v /home/mount/data

  3. docker run -v name:/var/lib/data

version: '3'

services:

  mongodb:
    image: mongo
    ports: 
        -27017:27017
    volumes:
        - db-data:/var/lib/mysql/data
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: password


  mongo-express:
    image: mongo-express
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: admin
      ME_CONFIG_MONGODB_ADMINPASSWORD: password
      ME_CONFIG_MONGODB_URL: mongodb
volumes:
    db-data

Did you find this article valuable?

Support Supraja Manda by becoming a sponsor. Any amount is appreciated!