Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker-compose.yml and custom script after entrypoint (before command) #205

Closed
Ciantic opened this issue Feb 8, 2017 · 12 comments
Closed

Comments

@Ciantic
Copy link

Ciantic commented Feb 8, 2017

I have fairly simple need, and have been struggling with this many hours now. I want to run some script after entrypoint, and before command. I've tried running it as a command but no luck.

docker-compose.yml

---
version: '2'
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/wp-init.sh"
    depends_on:
    - db
    ports:
    - 80:80
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_PASSWORD: wordpress
    entrypoint: docker-entrypoint.sh
    command: /usr/local/bin/wp-init.sh
volumes:
  db_data: 

wp-init.sh

# I would do my thing here, but this ain't running the wp 

# execute CMD
exec "apache2-foreground" # this seems to be running just fine

If I start this, I only get forbidden from browser. It starts up normally if I comment entrypoint and command though.

I've also tried creating a dockerfile, like in the issue #130, but it didn't work with docker compose either.

@Ciantic
Copy link
Author

Ciantic commented Feb 8, 2017

Finally! Found it. Looks like it works like this:

(Notice that your command must be named "apache2-something", with apache2 in front, otherwise the entrypoint in this docker image does not do what it's supposed to do.

docker-compose.yml

---
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
volumes:
  db_data: 

then

wp-init.sh

echo "Doing my thing! E.g. install wp cli, install wordpress, etc..."

# execute apache
exec "apache2-foreground"

@Ciantic Ciantic closed this as completed Feb 8, 2017
@rsegecin
Copy link

Why the command file should be named with prefix "apache2"? I'm trying to do the same thing with toke/mosquitto do you know the prefix that I should use? Do you know if I could be able to run a command line? Thank you.

@venantvr
Copy link

Thanks, you saved my day !

@hustshawn
Copy link

Same question as @rsegecin

@allenyllee
Copy link

@rsegecin @hustshawn I think it is because this line
The option "command" in docker-compose.yml just pass "apache2-custom.sh" to docker-entrypoint.sh as an argument.

@Ciantic
Copy link
Author

Ciantic commented Oct 15, 2017

I suppose this image is not meant to be used like this (even though it works as I mentioned) instead one should create own Docker image that 'inherits' from the WordPress image with own init stuff.

With docker-compose.yml the image is always recreated, but for simple use cases there should be a more obvious and documented way to add a script before all that init.

@chusiang
Copy link

Thanks for @Ciantic , it's working for me. 💯

@benaich
Copy link

benaich commented Aug 22, 2019

I prefer using a dokerfile this way I avoid creating the image each time I run docker-compose

dockerfile

FROM wordpress

# copy files
COPY --chown=www-data:www-data ./src/ /usr/src/wordpress/wp-content/
COPY docker-entrypoint.sh /usr/local/bin/dockerInit

RUN chmod +x /usr/local/bin/dockerInit
RUN dockerInit

docker-entrypoint.sh

#!/usr/bin/env bash
echo "install plugin and setup wordpress ..."
chown -R www-data:www-data /usr/src/wordpress

@triusis92
Copy link

I prefer using a dokerfile this way I avoid creating the image each time I run docker-compose

dockerfile

FROM wordpress

# copy files
COPY --chown=www-data:www-data ./src/ /usr/src/wordpress/wp-content/
COPY docker-entrypoint.sh /usr/local/bin/dockerInit

RUN chmod +x /usr/local/bin/dockerInit
RUN dockerInit

docker-entrypoint.sh

#!/usr/bin/env bash
echo "install plugin and setup wordpress ..."
chown -R www-data:www-data /usr/src/wordpress

Hi @benaich
I'm trying to use your suggested method during docker build, specifically changing the ownership of /var/www/html directory and all the files and subdirectories under it. However I am also mounting some files onto wp-content and the script doesn't seem to change the ownership during the build process so I have to docker exec into the container and execute chown -R www-data:www-data /usr/src/html manually which then solves my permission problems inside wordpress. Was just wondering if you encountered a similar problem and or can suggest a solution to mine ?

@yosifkit
Copy link
Member

yosifkit commented Jun 8, 2020

@triusis92 see #474 (comment) (and #74 + #249) for why mounted files are not automatically chowned. You need to either run the container as the owner of the files with --user (or APACHE_RUN_USER as approprate for your setup) or chown/chmod the files so that the default user has access (www-data).

@triusis92
Copy link

triusis92 commented Jun 9, 2020

@triusis92 see #474 (comment) (and #74 + #249) for why mounted files are not automatically chowned. You need to either run the container as the owner of the files with --user (or APACHE_RUN_USER as approprate for your setup) or chown/chmod the files so that the default user has access (www-data).

Hi @yosifkit
Thanks for the reply. Where in the build process should I chown/chmod the files so that the default user has access (www-data) . I'm using docker-compose if that matters
Just for context:
docker-compose.yaml

version: '3.7'
services:
  wp:
    build: ./wordpress/
    container_name: wp
    ports:
      - "80:80"
      - "443:443"
    environment:
      WORDPRESS_DB_HOST: db-wp
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_PASSWORD: pass
      WORDPRESS_DB_NAME: wp_db
      APACHE_RUN_USER: www-data
      APACHE_RUN_GROUP: www-data
    depends_on:
      - db-wp
    volumes:
      - ./wordpress/themes:/var/www/html/wp-content/themes
      - ./wordpress/plugins:/var/www/html/wp-content/plugins
      - ./wordpress/uploads:/var/www/html/wp-content/uploads

Dockerfile called during build

FROM wordpress

COPY --chown=www-data:www-data plugins/ /usr/src/wordpress/wp-content/plugins
COPY --chown=www-data:www-data themes/ /usr/src/wordpress/wp-content/themes
COPY --chown=www-data:www-data uploads/ /usr/src/wordpress/wp-content/uploads

COPY docker-entrypoint.sh /usr/local/bin/dockerInit

RUN chmod +x /usr/local/bin/dockerInit
RUN dockerInit

docker-entrypoint.sh

#!/usr/bin/env bash
echo "changing ownership ..."
chown -R www-data:www-data /usr/src/wordpress

The outcome of all this is that all the files and directories mounted inside the container have the correct permission "www-data:www-data" except for wp-content which is "root:root". Even all the content inside wp-content has "www-data:www-data" privileges

root@75d3e394b730:/var/www/html# ls -ltr
total 212
-rw-r--r--  1 www-data www-data  7278 Jan 10 14:05 readme.html
-rw-r--r--  1 www-data www-data  3133 Feb  6 06:33 xmlrpc.php
-rw-r--r--  1 www-data www-data  4755 Feb  6 06:33 wp-trackback.php
-rw-r--r--  1 www-data www-data 31111 Feb  6 06:33 wp-signup.php
-rw-r--r--  1 www-data www-data  3300 Feb  6 06:33 wp-load.php
-rw-r--r--  1 www-data www-data  2496 Feb  6 06:33 wp-links-opml.php
-rw-r--r--  1 www-data www-data  3940 Feb  6 06:33 wp-cron.php
-rw-r--r--  1 www-data www-data  2275 Feb  6 06:33 wp-comments-post.php
-rw-r--r--  1 www-data www-data   351 Feb  6 06:33 wp-blog-header.php
-rw-r--r--  1 www-data www-data  6912 Feb  6 06:33 wp-activate.php
-rw-r--r--  1 www-data www-data   405 Feb  6 06:33 index.php
-rw-r--r--  1 www-data www-data 47874 Feb 10 03:50 wp-login.php
-rw-r--r--  1 www-data www-data 19915 Feb 12 11:54 license.txt
-rw-r--r--  1 www-data www-data 19396 Apr 10 03:59 wp-settings.php
-rw-r--r--  1 www-data www-data  8509 Apr 14 11:34 wp-mail.php
drwxr-xr-x  9 www-data www-data  4096 Apr 29 18:58 wp-admin
drwxr-xr-x 21 www-data www-data 12288 Apr 29 18:58 wp-includes
drwxr-xr-x  5 root     root      4096 Jun  9 02:57 wp-content
-rw-r--r--  1 www-data www-data  2823 Jun  9 02:57 wp-config-sample.php
-rw-r--r--  1 www-data www-data  3200 Jun  9 02:57 wp-config.php

If I change the ownership of wp-content folder manually only to "www-data www-data" everything works perfectly. I'm not a proficient Docker user and I've tried adjusting the script, docker-compose and Dockerfile in many ways, the result is always the same, wp-content always ends up under "root:root" privileges

UPDATE:
#474 (comment) This solved my problem as I dont have to have the volumes mounted for my image

@roppa
Copy link

roppa commented Jun 13, 2024

Thanks for this. I was trying to load a .dump file into neo4j. This was the only way I could do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants