How do I run Apache 2 on Alpine in Docker? - docker

According to Alpine wiki I have to run rc-service apache2 startafter installation of Apache 2. However, there's no rc-service in the Alpine running inside the container. How do I get the service command to run inside Docker container?

gliderlabs/docker-alpine issue 183 illustrate the docker Alpine image has no service or rc-service.
You can see instead nimmis/docker-alpine-apache based on nimmis/docker-alpine-micro, which includes a runit, used to handle starting and shutting down processes automatically started.
That initd will start the apache2 script, which calls:
exec /usr/sbin/httpd -D FOREGROUND -f /web/config/httpd.conf

Alpine does not have rc-service installed by default. You need to install it (either as part of your Dockerfile build process or manually in the container).
The secret invocation is:
apk add openrc --no-cache
If you want to run it from outside the container (say docker run), then use:
docker run [options etc] bin/ash -c "apk add openrc --no-cache"
PS: rc-service is good for other things and stuff like mariadb (also not included in alpine)

Related

Docker: Keep Ubuntu container running after starting?

I am trying to start a docker container using the ubuntu image:
docker container run -d --name ubuntu_assignment_4 6e4f1fe62
However as soon as I start the container it stops again.
Why does this happen and how can I ensure the container stays running?
The image I am trying to run here is: ubuntu:14.04
If you are going to use the ubuntu:14.04 image without any modifications to it, you would not require a separate Dockerfile. And it is not possible to keep the plain ubuntu:14.04 image running as a container.
You can directly launch the container with an interactive shell using the ubuntu:14.04 image.
docker run -it ubuntu:14.04 /bin/bash
But the plain ubuntu:14.04 image does not have curl pre-installed on it.
You will need a custom Dockerfile for this.
I can't say exactly what is happening without seeing the complete Dockerfile that was used to build the image, but I am pretty certain that the trouble you are having is just because whatever task that is being started inside the container is finishing and exiting.
Docker containers work by having some command assigned (using ENTRYPOINT or CMD directives in the Dockerfile, or as an argument to docker start or docker run on the command line) which is the program that is started when the container loads. The container will live for as long as that task continues to run, and once that program finishes the container will terminate.
To specify the startup entrypoint at the command line, try:
docker create -it [image] /bin/bash
Then start it like this:
docker start -ia [Container ID]
The container will exit once the shell exits, because this is assigning the shell as the entry point.
cURL may not be installed by default. It is possible to install it using apt-get. But again, once the shell is closed, the container will stop and any changes will be lost. As a start, try creating a new directory somewhere, and then add a file called Dockerfile with this content:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl
ENTRYPOINT ["/bin/bash"]
That will create a new image with curl installed. then, from inside the new directory where the Dockerfile was created, use:
docker build .
docker images
which will build a new image, using the Dockerfile as the blueprint.
Once the build finishes, find the image ID for the new container, and run it using:
docker run -it [image id]
Ultimately, to make Docker really useful, the typical approach is to replace that last line in the Dockerfile (ENTRYPOINT ["command"]) with something that will continue running forever (like ENTRYPOINT ["apache2"] or ENTRYPOINT ["redis"] or similar). If you have experience using regular desktop/server OS installs, and full virtual machines like VMWare or VirtualBox, just remember that Docker is very different; the way it works and the patterns used to deploy it are not the same.

Docker container is not running even if -d

