Automatically Start Services in Docker Container
Asked Answered
C

5

12

I'm doing some initial tests with docker. At moment i have my images and I can put some containers running, with:

docker ps

I do docker attach container_id and start apache2 service.

Then from the main console I commit the container to the image.

After exiting the container, if I try to start the container or try to run one new container from the committed image, the service is always stopped.

How can create or restart one container with the services started, for example apache?

Ching answered 4/8, 2013 at 21:50 Comment(0)
C
28

EDIT: I've learned a lot about Docker since originally posting this answer. "Starting services automatically in Docker containers" is not a good usage pattern for Docker. Instead, use something like fleet, Kubernetes, or even Monit/SystemD/Upstart/Init.d/Cron to automatically start services that execute inside Docker containers.

ORIGINAL ANSWER: If you are starting the container with the command /bin/bash, then you can accomplish this in the manner outlined here: https://mcmap.net/q/245729/-run-a-service-automatically-in-a-docker-container

So, if you are starting the container with docker run -i -t IMAGE /bin/bash and if you want to automatically start apache2 when the container is started, edit /etc/bash.bashrc in the container and add /usr/local/apache2/bin/apachectl -f /usr/local/apache2/conf/httpd.conf (or whatever your apache2 start command is) to a newline at the end of the file.

Save the changes to your image and restart it with docker run -i -t IMAGE /bin/bash and you will find apache2 running when you attach.

Caviar answered 9/11, 2013 at 5:59 Comment(5)
/etc/bash.bashrc will get executed on every bash startup which might cause problems if you later want to interact with the container (e.g. executing docker exec -ti ID bash) ... I think this is not the right place to put startup scripts for services like apache2Lavabo
Agreed Petr. I've learned about Docker since I posted this answer, and while it works, it's not at all how we actually use Docker in real-life.Caviar
Why not docker run -i -t IMAGE /usr/local/apache2/bin/apachectl -f /usr/local/apache2/conf/httpd.conf what is bash for?Transformer
vbence, it seems you did not read the comments just above yours and my EDIT to my answer? Yes what you're mentioning is a more proper way, but this answer is from before I learned more about containerization and process initialization, as demonstrated by my EDIT. It's also from before Docker added process injection with "docker exec ..." - so at the time we had few ways to interact with or troubleshoot a container without bash running in it.Caviar
Saying "now how we use Docker in real-life" seems silly. It is perfectly OK to use docker to wrap dependencies for something spun up and thrown away for a build or some other thing that isn't production. Don't feel bad if you're using docker this way.Muddle
A
6

An option that you could use would to be use a process manager such as Supervisord to run multiple processes. Someone accomplished this with sshd and mongodb: https://github.com/justone/docker-mongodb

Actinozoan answered 7/10, 2013 at 5:54 Comment(0)
C
4

I guess you can't. What you can do is create an image using a Dockerfile and define a CMD in that, which will be executed when the container starts. See the builder documentation for the basics (https://docs.docker.com/reference/builder/) and see Run a service automatically in a docker container for information on keeping your service running.

You don't need to automate this using a Dockerfile. You could also create the image via a manual commit as you do, and run it command line. Then, you supply the command it should run (which is exactly what the Dockerfile CMD actually does). You can also override the Dockerfiles CMD in this way: only the latest CMD will be executed, which is the command line command if you start the container using one. The basic docker run -i -t base /bin/bash command from the documentation is an example. If your command becomes too long you could create a convenience script of course.

Colza answered 5/8, 2013 at 11:32 Comment(0)
H
2

By design, containers started in detached mode exit when the root process used to run the container exits. You need to start a Apache service in FOREGROUND mode.

docker run -p 8080:80 -d ubuntu/apache apachectl -D FOREGROUND

Reference: https://docs.docker.com/engine/reference/run/#detached-vs-foreground

Hrvatska answered 2/6, 2016 at 13:57 Comment(0)
F
1

Try to add start script to entrypoint in dockerfile like this;

ENTRYPOINT service apache2 restart && bash
Flatfish answered 27/1, 2020 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.