V
V
Victor Diditsky2016-05-03 04:09:40
Docker
Victor Diditsky, 2016-05-03 04:09:40

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

In console:
sudo docker build -t apache .
sudo docker run -dit --name cont_apache apache /bin/bash service apache2 start

In response to the container code, docker ps is clean. docker logs cont_apache reports starting apache - Starting web server apache2 ...
But the container itself dies for some reason. even though the -d flag is set.
Tried:
sudo docker run -dit --name cont_apache apache service apache2 start

The result is exactly the same.
2. Building the image and running Apache using the CMD
Dockerfile:
FROM ubuntu:14.04

RUN apt-get update && \
    apt-get install -y apache2
  
CMD ["service" "apache2", "start"]

In console:
sudo docker build -t apache .
sudo docker run -dit --name cont_apache apache /bin/bash service apache2 start

Let's start the container, it hangs like a daemon, I switch to the container:
[email protected]:/# service apache2 status
* apache2 is not running
Apache did not start.
Why?
3. Building the image with the forwarding of the bash script and its execution by the
Dockerfile:
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"]

Script apache2_start.sh:
#!/bin/bash

service apache2 start
echo "127.0.0.1"

In console:
sudo docker build -t apache .
sudo docker run -dit --name cont_apache apache

There is no container in docker ps. In the docker logs cont_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

Tried also with running bash from console:
sudo docker run -dit --name cont_apache apache /bin/bash

As a result, the container hangs in docker ps. Docker logs conf_apache is clean.
And in the container itself:
[email protected]:/# service apache2 status
* apache2 is not running

Outcome.
I tried to achieve the desired result in different ways, but none of them worked out for me.
Obviously, I make mistakes in working with docker.
I will be very! I am grateful for the constructive help in this matter, both in general and in what particular case I did wrong.
Of course, these examples are bogus, and you can find a bunch of examples of running an environment with docker compose. But my goal is to understand the issue, and in the future to use all sorts of cool things like docker compose

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
instanti, 2016-05-04
@GhostSt92

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 question

Ask a Question

731 491 924 answers to any question