Hi all, hope you can help! I’m very new to docker / linux based systems so I apologise in advance if my basic knowledge is a bit flaky!
I’m running docker (latest stable version 18.03) on a mac with OSX 10.13.3. The image I’m setting up is to run on a Raspberry Pi 3 running base image of “resin/rpi-raspbian:stretch”
The problem I’m having is getting services to start in the container on run. I’ve looked into CMD / ENTRYPOINT and tried multiple variations from lots of posts (so I havent just taken the easy route and jumped on here) but cannot get them to start automatically.
I actually need three different services to start when the container runs lighttpd / mysql and a custom service written in house. I have tried stripping it back to just get one of the services to start and then take it from there but I cant even get that to work!
I’ve tried running a start up script from command as well as trying to start the individual services from CMD just to get one going but no luck yet.
If I SSH into the container and manually start the services they run no problem so I’m assuming everything they need to work is in the image its just the method to start it automatically on container run that I’m having an issue with.
I expect I’m either misunderstanding the basics of how docker works or getting the start up routine wrong.
The standard pattern is to run these in three separate containers. You can use Docker Compose to coordinate launching them. I’d recommend using the standard mysql container over trying to roll your own, not that it’s complicated but that it’s out there and works just fine.
Hi, thanks for responding. I had already seen this thread and tried what it said but still cant get the service to start automatically. As I have a number of services to start the script file looked like it would do the job but I cant get it to work for a single service let alone all three. Is it possibly anything to do with the way I’m starting the container?
docker run -d -p 80:80/tcp -p 8000:8000/tcp --name skinbase-cont skinbase-rf
I then get into the container using :
exec -it skinbase-cont /bin/bash
Once I’m in I check if the service(s) are runing using :
service mysql status
… Ive tried the methods in the post for starting all three and each one individually but whenever I check none of them are running. If I manually start them using :
service mysql start
They start up and continue running no problem and I can access my app via a local browser.
Thanks again for your help.
Cheers
Rob
Hi David, thanks for posting back, I had read about using separate containers for each service and that was next on my “list of things to learn” however I just wanted to get something “basic” working to get a grip of the basics before moving on to something more advanced.
The problem is I cant even get a single service to start in the container (see my other reply to @garylonbrown). My assumption is I’m doing something fundamentally wrong in the setup / execution of the container so until I can get something basic running (i.e. a single service) I dont want to move on to anything more advanced.
A general rule around Docker is that one container usually does one thing; and if you want a container that does more than one thing, you’re totally responsible for all of the machinery that makes it work. I’m guessing you’ll find a multi-container setup easier.
Containers usually don’t run init processes (especially now that Linux distributions seem to have standardized on the incredibly heavyweight systemd, which does so many system-level things that it can’t really be run in a Docker container) and so commands like “service” and “systemctl” and “/etc/init.d/…” tend to just not work.
If you really want to go down this path, supervisord is a popular process manager, but I still think it will be easier (and better form) to split this up into three containers.
Leave off the /tcp from port assignments then try running after. Before doing anything else, check the docker logs. Use docker ps to get the container ID then do docker logs [containerid] and see if you get any clues there as to what might be failing if anything. If still a problem, can you post your Dockerfile?
A pattern that might work for you is the following:
Make your entry point a shell script. That she’ll script will do three things:
Start MySQL service
Start lighttpd
Whatever business logic is needed for initializing and starting your service
So run.sh might look like
#!/bin/bash
service mysql start
service lighttpd start
python server.py # this is your servIce that runs indefinitely
Now, as others have said, you may want to eventually move to a model with separate containers and using docker compose for orchestration, but this should let you at least get something up and running in the way that’s comfortable for you
Hi all, thanks again for taking the time to assist!
As I said, I get that using multiple containers may be the best option long term but I cant get just one service to spin up and keep the container “active”.
This will copy all files in our root to the working directory in the container
COPY . ./
RUN sudo lighttpd-enable-mod fastcgi &&
sudo lighttpd-enable-mod fastcgi-php &&
sudo chmod +x /etc/skinbase/skinbasesvc &&
sudo chmod 777 /tmp/ &&
sudo chmod 777 /start.sh &&
sudo service mysql start &&
sudo bash /etc/skinbase/mysql_setup.sh &&
sudo mysql -u root -e “CREATE DATABASE skinbase” &&
sudo mysql -u root skinbase < /tmp/skinbase.sql
#Clean up packages and remove install files.
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
Enable systemd init system in container
ENV INITSYSTEM ON
#EXPOSE PORT 80 / 8000 TO ACCESS SITE
EXPOSE 80
EXPOSE 8000
CMD ["/bin/bash","./start.sh"]
The start.sh file contains :
#!/bin/bash
echo “Starting services” &&
sudo service lighttpd start &&
sudo service mysql start &&
sudo service skinbasesvc start &&
echo “Start complete”
If I build / run this container and look at the logs all I get is :
Systemd init system enabled.
Log into the container using : “docker exec -it skinbase-cont /bin/bash” and none of the services are running. If I strip back start.sh to just once service (e.g. lighttpd) again, when I log in nothing is running.