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?

errordeveloper
  • 6,716
  • 6
  • 41
  • 54
Pedro
  • 2,907
  • 6
  • 34
  • 46

5 Answers5

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://stackoverflow.com/a/19872810/2971199

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.

Community
  • 1
  • 1
damick
  • 1,055
  • 1
  • 10
  • 17
  • 6
    `/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 apache2 – Petr Peller Aug 14 '15 at 10:23
  • 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. – damick Sep 08 '15 at 17:22
  • Why not `docker run -i -t IMAGE /usr/local/apache2/bin/apachectl -f /usr/local/apache2/conf/httpd.conf` what is bash for? – vbence Sep 28 '15 at 09:03
  • 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. – damick Oct 26 '15 at 16:24
  • 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. – sclarson Oct 12 '21 at 16:02
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

bacongobbler
  • 867
  • 6
  • 14
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.

Community
  • 1
  • 1
qkrijger
  • 26,686
  • 6
  • 36
  • 37
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

QoP
  • 27,388
  • 16
  • 74
  • 74
1

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

ENTRYPOINT service apache2 restart && bash