DaveStance
DaveStance

Reputation: 421

How can I run configuration commands after startup in Docker?

I have a Dockerfile set up to run a service that requires some subsequent commands be run in order to initialize properly. I have created a startup script with the following structure and set it as my entrypoint:

  1. Set environment variables for service, generate certificates, etc.
  2. Run service in background mode.
  3. Run configuration commands to finish initializing service.

Obviously, this does not work since the service was started in background and the entry point script will exit with code 0. How can I keep this container running after the configuration has been done? Is it possible to do so without a busy loop running?

Upvotes: 0

Views: 1325

Answers (2)

ofekp
ofekp

Reputation: 525

You can look at this GitHub issue and specific comment - https://github.com/docker-library/wordpress/issues/205#issuecomment-278319730

To summarize, you do something like this:

version: '2.1'
services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
  wordpress:
    image: wordpress:latest
    volumes:
     - "./wp-init.sh:/usr/local/bin/apache2-custom.sh"
    depends_on:
      db:
        condition: service_started
    ports:
    - 80:80
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_PASSWORD: wordpress
    command: 
    - apache2-custom.sh

wp-init.sh is where you write the code to execute.

Note the command yml tag:

command: 
    - apache2-custom.sh

because we bounded the two in the volumes tag, it will actually run the code in wp-init.sh within your container.

Upvotes: 0

larsks
larsks

Reputation: 311526

How can I keep this container running after the configuration has been done? Is it possible to do so without a busy loop running?

Among your many options:

  • Use something like sleep inf, which is not a busy loop and does not consume CPU time.
  • You could use a process supervisor like supervisord to start your service and start the configuration script.
  • You could run your configuration commands in a separate container after the service container has started.

Upvotes: 1

Related Questions