How to copy local wp-content files to Wordpress container using Dockerfile

Viewed 1067

I have a Wordpress container that I would like to copy local folders (with files) to at startup

I have local files in folders:

/html/wp-content/plugins
/html/wp-content/themes
/html/wp-content/uploads

I have a Dockerfile with:

FROM wordpress

COPY ./html/wp-content/plugins/ /var/www/html/wp-content/plugins
COPY ./html/wp-content/themes/ /var/www/html/wp-content/themes
COPY ./html/wp-content/uploads/ /var/www/html/wp-content/uploads

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-foreground"]

Although the COPY is succeeding (the build passes), the files are not showing up in these destination folders.

If I change the destination folders to be on any path before /html, eg if I put the destination to be /var or /var/www, then the files copy and I can see them in the container.

I checked out this old post here, which mentions that the /html folder is actually mounted as a volume at startup, and so I need to copy the files into this folder first

/usr/src/wordpress/wp-content

and then at startup these folders will be automatically copied across to /var/www/html/wp-content/. (This would explain why copying directly does not seem to work)

I tried that too, and while my local folders are indeed copied into these folders (I can see them in the container), they are then not copied across to /var/www/html/content at startup!

  1. Is it possible to copy the local files directly into the /var/www/html folders via the dockerfile?
  2. If not, how can I ensure that if copying to /usr/src/wordpress/wp-content, the folders will be copied across to /var/www/html/wp-content/ at startup?

(Some posts I've looked through that don't work, as this seems to be particular to Wordpress, and not Dockerfile COPY on its own:)

Dockerfile copy keep subdirectory structure Docker is not copying subdirectory into Container How to copy folders to docker image from Dockerfile? https://www.serverlab.ca/tutorials/containers/docker/how-to-host-your-wordpress-site-with-docker/ Wordpress docker copy theme into exposed folder

1 Answers

Today I ran into the same problem. I wanted to add theme when building the image by using COPY. However, it did not work. This was because I already set my wp-content folder as a volume. It seems you cannot COPY into a volume.

The discussion in the following link helped me realise this:

https://github.com/docker-library/wordpress/issues/146

Below I have added my WordPres Dockerfile and docker-compose file. After commenting out the volume everything worked as expected.

I sure hope you do not need it anymore after two years, haha.

Reference: https://docs.docker.com/samples/wordpress/

Dockerfile

FROM wordpress:latest

WORKDIR /var/www/html

COPY ./wp-content/ ./wp-content/
RUN chmod -R 755 ./wp-content/


docker-compose.yml

version: "3"
services:
  db:
    build:
      context: .
      dockerfile: ./compose/local/db/Dockerfile
    image: new_db_image_name
    container_name: new_db_image_name
    command: '--default-authentication-plugin=mysql_native_password'
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=somewordpress
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=wordpress
    expose:
      - 3306
      - 33060
  wordpress:
    build:
      context: .
      dockerfile: ./compose/local/wordpress/Dockerfile
    image: new_wordpress_image_name
    container_name: new_wordpress_image_name
#    volumes:
#      - wp-content:/var/www/html/wp-content
    ports:
      - 80:80
    restart: always
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=wordpress
      - WORDPRESS_DB_NAME=wordpress

volumes:
  db_data:
#  wp-content:



Related