What are we doing here?

This blog includes a series of videos and references to help new users or enthusiasts better understand how to use open source and free technology tools. The quick links includes more information for accessing many of the tools covered along with other references to learn more for taking advantage of these tools.

Click HERE to see the full list of topics covered!

Connecting new Docker Containers to an Existing Docker Compose

Docker is amazing, but it has it's own way of handling things. 

Docker-Compose is amazing and it to needs to be used the right way. 

This post is about explaining how to attach new containers to existing containers already set up via docker-compose. 

Background:

Docker-Compose, when it brings up a bunch of containers or 'services' puts all of them in the same network with a common subnet and DNS so each service can talk to one-another based on the service name. 

Example:

mongodb://root:mongopwd@mongo:27017/

The 'mongo' in that line is the service name, and to connect say Mongo-Express to that container we can simply call the service name. Without the DNS component of the networking layer, we would need to know the specific IP of the container to connect. The 27017 is the port which is the default for MongoDB. 

Normally with Docker-Compose all the services or containers that run within the config file - normally docker-comopose.yml - will be given their own subnet and DNS to operate so they can just talk to one-another based on the service name. 

Example:

version: '3.1'

services:

  mongo:
    image: mongo
    container_name: mongodb
    restart: unless-stopped
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: mongopwd

  websvr:
    build: ./work
    container_name: web4mongo
    restart: unless-stopped
    volumes:
      - ./work/src:/var/www/html
    ports:
      - 80:80
    links:
      - mongo
    depends_on:
      - mongo

The services are denoted using indentation, and shown as 'mongo' and 'websvr'. Those two can than talk to one-another using 'mongo' or 'websvr' rather than the IP address of each individual container. This is really powerful since it makes the networking portion more easily understood, and also more easily repeatable since containers brought up on one system may get assigned different IP ranges than say on a different system. From a code perspective, just call the service name each time and all of that is handled. 

Adding another container:

However say we want to add a container. Often you need to adjust the docker-compose config, stop / remove the containers, and restart them. If the containers are removed and don't have a mapped volume to store the data already created, than the data could be lost. 

Less than ideal. 

However, with Docker you can map new containers to the network!

First find the network. Run 'docker network list'

 NETWORK ID     NAME                                DRIVER    SCOPE
50711*******   bridge                                bridge    local
ca9cc
*******   host                                  host      local
118ef
*******   lamp_default                          bridge    local
1887b
*******   local-debug_default                   bridge    local
9299d
*******   none                                  null      local
06023
*******   ournoteorganizer_default              bridge    local
186fc
*******   phps3object_default                   bridge    local
2209e
*******   wordpress_default                     bridge    local

An output like the above may be what you see depending on the number of Docker instances you are running or have setup. This gives us the network names which we can use to attach a new container in the network. 

For this example we are adding a Mongo Express which is a web-based graphical UI that helps see and interact with a MongoDB database more easily. It's not necessary and should be disabled (docker stop mongo-express) when not used, but it is clearer, and possibly faster than running through all the mongosh commands.

Here is what we run:

sudo docker run --name=mongoex -p=8082:8081 \
--network=ournoteorganizer_default \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=root \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=mongopwd \
-e ME_CONFIG_MONGODB_URL=mongodb://root:mongopwd@mongo:27017/\
mongo-express

We create the new container using the run command, give it a name, define the port mapping between the host and the container, then assign it to the same network as the the original Docker-Compose group of containers - placing it in the same subnet and DNS / user space. Then we assign the necessary environment varilables to match the existing ones shown in the previous docker-compose.yml file. Finally specific the Docker image as mongo-express. 

This worked a treat for me since I had commented out Mongo Express in my pusblished OurNoteOrganizer application due to security concerns. Now I have a Mongo Express container I can call up when I need it or turn it off when I don't. 

Start: sudo docker start mongoex

Stop: sudo docker stop mongoex

Docker Compose is amazingly powerful, but understanding how it works is important to take full advantage of it. Knowing that it is essentially an automated networking stack + container creation makes it possible to work with without having to destroy containers and possibly lose information.

Ada & Zangemann


Ada & Zangemann is a book by Matthias Kirschner and Sandra Brandstatter that extols the benefits, fun, and importance of software freedom. A fun read for kids, it strives to instill a passion for inventing and tech into kids, and also has an important lesson for adults not to rely on any one person or organization for all their software needs. 

The premise of the story is around the juxtaposition of Ada - a bright girl without the means to afford many of the fancy tech most other families have - and Zangemann - the inventor of said tech. Through necessity, Ada tinkers with broken gadgets and old bicycles to give them a new life and make up for her lack of fancy skateboards and ice cream machines that other children have. In her drive to build the solutions she wants and needs, Ada finds her passion for problem solving, and hardware and software development. 

Zangemann, who is designed with an obvious reference to other high-tech oligarchs, is trapped by his own idiosyncrasies. He tends to go overboard in locking down devices to do only the things he wants to do - like his ice cream flavor of the day rather than letting people choose which flavor they want. He designs the products he likes, but wants them to be used in ways that only he deems appropriate. Obviously, the book is a bit over-the-top in some of the things Zangemann imposes on people, such as the iron that can't iron ties because Zangemann hates ties. That said many of the examples are probably more benign than the data and privacy society has given up to much of the tech giants of the world.

As the story progresses, Ada with her friends become more and more interested in tinkering with inventions and modding the many inventions that Zangemann sells. Zangemann becomes angry about the mods, and tries to get the government to step in and create laws to stop it. Ada and her friends, with the help of family protest, and over time end up not merely overturning the law, but also helping the government by creating new software without the limitations of the Zangemann tech. The government in the story saw the power Zangemann had over them with his digital monopoly, and our hero, Ada, was able to help out with her skills.

The story is a fun, inspiring adventure that I can relate to quite well. I want to be Ada! The political side is important as well, and speaks to the work Matthias Kirschner does as his day job as the President of the Free Software Foundation Europe (FSFE). This blog is really just designed to be a free resource to help people get off the ground and start learning about software, and honestly very little to none of the knowledge that I've learned would have been possible without free and open source software.

Ada & Zangemann is an approachable read that children and politicians alike can understand and hopefully take away some insight into the value of software freedom. Similar to this blog detailing my own experience learning about free tools and writing code, catching that feeling of "wow I built this" is incredibly important for young children. Free and open source code help enable the education, either in the classroom or self-taught, for those just getting started with technical development.

Free tools are resources that are essential for enabling more people to learn and develop new skills. It's very hard for many, myself included, to justify spending money on tools and software when I may not end up using it, or decide it's too difficult to learn, or I lose interest in something. However, if the tools are free, then one can use them and start to learn and develop one's skills further. If the tools are open source, then there is an even greater benefit because the code is more likely to have been peer reviewed, and more people can contribute so the project can become more permanent. Closed-source tools can always be stopped if the maintainer can no longer support the project. Open source is something that can always be maintained.

Ada & Zangemann has a political bent to it, but it is not wrong. Free software and open source are important and honestly need to be protected. This story helps spread that message in an approachable way, and I think is definitely worth a read for both kids and adults. After reading it with my son, he immediately started planning what he wanted to invent.

More info about the story can be found here: https://fsfe.org/activities/ada-zangemann/index.en.html