Sharing Docker directory to another container – sidecar appraoch
Quote from moshe on 24/10/2020, 9:18 amProblem
User wants to user create sidecar style storage
- Benefits to make the docker as portable as possible
Create your first container
Create a file Dockerfile.storage with the following contentsFROM ubuntu:latest
RUN apt update && apt install openssh-server sudo -y
RUN useradd -rm -d /home/ubuntu -s /bin/bash -g root -G sudo -u 1000 test
RUN echo ‘test:test’ | chpasswd
RUN service ssh start
RUN mkdir -p /var/storageEXPOSE 22
CMD [“/usr/sbin/sshd”,”-D”]Build first container
sudo docker build -t storage -f Dockerfile.storage .
Run your container
sudo docker run -d -p 2222:22 storage -v /var/storage
Note
Parameter “-v /var/storage” expose this out as volume for another container
Create Second Container with NGINX
## Create a file Dockerfile.nginx with the following contentsFROM ubuntu:latest
## Install nginx
RUN \
add-apt-repository -y ppa:nginx/stable && \
apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/* && \
echo “\ndaemon off;” >> /etc/nginx/nginx.conf && \
chown -R www-data:www-data /var/lib/nginx# Define mount directories.
VOLUME [“/etc/nginx/sites-enabled”, “/etc/nginx/certs”, “/etc/nginx/conf.d”, “/var/log/nginx”, “/var/www/html”]# Define working directory.
WORKDIR /etc/nginx# Define default command.
CMD [“nginx”]# Expose ports.
EXPOSE 80
EXPOSE 443Build Docker nginx container
sudo docker build -t nginx -f Dockerfile.nginx .
Run your container
sudo docker run -d –volumes-from storage —env TAGS=“nginx” nginx
Note
“–volumes-from app_x” option mount data volumes from container “storage” and binds it into “nginx” container.
Files generated by “storage” in these directories will also be accessible to “nginx” container.
Problem
User wants to user create sidecar style storage
- Benefits to make the docker as portable as possible
Create your first container
Create a file Dockerfile.storage with the following contents
FROM ubuntu:latest
RUN apt update && apt install openssh-server sudo -y
RUN useradd -rm -d /home/ubuntu -s /bin/bash -g root -G sudo -u 1000 test
RUN echo ‘test:test’ | chpasswd
RUN service ssh start
RUN mkdir -p /var/storageEXPOSE 22
CMD [“/usr/sbin/sshd”,”-D”]
Build first container
sudo docker build -t storage -f Dockerfile.storage .
Run your container
sudo docker run -d -p 2222:22 storage -v /var/storage
Note
Parameter “-v /var/storage” expose this out as volume for another container
Create Second Container with NGINX
## Create a file Dockerfile.nginx with the following contents
FROM ubuntu:latest
## Install nginx
RUN \
add-apt-repository -y ppa:nginx/stable && \
apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/* && \
echo “\ndaemon off;” >> /etc/nginx/nginx.conf && \
chown -R www-data:www-data /var/lib/nginx# Define mount directories.
VOLUME [“/etc/nginx/sites-enabled”, “/etc/nginx/certs”, “/etc/nginx/conf.d”, “/var/log/nginx”, “/var/www/html”]# Define working directory.
WORKDIR /etc/nginx# Define default command.
CMD [“nginx”]# Expose ports.
EXPOSE 80
EXPOSE 443
Build Docker nginx container
sudo docker build -t nginx -f Dockerfile.nginx .
Run your container
sudo docker run -d –volumes-from storage —env TAGS=“nginx” nginx
Note
“–volumes-from app_x” option mount data volumes from container “storage” and binds it into “nginx” container.
Files generated by “storage” in these directories will also be accessible to “nginx” container.
Quote from moshe on 24/10/2020, 11:45 amMount without directories just for test nginx
docker run -d -p 80:80 nginxMount with directories from Docker Machine
docker run -d -p 80:80 -v <sites-enabled-dir>:/etc/nginx/conf.d -v <certs-dir>:/etc/nginx/certs -v <log-dir>:/var/log/nginx -v <html-dir>:/var/www/html nginx
Mount without directories just for test nginx
docker run -d -p 80:80 nginx
Mount with directories from Docker Machine
docker run -d -p 80:80 -v <sites-enabled-dir>:/etc/nginx/conf.d -v <certs-dir>:/etc/nginx/certs -v <log-dir>:/var/log/nginx -v <html-dir>:/var/www/html nginx
Quote from moshe on 25/10/2020, 10:53 amAnother example with Network Sharing between containers
This assumes you already have containers you wants to share network
Start rabbitMQ
docker run -d -e RABBITMQ_NODENAME=my-rabbit –name my-rabbit -p 9419:9419 rabbitmq:3-management
Start rabbitmq_exporter in container.
docker run -d –net=container:my-rabbit kbudde/rabbitmq-exporter
Another example with Network Sharing between containers
This assumes you already have containers you wants to share network
Start rabbitMQ
docker run -d -e RABBITMQ_NODENAME=my-rabbit –name my-rabbit -p 9419:9419 rabbitmq:3-management
Start rabbitmq_exporter in container.
docker run -d –net=container:my-rabbit kbudde/rabbitmq-exporter