Start service automatic inside Docker container - docker

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.

Related

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 with LAMP running (Ubuntu)

I'm trying to create a Docker (LAMP) image with the following
Dockerfile:
FROM ubuntu:latest
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
apache2 \
mysql-server \
php7.0 \
php7.0-bcmath \
php7.0-mcrypt
COPY start-script.sh /root/
RUN chmod +x /root/start-script.sh && /root/start-script.sh
start-script.sh:
#!/bin/bash
service mysql start
a2enmod rewrite
service apache2 start
I build it with:
docker build -t resting/ubuntu .
Then run it with:
docker run -it -p 8000:80 -p 5000:3306 -v $(pwd)/html:/var/www/html resting/ubuntu bash
The problem is, the MYSQL and Apache2 service are not started.
If I run /root/start-script.sh manually in the container, port 80 maps fine to port 8000, but I couldn't connect to MYSQL with 127.0.0.1:5000.
How can I ensure that the services are running when I spin up a container with the image, and map MYSQL out to my host machine?
You need to change the execution of the script to a CMD instruction.
FROM ubuntu:latest
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
apache2 \
mysql-server \
php7.0 \
php7.0-bcmath \
php7.0-mcrypt
COPY start-script.sh /root/
RUN chmod +x /root/start-script.sh
CMD /root/start-script.sh
Althought this works, this is not the right way to manage containers. You should have one container for your Apache2 and another one for MySQL.
Take a look to this article that build a LAMP stack using Docker-Compose: https://www.kinamo.be/en/support/faq/setting-up-a-development-environment-with-docker-compose
you need multiple images - one for each service or app.
A Docker container is not a virtual machine in which you run an entire stack. It is a virtual application, running one primary process.
If you need php, apache and mysql, then you will need 3 docker containers. one for your php app, one for apache and one for mysql.

Starting Multiple service in Dockefile

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

adding a command docker image

I have built an image using these steps:
download adminer package
wget https://www.adminer.org/static/download/4.2.4/adminer-4.2.4-en.php
mv adminer-4.2.4-en.php adminer.php
create docker file
vi dockerfile
FROM ubuntu
RUN apt-get -y install apache2 php5 php5-curl php5-cli php5-mysql php5-gd mysql-client mysql-server
RUN apt-get -y install postgresql postgresql-contrib
RUN apt-get -y install php5-pgsql
COPY adminer.php /var/www/html/
RUN chmod -R 777 /var/www/html/
build and run
docker build -t shantanuo/adminer1 .
docker run -i -t --rm -p 80:80 --name adminer1 shantanuo/adminer1
I need to run this command to start apache once I am inside the container.
sudo service apache2 start
How do I include this command in the dockerfile? (Build failed after adding CMD for this purpose.)
Is there any other (better) way of installing adminer.php package?
Is it possible to reduce the size of this image?
What you do is opening an interactive bash session and try to start a server.
It would be better if you started your same image in detached mode (-d) instead of -it, and let apache runs.
For that, as commented, you need to start FROM httpd:2.4 which:
has a Dockerfile starting by default apache
has a httpd-foreground scripts launching apache server in foreground.
Even better would be to use a PHP docker image:
FROM php:5.6-apache
That way, you don't even have to install apache or php. You just copy your php application.
Then, if you need to, you can still open a bash session with:
docker exec -it <yourContainer> bash

Docker-machine Port Forwarding on Windows not working

I'm attempting to access my django app running within Docker on my windows machine. I'm using docker-machine. I've been taking a crack at this for hours now.
Here's my Dockerfile for my django app:
FROM python:3.4-slim
RUN apt-get update && apt-get install -y \
gcc \
gettext \
vim \
curl \
postgresql-client libpq-dev \
--no-install-recommends && rm -rf /var/lib/apt/lists/*
EXPOSE 8000
WORKDIR /home/
# add app files from git repo
ADD . server/
WORKDIR /home/server
RUN pip install -r requirements.txt
CMD ["python", "manage.py", "runserver", "8000"]
So that should be exposing (at least in the container) port 8000.
When I use the command docker-machine ip default I am given the IP 192.168.99.101. I go to that IP on port 8000 but get no response.
I went into the VirtualBox to see if forwarding those ports would work. Here is the configuration:
I also tried using 127.0.0.1 as the Host IP. I also tried disabling the windows firewall.
Here's my command for starting the container:
docker run --rm -it -p 8000:8000 <imagename>
I am at a loss on why I am unable to connect on that port. When I run docker-machine ls the url it gives me is tcp://192.168.99.101:2376 and when I go to that it gives me some kind of file back, so I know the docker-machine is active on that port.
Also when I run docker ps I get this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c00cc28a2bd <image name> "python manage.py run" 7 minutes ago Up 7 minutes 0.0.0.0:8000->8000/tcp drunk_knuth
Any help would be greatly appreciated.
The issue was that the server was running on 127.0.0.1 when it should have been running on 0.0.0.0.
I changed the CMD line in the Dockerfile from
CMD ["python", "manage.py", "runserver", "8000"]
to
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
and it now works.

Resources