Starting Multiple service in Dockefile - docker

I have one dockerfile as below.
FROM centos:centos6
RUN yum install httpd* -y
RUN yum install mysql* -y
ENTRYPOINT service mysqld start && bash
ENTRYPOINT service httpd start && bash
Docker file running successful but when i enter into the container only one service is in start start that is httpd.
I want to start both the service automatically using dockerfile.
Please let us know how to do that

You should create a entrypoint.sh file:
#!/bin/bash
service mysqld start
service httpd start
And Dockerfile:
FROM centos:centos6
RUN yum install httpd* -y
RUN yum install mysql* -y
COPY ./entrypoint.sh /
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
You also try to use supervisord in your docker image

Related

Docker ENTRYPOINT not run two commands

I have a docker-compose.yml with two services, Grafana and Ubuntu. I'm trying to run Prometheus and node_exporter commands in Ubuntu container through entrypoint but only works for the first command.
Dockerfile:
FROM ubuntu:20.04
ENV PROMETHEUS_VERISION=2.38.0
ENV NODE_EXPORTER_VERISION=1.4.0
RUN apt update -y && apt upgrade -y
RUN apt install -y wget
WORKDIR /
# Install Prometheus
RUN wget https://github.com/prometheus/prometheus/releases/downloa/v$PROMETHEUS_VERISION/prometheus-$PROMETHEUS_VERISION.linux-amd64.tar.gz && \
tar xvfz prometheus-$PROMETHEUS_VERISION.linux-amd64.tar.gz
ADD cstm_prometheus.yml /prometheus-$PROMETHEUS_VERISION.linux-amd64/cstm_prometheus.yml
EXPOSE 9090
# Install Node Exporter
RUN wget https://github.com/prometheus/node_exporter/releases/download/v$NODE_EXPORTER_VERISION/node_exporter-$NODE_EXPORTER_VERISION.linux-amd64.tar.gz && \
tar xvfz node_exporter-$NODE_EXPORTER_VERISION.linux-amd64.tar.gz
EXPOSE 9100
COPY ./cstm_entrypoint.sh /
RUN ["chmod", "+x", "/cstm_entrypoint.sh"]
ENTRYPOINT ["/cstm_entrypoint.sh"]
cstm_entrypoint.sh:
#!/bin/bash
./prometheus-$PROMETHEUS_VERISION.linux-amd64/prometheus --config.file=/prometheus-$PROMETHEUS_VERISION.linux-amd64/cstm_prometheus.yml
./node_exporter-$NODE_EXPORTER_VERISION.linux-amd64/node_exporter
When check the services on web browser i have access to:
grafana: 0.0.0.0:3000
prometheus: 0.0.0.0:9090
but not for node_exporter on 0.0.0.0:9100
Anybody could help me please?
Thanks in advance.
Your script waits for Prometheus to finish before it starts node_exporter. Try adding a & at the end of the Prometheus command to have it detach from the shell. Then the script will continue and run the node_exporter command. Like this
#!/bin/bash
./prometheus-$PROMETHEUS_VERISION.linux-amd64/prometheus --config.file=/prometheus-$PROMETHEUS_VERISION.linux-amd64/cstm_prometheus.yml &
./node_exporter-$NODE_EXPORTER_VERISION.linux-amd64/node_exporter

Docker exits container

I am trying to build my own docker image for apache2 and PHP. Can anyone tell my why my container exits after run when it supposes to run ["apache2ctl", "-D", "FOREGROUND"]?
FROM ubuntu:latest
RUN apt update -y && apt upgrade -y
RUN apt install software-properties-common -y
RUN add-apt-repository ppa:ondrej/php -y
RUN apt update -y && apt upgrade -y
ARG DEBIAN_FRONTEND=noninteractive
RUN DEBIAN_FRONTEND=noninteractive apt install -y nano vim iputils-ping sudo git curl php php-cli php-fpm
RUN apt install -y php-json php-mysql
RUN apt install -y php-zip php-mbstring php-curl php-xml php-pear php-bcmath
RUN apt install psmisc -y
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOF_DIR /var/log/apache2
# RUN useradd -ms /bin/bash devwl
EXPOSE 80/tcp
ENTRYPOINT ["/bin/sh", "-c"]
CMD ["apache2ctl", "-D", "FOREGROUND"]
Build command:
docker build -t www .
Run command:
docker run -itd -p 80:80 www
Ouput docker ps:
Just tried to build your Dockerfile. docker logs shows a problem with start command. Running container without -D option works well...
CMD ["apache2ctl", "start"]
Do you need to use <IfDefine ...> in conf files?
You need to delete the ENTRYPOINT line.
Since you have both an ENTRYPOINT and a CMD, they get combined together into a single argument list. That means you have an effective command like
ENTRYPOINT+CMD ["/bin/sh", "-c", "apache2ctl", "-D", "FOREGROUND"]
But sh -c only reads in the single next argument and executes it. The remaining arguments would be accessible inside that string as positional parameters $0, $1, ... but unless you refer to one of those then the command you're eventually running is only apachectl with no arguments.
You only need to invoke a shell at all if your command uses shell features (referencing environment variables or running multiple commands). Yours doesn't, so you don't need anything that mentions a shell; just delete the ENTRYPOINT and have the existing CMD start Apache.
In a Dockerfile, you shouldn't usually need to say sh -c at all. If you do need to invoke a shell to run some command, you can use Docker shell syntax as a plain string without the JSON-array syntax; for example
# needs a shell because of $VARIABLE and command; command syntax
CMD touch "$APACHE_LOG_DIR/started"; exec apache2ctl -DFOREGROUND
(If you do need to override this command with docker run arguments or in a Compose command:, these syntaxes will not automatically insert a shell wrapper and there you do need to specifically say sh -c 'some command' if you need a shell to process the command string; again note the single quotes to make the command string a single argument.)

