How can I start Zookeper and Kafka both at the same time? - docker

I wrote a python app that uses Kafka to share data between producer and consumer. I am going to dockerize it and so I wrote the dockerfile. The problem is in the final steps, when I run the Zookeeper server, it says binding to port .... and it never exits, so that it would be possible to run Kafka server also.
FROM ubuntu:18.04
ADD app.py /
RUN apt-get update -y && apt-get install -y apt-transport-https
RUN apt-get update \
&& apt-get install -y wget \
&& rm -rf /var/lib/apt/lists/*
RUN apt-get install gzip
RUN apt-get update \
&& apt-get install -y default-jdk \
&& rm -rf /var/lib/apt/lists/*
RUN wget http://mirrors.estointernet.in/apache/kafka/2.2.0/kafka_2.11-2.2.0.tgz
RUN tar -xvzf kafka_2.11-2.2.0.tgz
RUN cd kafka_2.11-2.2.0 \
&& ./bin/zookeeper-server-start.sh ./config/zookeeper.properties \
&& ./bin/kafka-server-start.sh ./config/server.properties
CMD [ “python”, “.app.py”]

Typically, you would only need to create a container for your own application. There are already numerous containers available for Kafka and Zookeeper.
A suggestion would be to run a simple Kafka stack using docker compose and then either start your application externally (for instance via command-line or IDE) or create a docker container and run it.
A simple Kafka stack with just Kafka and Zookeeper would require a simple docker-compose file with the following (of course you will need to have docker-compose installed on your system):
version: '3'
services:
zookeeper:
image: confluentinc/cp-zookeeper:4.0.0
restart: unless-stopped
ports:
- 2181:2181
environment:
ZOOKEEPER_SERVER_ID: 1
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ZOOKEEPER_INIT_LIMIT: 5
ZOOKEEPER_SYNC_LIMIT: 2
kafka:
image: confluentinc/cp-kafka:4.0.0-2
depends_on:
- zookeeper
ports:
- 9092:9092
environment:
KAFKA_BROKER_ID: 1
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
KAFKA_LOG4J_LOGGERS: "kafka.controller=INFO,kafka.producer.async.DefaultEventHandler=INFO,state.change.logger=INFO"
If you put the above in a file named simple-kafka-stack.yml, you can execute it by running the command:
docker-compose -f simple-kafka-stack.yml up

Related

Jenkins SSH remote hosts Can't connect to server

I can access to the target with ssh password and with the private key from Jenkins bash, I configured SSH sites on jenkins with the same host, User and private key I get the next error:
Docker logs:
2022-09-23 05:06:52.357+0000 [id=71] SEVERE o.j.h.p.SSHBuildWrapper$DescriptorImpl#doLoginCheck: Auth fail 2022-09-23 05:06:52.367+0000 [id=71] SEVERE o.j.h.p.SSHBuildWrapper$DescriptorImpl#doLoginCheck: Can't connect to server
Docker-compose:
version: '3'
services:
jenkins:
container_name: jenkins
image: jenkins/jenkins
ports:
- "8080:8080"
volumes:
- $PWD/jenkins_home:/var/jenkins_home
networks:
- net
remote_host:
container_name: remote-host
image: remote-host
build:
context: fedora
dockerfile: Dockerfile
networks:
- net
db_host:
container_name: db
image: mysql:5.7
environment:
- "MYSQL_ROOT_PASSWORD=PASSWORD"
volumes:
- "$PWD/db:/var/lib/mysql"
networks:
- net
networks:
net:
DockerFile:
FROM fedora
RUN yum update -y
RUN yum -y install unzip
RUN yum -y install openssh-server
RUN useradd RemoteUser && \
echo "RemoteUser:Password"| chpasswd && \
mkdir /home/madchabelo/.ssh && \
chmod 700 /home/madchabelo/.ssh
COPY remote-ki.pub /home/madchabelo/.ssh/authorized_keys
RUN chown madchabelo:madchabelo -R /home/madchabelo/.ssh/ && \
chmod 600 /home/madchabelo/.ssh/authorized_keys
RUN ssh-keygen -A
RUN yum -y install mysql
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
unzip awscliv2.zip && \
sudo ./aws/install
RUN yum -y install vim
CMD /usr/sbin/sshd -D
I try with the IP and I get the same.
regards
When creating a private key, you should create a code with the following command after version ubuntu 20.04.
ssh-keygen -t ecdsa -m PEM -f remote-key
For a more detailed explanation, see the link below:
https://community.jenkins.io/t/ssh-connection-auth-fail/4121/7

Docker containers are running but can't find them

Yesterday I created 1 network and 3 containers for prestashop, phpmyadmin and mysql.
I saved them by running a docker save command and today when I came back my containers are still working on the specified ports however I can't see the containers by running docker ps -a. Also the prestashop folder is not writable anymore.
How can I see where my docker containers that are running ?
docker ps -a :
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker network ls :
NETWORK ID NAME DRIVER SCOPE
e03870bf5fec bridge bridge local
63cfaacfbb2e chrono bridge local
c23a0b85edb9 host host local
1ae1b68b12c0 none null local
docker images :
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 5fab8d650e42 4 hours ago 657MB
prestadock_apache latest 740c640530f3 2 days ago 657MB
phpmyadmin latest f7fd780fedba 7 days ago 469MB
php 7.4-apache 899ab23566b7 7 days ago 414MB
mysql latest c8562eaf9d81 10 days ago 546MB
docker-compose.yml :
version: "3.8"
services:
db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
container_name: chrono_db
restart: always
volumes:
- db-data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=root
networks:
- dev
phpmyadmin:
image: phpmyadmin:latest
container_name: chrono_phpmyadmin
restart: always
depends_on:
- db
ports:
- 8081:80
environment:
PMA_HOST: db
networks:
- dev
apache:
build: php
container_name: chrono_prestashop
ports:
- 8080:80
volumes:
- ./php/vhosts:/etc/apache2/sites-enabled
- ./:/var/www/html
restart: always
networks:
- dev
networks:
dev:
volumes:
db-data:
Dockerfile :
FROM php:7.4-apache
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
RUN apt-get update \
&& apt-get install -y --no-install-recommends locales apt-utils git libicu-dev g++ libpng-dev libxml2-dev libzip-dev libonig-dev libxslt-dev;
RUN echo "en_US.UTF8 UTF8" > /etc/locale.gen && \
echo "fr_FR.UTF-8 UTF-8" >> /etc/locale.gen && \
locale-gen
RUN curl -sSk https://getcomposer.org/installer | php -- --disable-tls && \
mv composer.phar /usr/local/bin/composer
RUN docker-php-ext-configure intl
RUN docker-php-ext-install pdo pdo_mysql gd opcache intl zip calendar dom mbstring zip gd xsl
RUN pecl install apcu && docker-php-ext-enable apcu
RUN a2enmod rewrite && service apache2 restart
RUN chown -R www-data:w
Here is the error after running docker-compose up -d :
ERROR: for chrono_prestashop Cannot start service apache: driver failed programming external connectivity on endpoint chronopiles_prestashop (11458be3c31e5f1107770b98ec5e56c645ac9e68b742fd4eb41ad965bd641e9c): Error starting userland proxy: listen tcp 0.0.0.0:8080: bind: address already in use

Docker Volume Persistence

I am trying to learn Docker. I have installed Docker Desktop on my Windows10 Pro computer.
I started with getting a PHP, MySQL website up and running using Docker.
My docker-compose.yml looks like
version: "3.8"
services:
www:
build: .
ports:
- "80:80"
volumes:
- ./www:/var/www/html
links:
- db
networks:
- default
db:
image: mysql:8.0
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: myDb
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- ./conf:/etc/mysql/conf.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8000:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
and the DockerFile looks like
FROM php:7.3-apache
RUN docker-php-ext-install mysqli
RUN apt-get update \
&& apt-get install -y libzip-dev \
&& apt-get install -y wget \
&& apt-get install -y zlib1g-dev \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-install zip
I have a www folder with an index.php.
When I run
docker-compose up -d
I have the PHP, mySQL site up and running correctly and I can access the index.php with expected results.
So far so good.
Now, I want to change the Dockerfile to setup a php forum website (phpbb) - so I have updated my Dockerfile as follows:
FROM php:7.3-apache
RUN docker-php-ext-install mysqli
RUN apt-get update \
&& apt-get install -y libzip-dev \
&& apt-get install -y wget \
&& apt-get install -y zlib1g-dev \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-install zip
WORKDIR /var/www/html
RUN wget https://download.phpbb.com/pub/release/3.3/3.3.0/phpBB-3.3.0.tar.bz2 \
&& tar -xvjf phpBB-3.3.0.tar.bz2
&& ls -l
When I run
docker-compose build --no-cache
I can see the expected results - i.e, the "ls" command shows all the expected phpBB files in /var/www/html
However, when I run
docker-compose up -d
My container only has the index.php in the /var/www/html (the index.php from the www folder). None of the phpBB files are there.
What am I doing wrong?
The files are there but you are hiding them by your bind mount.
You are bind mounting ./www into the same directory (/var/www/html) which will hide its contents.
here:
www:
...
volumes:
- ./www:/var/www/html
When you are building the image, you correctly see the files but once you run docker-compose the bind mount is created and it hides those files.

Docker-compose container exits with status 0

I am trying to create a docker environment for TYPO3. So far I am creating an application container with apache webserver. But the container always stops with status 0. Is there any tips?
My docker-compose:
version: '3'
services:
app:
build: ./docker/app/
volumes:
- ./app:/app
ports:
- 8080:80
- 8443:443
environment:
- SERVER_NAME=local.typo3.com
db:
image: mysql
build: ./docker/db/
restart: always
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD="root"
- MYSQL_ROOT_USER="root"
The Dockerfile looks like this:
FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update; \
apt-get -y upgrade; \
echo "Europe/Berlin" > /etc/timezone; \
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime;
#Install apache
RUN apt-get update && \
apt-get -y install apache2;
RUN apt-get update;
#Install php
RUN apt-get -y install php7.2 \
php7.2-soap \
php7.2-mysql \
php7.2-xml \
php7.2-cli \
php7.2-json \
php7.2-ldap \
php-curl;
RUN apt-get install -y --no-install-recommends mysql-client;
COPY resources/000-default.conf /etc/apache2/sites-available/
RUN a2ensite 00-default;
CMD ["service apache2 restart"]
Any help will be appreciated.
As David Maze points out, this simply doesn't work in most containerized environments:
CMD ["service apache2 restart"]
Depending on the environment, this will do one of two things:
It could try to contact systemd, which isn't running because you haven't started it. You don't want to start it! Running a process manager inside a container is generally an anti-pattern.
It would start Apache in the background. When this happens, from Docker's perspective the main process has exited, so the container exits. This is probably what is happening to you.
Rather than using the service command, you should start Apache in the foreground by hand, e.g. by running:
httpd -DFOREGROUND
Or, as suggested, building your application on top of an existing image that already has the necessary features.

How to define a docker cli service in docker-compose

I have a docker-compose file that runs a few services.
services:
cli:
build:
context: .
dockerfile: docker/cli/Dockerfile
volumes:
- ./drupal8site:/var/www/html/drupal8site
drupal:
container_name: drupal
build:
context: .
dockerfile: docker/DockerFile.drupal
args:
DOC_ROOT: /var/www/html/drupal8site
ports:
- 80:80
volumes:
- ./drupal8site:/var/www/html/drupal8site
restart: always
environment:
APACHE_DOCUMENT_ROOT: /var/www/html/drupal8site/web
mysql:
image: mysql:5.7
ports:
- 3306:3306
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
I would like to add another service which will be a container in which I could run CLI commands (composer, drush for drupal, php, etc).
The following Dockerfile was how I initially defined the cli service but it stops right after it is run. How do I define it so it is part of my docker-compose, shares my mounted volume, and I can interactively connect to it and run CLI commands on it ?
FROM php:7.2-cli
#various programs
RUN apt-get update \
&& apt-get install vim --assume-yes \
&& apt-get install git --assume-yes \
&& apt-get install mysql-client --assume-yes
CMD ["bash"]
Thanks,
Yaron
If you want to run automated scripts on docker images this is obviously a job for a ci-pipeline. You can use CloudFoundry or OpenStack to do this.
But there are many other questions in this post:
1.) How can i share my mounted volume:
You can pass a volume with the -v option to a container. e.g.:
docker run -it -d -v $(pwd)/localFolder:/exposedFolderFromDocker mydockerhub/myawesomeimage
2.) Can I interactively connect to it and run CLI commands on it
docker exec -it docker_cli_1 bash
I recommend to implement features of an docker-image to the individual docker-images Dockerfile. For example copying and running a prepared shell-script:
# your Dockerfile
FROM php:7.2-cli
#various programs
RUN apt-get update \
&& apt-get install vim --assume-yes \
&& apt-get install git --assume-yes \
&& apt-get install mysql-client --assume-yes
# individual changes
COPY your_script.sh /
RUN chown root:root /your_script.sh && \
chmod 0755 /your_script.sh
CMD ["/your_script.sh"]
# a folder to expose
VOLUME /exposedFolderFromDocker
CMD ["bash"]

Resources