skip to Main Content

I’m new to using Docker and docker-compose so apologies if I have some of the terminology wrong.

I’ve been provided with a Dockerfile and docker-compose.yml and have successfully got the images built and container up and running (by running docker-compose up -d), but I would like to update things to make my process a bit easier as occasionally I need to restart Apache on the container (WordPress) by accessing it using:

docker exec -it 89a145b5ea3e /bin/bash

Then typing:

service apache2 restart

My first problem is that there are two other services that I need to run for my project to work correctly and these don’t automatically restart when I run the above service apache2 restart command.

The two commands I need to run are:

service memcached start
service cron start

I would like to know how to always run these commands when apache2 is restart.

Secondly, I would like to configure my Dockerfile or docker-compose.yml (not sure where I’m supposed to be adding this) so that this behaviour is baked in to the container/image when it is built.

I’ve managed to install the services by adding them to my Dockerfile but can’t figure out how to get these services to run when the container is restart.

Below are the contents for relevant files:

Dockerfile:

FROM wordpress:5.1-php7.3-apache

RUN yes | apt-get update -y 
    && apt-get install -y vim 
    && apt-get install -y net-tools 
    && apt-get install -y memcached 
    && apt-get install -y cron

docker-compse.yml

version: "3.3"

services:
  db:
    image: mysql:5.7
    volumes:
      - ./db_data:/var/lib/mysql:consistent
    ports:
      - "3303:3306"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: vagrant
      MYSQL_DATABASE: wp_database
      MYSQL_USER: root
      MYSQL_PASSWORD: vagrant
  wordpress:
    container_name: my-site
    build: .
    depends_on:
      - db
    volumes:
      - ./my-site-wp:/var/www/html/:consistent
    ports:
      - "8001:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: vagrant
      WORDPRESS_DB_NAME: wp_database
volumes:
  db_data:
  my-site-wp:

2

Answers


  1. Per each container in the compose file, you can add a run command flag in the yaml which will run a command AFTER your container has started. This will run during every start up. On the other hand, commands in the Dockerfile will only run when the image is being built. Ex:

     db:
        image: mysql:5.7
        volumes:
          - ./db_data:/var/lib/mysql:consistent
        command: # bash command goes here
        ports:
          - "3303:3306"
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: vagrant
          MYSQL_DATABASE: wp_database
          MYSQL_USER: root
          MYSQL_PASSWORD: vagrant
    

    However, this is not what you are after. Why would you mess with a container from another container? The depends_on flag should restart the downstream services. It seems your memcache instance isn’t docked and hence, you are trying to fit it in the application level logic, which is the antithesis of Docker. This code should be in the infra level from the machine or the orchestrator (eg. Kubernetes).

    Login or Signup to reply.
  2. …occasionally I need to restart Apache on the container (WordPress)…

    Don’t do that. It’s a really, really bad habit. You’re treating the container like a server where you go in and fix things that break. Think of it like it’s a single application — if it breaks, restart the whole dang thing.

    docker-compose restart wordpress
    

    Or restart the whole stack, even.

    docker-compose restart
    

    Treat your containers like cattle not pets:

    Simply put, the “cattle not pets” mantra suggests that work shouldn’t grind to a halt when a piece of infrastructure breaks, nor should it take a full team of people (or one specialized owner) to nurse it back to health. Unlike a pet that requires love, attention and more money than you ever wanted to spend, your infrastructure should be made up of components you can treat like cattle – self-sufficient, easily replaced and manageable by the hundreds or thousands. Unlike VMs or physical servers that require special attention, containers can be spun up, replicated, destroyed and managed with much greater flexibility.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search