uwsgi-nginx in docker not works

I have Dockerfile like:
FROM python:3.6.5-jessie
MAINTAINER twitter myname
RUN apt-get update
RUN apt-get install -y git
RUN apt-get install -y nginx
RUN pip install --upgrade pip
RUN git clone https://github.com/hongmingu/requirements
RUN pip install -r /requirements/requirements_django.txt
RUN apt-get install -y vim
RUN mkdir -p /uwsgi_log
RUN git clone https://github.com/hongmingu/smaple_django
RUN apt-get install -y nginx
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./uwsgi.ini /uwsgi.ini # it runs in daemonized mode
# These files are just setting files. nginx get request at port 8000 and uwsgi runs django project.
RUN uwsgi --ini /uwsgi.ini
RUN service nginx restart
CMD ["python3"]
I think these 2 lines not work:
RUN uwsgi --ini /uwsgi.ini
RUN service nginx restart
Because When I build it and run it with linux command: sudo docker run --rm -it -p 8080:8000 hongmingu/smaple:0.1 /bin/bash my 127.0.0.1:8080 does not work. But, When I attach container and type command manually like, uwsgi --ini /uwsgi.ini and service nginx restart, It works well.
So, Is it impossible to run uwsgi, nginx in Dockerfile?
I want to do it so that I hope I don't need to run uwsgi and nginx manually.
Where did I make fault? Is there any good way to do this?
This docker image(hongmingu/smaple:0.1) is here: https://cloud.docker.com/u/hongmingu/repository/docker/hongmingu/smaple
You misunderstood the RUN instruction
The RUN instruction will execute any commands in a new layer on top of
the current image and commit the results
It's used to build your image, it is not docker run which executes the command in the container.
The solutions involves to execute those 2 lines in the CMD or ENTRYPOINT with a shell script. uwsgi has also to be daemonized. Checkout this image https://github.com/tiangolo/uwsgi-nginx-docker

Dockerfile supervisord cannot find path

For some reason supervisord cannot start up when executing docker run... If I log out the path where the configuration is stored for supervisord I can clearly see that the file is present.
Below is the part of my Dockerfile thats not currently commented out.
FROM ubuntu:16.04
MAINTAINER Kevin Gilbert
# Update Packages
RUN apt-get -y update
# Install basics
RUN apt-get -y install curl wget make gcc build-essential
# Setup Supervisor
RUN apt-get -y install supervisor
RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD ["/usr/bin/supervisord", "-c /etc/supervisor/conf.d/supervisord.conf"]
Here is the error I get in terminal after running.
remote-testing:analytics-portal kgilbert$ docker run kmgilbert/portal
Error: could not find config file /etc/supervisor/conf.d/supervisord.conf
For help, use /usr/bin/supervisord -h
Try with the exec form of CMD:
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
or with the shell form
CMD /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
Depending on the OS used by the base image, you might not even have to specify the supervisord.conf in the command line (see this example, or the official documentation)
It happended to me on Alpine linux 3.9, but eventually ran successfully with
CMD ["supervisord", "-c", "<path_to_conf_file>"]

Start service automatic inside Docker container

I'm trying to start a service like Apache2 automatic inside a Docker container
My Dockerfile:
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get -y install apache2
ADD ./startup.sh /opt/startup.sh
RUN chmod +x /opt/startup.sh
CMD ["/bin/bash", "/opt/startup.sh"]
RUN /opt/startup.sh
My startup.sh:
#!/bin/bash
service apache2 start
But Apache2 isn't started automatic in the container.
Containers by themselves have no capability to start services in the traditional sense that you're used to, eg. by using upstart or systemd. So you just have to start apache manually...
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get -y install apache2
EXPOSE 80 443
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Remember that when you start the container you will need to map the port correctly with the -p parameter. The dockerfile doesn't deal with any VOLUMES, this simply installs apache2 and starts it. If you need to understand how those work, you'll need to consult the Dockerfile Reference.

Resources