I'm french and new here (so I don't know how stack overflow works, his community) I'm gonna try to adapt myself.
So, my first problem is the following :
I run docker container with my image who it created with Dockerfile. (there is DNS container)
In Dockerfile, this container have to start script.sh when it start.
But after use that :
docker run -d -ti -p 53:53 alex/dns
(Use -p 53:53 because DNS.)
I can see my DNS runing at the end of my script.sh but, when I do :
Docker ps -a ; but > container is not running.
I'm novice with docker. I have started to learn it 2days ago.
I tried to add (one by one of course):
CMD ["bash"]
CMD ["/bin/bash"]
to run bash and make sure that does not poweroff.
I tried to add -d in Docker run command
I tried to use :
docker commit ti alex/dns
and
docker exec -ti alex/dns /bin/bsh
My dockerfile file :
FROM debian
...
RUN apt-get install bind9
...
ADD script.sh /usr/bin/script.sh
...
ENTRYPOINT ["/bin/bash", "script.sh]
CMD ["/bin/bash"]
My file script.sh :
service bind9 stop
*It copy en remplace conf file for bind9*
service bind9 restart
I hope that there are not too many mistakes and that I managed to make myself understood
I expect the DNS container stay runing and can use it with docker exec.
But now, after use docker run, the container start en stop juste after my script finish. Yes, the DNS server is runing the container tell me before close [ok] Bind9 running or somthing like that. But after container stop.
I suspect the problem you're facing is that your container will terminate once service bind9 restart completes.
You need to have a foreground process running to keep the container running.
I'm unfamiliar with bind9 but I recommend you explore ways to run bind9 in the foreground in your container.
Your command to run the container is correct:
docker run -d -ti -p 53:53 alex/dns
You may need to:
RUN apt-get update && apt-get -y install bind9
You will likely need something like (don't know):
ENTRYPOINT ["/bind9"]
Googled it ;-)
https://manpages.debian.org/jessie/bind9/named.8.en.html
After you've configured it, you can run it as a foreground process:
ENTRYPOINT ["named","-g"]

How to execute jar file in docker

A jar need to deploy in docker.I know how to write Dockerfile for a running jar.
this jar is a commandline option application.it has serveral arguments.and will be needed to run serveral times with different arguments.
for example. It has arg1,arg2.
User can run with arg1=A,arg2=B then run with arg1=A2. No arg2.
Docker cannot run this, i have specified these arguments when they run and the container stop once the jar main task finished. I need to start another container to run jar.
Don't think this is friendly. My question is in this case, is it not suitable to deploy with docker?
You can configure the container to run a script that will never end just to keep the container running.
As an example you can include the following in the Dockerfile:
RUN echo 'sleep infinity' >> /bootstrap.sh && chmod +x /bootstrap.sh
You can start the container in the following way:
docker run -d --name <container-name> <image> ./bootstrap.sh
To run the jar you can use:
docker exec <container-name> java [arguments]
Having in mind it is a java program and it is OS agnostic you don't have a huge benefit in running inside a container but is possible.
You can use a simple "hack" for this purpose... But I do not think this is the best solution.
Start a container with a process that is not supposed to be ended soon, e.g. bash. Also, lets say you want to use the latest ubuntu image. Then you can start the container with:
$ docker run -d -it ubuntu bash
This starts a ubuntu container and keeps it running as a daemon edit: detached (-d) in the background.
Lets lookup the container's name:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
59104211e795 ubuntu "bash" 2 seconds ago Up 1 seconds jolly_hawking
It is jolly_hawking. Your commands (here: ls /) can then be sent to the container with this command:
$ docker exec jolly_hawking ls /
But that is definitely not the best solution. Maybe just keep this as an example how this might work for you and how Docker containers are working.

Puppet container wont start automatically

So I have created a puppet container for a certificate authority. It works, but does not start correctly. Here is my Dockerfile:
FROM centos:6
RUN yum update -y
RUN rpm -ivh http://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm
RUN yum install -y ruby puppet-server
ADD puppet.conf /etc/puppet/puppet.conf
VOLUME ["/var/lib/puppet/ssl/"]
EXPOSE 9140
#NOTHING BELOW THIS COMMENT RUNS
RUN puppet master --verbose
CMD service puppetmaster start
CMD chkconfig puppetmaster on
CMD []
I can then start the container with the following run(note that I named the image ca-puppet):
docker run -d -p 9140:9140 -it --name ca-puppet \
-v /puppet-host/ssl:/var/lib/puppet/ssl \
ca-puppet bash
The issue is that I need to docker exec into the container and run the following commands to get it started and create the ca certificates in its ssl directory:
puppet master --verbose
service puppetmaster start
chkconfig puppetmaster on
I have a feeling I should be using some other Docker file commands to run the last 3 commands. What should that be?
There can only be one CMD instruction in a Dockerfile. If you list
more than one CMD then only the last CMD will take effect.
also
If the user specifies arguments to docker run then they will override
the default specified in CMD.
However, using the default process manager (e.g., SysV, systemd, etc) in Docker for most mainstream distros can cause problems (without making a lot of modifications). You generally don't need it, however -- particularly if you're only running one application (as is often considered best practice). In a Docker container, you generally want your primary application to be the first process (PID 1).
You can do this by not daemonizing puppet and start it as the default container command via something like:
CMD puppet master --verbose --no-daemonize
And use the Docker host to manage it (via restart policy, etc.).

Why is Docker Tomcat failing to start?

I am trying to build a Tomcat image from a Dockerfile. This is what my Dockerfile looks like:
FROM dockerfile/java
RUN sudo apt-get update
RUN sudo apt-get install tomcat7
EXPOSE 8086
CMD sudo service tomcat7 start && tail -f /var/log/tomcat7/catalina.out.
but when I build an image from this and run the image with
$ docker run tomcat7-test
it gives the following:
Starting Tomcat servlet engine tomcat7 …fail!
I don’t know what is causing the problem. How can I check the logs of this Docker Tomcat? Can anybody tell me what commands I should use in the Dockerfile to run Tomcat?
There is an official Tomcat image you can use. There are links to the Dockerfiles there to checkout and install Tomcat.
If you want to inspect what is going on when you build your dockerfile, just perform the same steps (apt-getting tomcat7 and starting the service) manually after starting an interactive shell inside the dockerfile/java container with this command:
docker -it dockerfile/java bash
There you will be able to check the logs and see what could be going on.
I did install tomcat server in the docker container instead of using the official Tomcat image.
When I start the server I get the fail response but was curl the tomcat server index page.
Also instead of exiting the container, if you detach from it back to your terminal by typing:
ctrl-p then ctrl-q (source)
You can access your webapps from browser using the below URL:
http://<< boot2docker_ip >>:8080
Try running the image with following command
docker run -dt --cap-add SYS_PTRACE -p 8082:8080 tomcat7-test

Resources