Running multiple containers is really common - and that's why it a really good idea to learn about it. This is an article following an online tutorial.
Kalle Tolonen
Aug. 31, 2022
First I cloned the demo repo to my home folder.
cd
git clone https://github.com/prakhar1989/FoodTrucks
The tutorial splits the demo app into two containers - one for the Flask process and one for the database(?). I had a previous Flask container so all that was needed was a container for Elasticsearch.
sudo docker search elasticsearch
sudo docker pull docker.elastic.co/elasticsearch/elasticsearch:8.4.0
sudo docker ps
The container is available for use, so we can try it out.
$sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9afb4c55be8d docker.elastic.co/elasticsearch/elasticsearch:8.4.0 "/bin/tini -- /usr/l…" About a minute ago Up About a minute 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp es
c9e14692f9dc prakhar1989/static-site "./wrapper.sh" 26 hours ago Up 26 hours 0.0.0.0:49154->80/tcp, :::49154->80/tcp, 0.0.0.0:49153->443/tcp, :::49153->443/tcp static-site
sudo docker run -d --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:8.4.0
Since the container is now named as “es”, you can refer to it with that name instead of the id. I could not get the container to answer from the tutorial’s IP, so I tried to do things again with the same version as the tutorial had.
docker pull docker.elastic.co/elasticsearch/elasticsearch:6.3.2
sudo docker run -d --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.3.2
This time around I managed to reproduce the results.
"name" : "gvHSlS9",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "0WMvJrL1Q3OQD-VVTGZynQ",
"version" : {
"number" : "6.3.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "053779d",
"build_date" : "2018-07-20T05:20:23.451332Z",
"build_snapshot" : false,
"lucene_version" : "7.3.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}The Dockerfile is as follows:
# start from base
FROM ubuntu:18.04
LABEL maintainer="Prakhar Srivastav <prakhar@prakhar.me>"
# install system-wide deps for python and node
RUN apt-get -yqq update
RUN apt-get -yqq install python3-pip python3-dev curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash
RUN apt-get install -yq nodejs
# copy our application code
ADD flask-app /opt/flask-app
WORKDIR /opt/flask-app
# fetch app specific deps
RUN npm install
RUN npm run build
RUN pip3 install -r requirements.txt
# expose port
EXPOSE 5000
# start app
CMD [ "python3", "./app.py" ]
Notice how piping commands can be done and a working directory can be set for subsequent commands.
sudo docker build -t kalletolonen/foodtruckdemo .
The flag “t” tells that the following string will be the tag for the image and the last period indicates that everything from the working directory should be included in the container.
Let’s examine the networks Docker has creted for us.
sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
041be8b608b8 bridge bridge local
6337965bc456 host host local
8d10629d0230 none null local
Bridge is the network that is that used by default by containers. This can be validated by testing it:
sudo docker network inspect bridge
The resulting JSON tells us that the IP-address of the container.
Containers": {
"648bbcc410082baa4bd1e94a8ae27e9c91c7ded0b38a6040094997465a226bb7": {
"Name": "es",
"EndpointID": "245b53d50d38837c84370d694d6e1d51f3b5199df603d4f8a9534a06c4a2f8b1",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
}
Next - let’s try connectiog to the es-container with the flask-container.
sudo docker run -it --rm kalletolonen/foodtrucktest bash
The it-flag makes the connection look like a regular terminal connection and rm-flag indicates that the container gets cleaned up after it’s exited.
root@8eda7f42cdc4:/opt/flask-app# pwd
/opt/flask-app
root@8eda7f42cdc4:/opt/flask-app# curl 172.17.0.3:9200
{
"name" : "gvHSlS9",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "0WMvJrL1Q3OQD-VVTGZynQ",
"version" : {
"number" : "6.3.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "053779d",
"build_date" : "2018-07-20T05:20:23.451332Z",
"build_snapshot" : false,
"lucene_version" : "7.3.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
That worked as expected.
The previous solution was great for testing the connection, and now we need to create a dedicated network.
sudo docker network create foodtrucknet
~/FoodTrucks$ sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
041be8b608b8 bridge bridge local
946d41a01d2d foodtrucknet bridge local
6337965bc456 host host local
8d10629d0230 none null local
Next it’s time to remove the es container and make a new one with a –net flag to put it into our brand new net.
sudo docker container stop es
sudo docker container rm es
sudo docker run -d --name es --net foodtrucknet -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.3.2
sudo docker network inspect foodtrucknet
"Containers": {
"c96fd38ec00e5bfa4b5c8fabd98fe1cfd7ae012e3aed0dbfcaff39b143870d76": {
"Name": "es",
"EndpointID": "e9e4fe67c6c9a8ab2624eaf01936295969eaf3c9a422af1ac05b27eabc770d44",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
Foodtrucknet has the es container running in it. So let’s see what happens when we launch the Flask-container in the same network.
sudo docker run -it --rm --net foodtrucknet kalletolonen/foodtrucktest bash
root@04f3227e581d:/opt/flask-app# curl es:9200
{
"name" : "OoIkiJY",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "wUf-xq3oQwqR8qHVkXzOCA",
"version" : {
"number" : "6.3.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "053779d",
"build_date" : "2018-07-20T05:20:23.451332Z",
"build_snapshot" : false,
"lucene_version" : "7.3.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
To launch the Flask container without the bash, you just need to change some flags.
sudo docker run -d --net foodtrucknet -p 5000:5000 --name foodtrucktest kalletolonen/foodtrucktest
The -d flag tell Docker to run in detached mode, ie. in the background.

No published comments yet.
Your comment may be published.