Answer the question
In order to leave comments, you need to log in
How to execute scripts when starting a docker container?
Hello!
The question may be a little beaten, but I did not find an answer to it. And similar questions on the toaster, as in Google, did not help.
I will be very grateful to those people who will take a little of their time and give a detailed answer!
And so, the minimum task is to start apache at the start of the container
The maximum task is to execute the bash script at the start of the container
I will describe the options that I tried.
1. Building a simple image and running the container from the
Dockerfile console
FROM ubuntu:14.04
RUN apt-get update && \
apt-get install -y apache2
sudo docker build -t apache .
sudo docker run -dit --name cont_apache apache /bin/bash service apache2 start
sudo docker run -dit --name cont_apache apache service apache2 start
FROM ubuntu:14.04
RUN apt-get update && \
apt-get install -y apache2
CMD ["service" "apache2", "start"]
sudo docker build -t apache .
sudo docker run -dit --name cont_apache apache /bin/bash service apache2 start
FROM ubuntu:14.04
RUN apt-get update && \
apt-get install -y apache2
COPY ./apache2_start.sh /root/apache2_start.sh
CMD ["chmod", "777", "/root/apache2_start.sh"]
CMD ["/root/apache2_start.sh"]
#!/bin/bash
service apache2 start
echo "127.0.0.1"
sudo docker build -t apache .
sudo docker run -dit --name cont_apache apache
* Starting web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
*
127.0.0.1
sudo docker run -dit --name cont_apache apache /bin/bash
[email protected]:/# service apache2 status
* apache2 is not running
Answer the question
In order to leave comments, you need to log in
Most likely the problems are related to the fact that in all the methods that you have tried, apache is launched in a background process that is spawned from the main one, after which the main process exits. From the point of view of the docker, the container also terminates its work.
To prevent this from happening, start apache with the key FOREGROUND
, for example:
Here you can read more about why this happens, about the docker philosophy and processes inside the container: https://blog.phusion.nl/2015/01/20/docker-and-the- ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question