I am using docker to run a develop environment for several wordpress themes / plugins. However, I can't figure out how to enable my themes automatically on container creation. Normally I would just use wp-cli for this. I have created a custom image that extends the official wordpress image, but I can't figure out how to make the wp-cli commands run after the /var/www/html folder get's setup (I think this is created by the wordpress image entrypoint script). If I put the commands in a "RUN" command in the dockerfile, the commands just fail since the /var/www/html directory is empty. However, if I connect to the container once it is setup, the directory is populated, and wp-cli works just fine. How can I run a command after the entrypoint.sh from the parent image is run?
Here is the content of my dockerfile (which does not work):
FROM wordpress
MAINTAINER Me!
COPY docker-install/wp-cli.phar /usr/local/bin/wp
WORKDIR /var/www/html
RUN chmod u+x /usr/local/bin/wp
RUN /usr/local/bin/wp core install --title="Test WP Site" --admin_user=admin --admin_password=something --admin_email=my#cool.email --url=localhost:8080 --allow-root
RUN /usr/local/bin/wp theme activate mytheme --allow-root
The short answer: You can't. The ENTRYPOINT and CMD is are used at Docker container runtime, not build time. In this case, the Wordpress environment is actually started at container runtime so you can't interact with it during a build.
The long answer: You might be able to help accomplish your goals by using Docker Compose to create two services, one for Wordpress and another for the Wordpress CLI variant (see the cli tags at https://hub.docker.com/_/wordpress/) with a volumes_from the first one. Then, you could use docker-compose run cli-service wp-cli command to run your commands.
I created a prepare.sh file that contains commands I want to run before Apache starts, and then added this to the Dockerfile:
COPY ./prepare.sh /prepare.sh
RUN chmod +x /prepare.sh
CMD /prepare.sh && apache2-foreground
You should create a script that install themes after docker-entrypoint. The name has to start with apache2 (docker-entrypoint checks name of CMD param https://github.com/docker-library/wordpress/blob/0a5405cca8daf0338cf32dc7be26f4df5405cfb6/php5.6/apache/docker-entrypoint.sh#L26), for example apache2-setup-wordpress.sh.
Dockerfile would be like this
FROM wordpress:4.7.3
COPY apache2-setup-wordpress.sh /usr/local/bin
RUN chmod +x /usr/local/bin/apache2-setup-wordpress.sh
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-setup-wordpress.sh", "apache2-foreground"]
Related
Yesterday I created a docker container with
docker-compose up -d
(and docker-compose.yaml file). It created a wordpress site, a database, phpmyadmin, etc.)
I made some changes to the wordpress installation, content, etc. I then shut it down with:
docker-compose down -volumes
This morning I wanted to run this container again and run the docker-compose up -d command again and when I visited the url it showed a wordpress configuration wizard instead of the existing installation from yesterday. In hindsight, it makes sense. Not sure why I expected not to create a new container. I then deleted the install* file from wp-admin but it didn't help.
Are the changes from my yesterday's wp installation lost? Have I overwritten everything?
Generally, how can I re-start an existing container with docker/docker-compose
by using docker-compose down -volumes you deleting :
Stops containers and removes containers, networks, volumes, and images created by up
see this
you may use docker-compsoe start/stop instead to stop or start your running containers
The command
docker-compose down
will stop all your containers, delete all your containers and remove any networks defined in your docker compose file.
It does not remove your volumes, by the way (unless you additonally pass the -v flag to the command).
So your command
docker-compose down --volumes
will also remove any volumes.
If you want to persist your wordpress installation for development purpose but want to be able to remove and create containers during development you can mount volumes on your host machine. E.g. for your database data or also for your wordpress source code (if needed).
See also here: https://docs.docker.com/compose/wordpress/
Take a look at the docker compose file provided there and specifically take a look at the volume directives.
In the example the database files are mounted on your host machine so that they don't vanish if you remove the database container.
If you are already using volumes in your docker compose file than you can simply remove the --volumes flag from the docker-compose down command
You can recreate a service inside compose file with following command.
for example you have wordpress,mysql,nginx services inside compose file.
docker-compose -f docker-compose.yml up -f --build wordpress
this command recreate your container
I am deploying a Docker image (Wordpress) on Elastic Beanstalk using a single container deployment.
My deployment zip file includes:
public folder containing a complete wordpress build
Dockerfile
.ebextensions/permissions.config
The standard Wordpress image creates a volume VOLUME /var/www/html and in my Dockerfile I do
COPY ./public /var/www/html
Now the problem is that I cannot upload media using Wordpress admin dashboard.
Unable to create directory wp-content/uploads/2019/02. Is its parent directory writable by the server?
I've tried to change the permissions on the uploads folder using the EB config in .ebextensions/permissions.config
container_commands:
91writable_dirs:
command: |
chmod -R 777 /var/app/current/public/wp-content/uploads
cwd: "/var/app/current"
I see from the logs that the docker image gets built before running chmod. I've seen on other SO posts that some run the script on /var/app/ondeck/, but that fails with the directory doesn't exist
Despite all the above, my question is actually how do I get to upload media to Wordpress with my current setup.
EDIT: When I attach a shell to the docker container and change the file permissions of wp-content/uploads in the VOLUME /var/www/html I am able to upload media. So how can this be made permanent on the VOLUME?
Whenever wordpress docker image is built and run, the docker ENTRYPOINT of the wordpress image is executed first. Hence your command to change the directory permissions is not getting executed.
This ENTRYPOINT is a bash script located in /usr/local/bin/docker-entrypoint.sh
If you want your command to be executed, you could add your command to this script and it will be called every time your container starts.
You could do it the following way -
Start your container and copy the contents of the existing
docker-entrypoint.sh
Create a new docker-entrypoint.sh outside the container and edit
that script to add your chmod command at appropriate location.
In your Dockerfile add a line to copy this new entrypoint to the
location /usr/local/bin/docker-entrypoint.sh
I have a docker container with installed WP and I want to create a new custom theme for it, but I don't want to install wp on own machine. Can I develop a new theme only via Docker container?
UDP:
I solved my problem in this way:
1) in docker-composer.yml in a wordpress field need create a field
volumes:
- ./wordpress:/var/www/html
2) Create in your file system the directory wordpress and run docker-compose build and then docker-compose up. Files will be moved in your directory and you can work with them and see a result in localhost.
you can try to login into container (make sure it is running) using following command
#>docker exec -it <container-name-or-id> sh
After that you can edit and create themes inside this container.
I have some basic git knowledge but I'm not sure how to accomplish this.
I am trying to clone (?) github WordPress starter theme underscores. The idea is to create a basic framework based (with some modifications) on that repository. Then create other themes with that framework as the base.
So it should look something like:
copy github underscores repository to local
create a local repository my_framework from the underscores one, modifying certain parts of those files always (such as the name) and adding some other files
create new local repositories my_theme1, my_theme2 based on my_framework
The goal is to keep everything updated with any underscores update, while changing and modifying the framework and the themes. Once the content from github is pulled it should keep (or inform) of any updates, but I don't need any change I make locally to go back in the path.
I am not sure which path to follow, and would appreciate any help or pointer.
The goal is to keep everything updated with any underscores update, while changing and modifying the framework and the themes
That is called the triangular workflow:
fork (see "Fork a Repo") the repo automattic/_s
clone that fork locally,
git clone /url/my/fork myfork
add as remote upstream the original repo
cd myfork
git remote add upstream https://github.com/automattic/_s
From there, with git 2.9 or more, configure:
git config --global pull.rebase true
git config --global rebase.autoStash true
Finally, each time you want to update your branches (where you modify your own version of the original repo), do a
git checkout mybranch
git fetch upstream
git rebase upstream/master
Then you can merge that updated branch (after testing it) to your other repos my_theme1, my_theme2, cloned from myfork.
cd my_theme1
git fetch
git merge origin/mybranch
If you want to work locally only, you can skip the fork step and clone directly the original repo.
you should learn about child themes. the concept of it is having a main theme - which gets updated - and a child theme that'll you'll modify, add content, create different templates & styles... everything to your needs.
I'd recommend taking some minutes to read this throughtfully: https://codex.wordpress.org/Child_Themes
Assuming you using a terminal,
cd to the themes directory:
cd [PROJECT]/wp-content/themes
Now clone _s to your project:
git clone git#github.com:Automattic/_s.git [THENE-NAME]
After the clone ends you can start working with your new theme.
cd to theme directory:
cd [THENE-NAME]
and create another remote for your repo.
git remote add [NEW-RENOTE-NAME] [NEW-RENOTE-URL]
From now on, you can push change into your private remote:
git push [NEW-RENOTE-NAME] master
and if you want to get updates from _s repo you can just:
git pull origin master
Good Luck!
you could do something like
git clone https://github.com/Automattic/_s.git
create directory my_framework with mkdir my_framework(if on windows)
cd my_framework
git init
git remote add <_s> <PATH to your local underscore>
git pull(to get latest version of underscore)
again:
mkdir my_theme1
cd my_theme1
git init
git remote add <my_framework> <PATH to your local my_framework>
git pull
Hope this is what you are looking for!
What you want to do is called nested git repo. GitHub does not allow nested repositories. You can use GitSubmodule or subtree. It is done for when projects become bigger.
One copy of underscores will remain as "control".
Second copy of underscores will remain is starting of my_framework. Third copy is copied and modification of my_framework.
You can :
Update underscores repo aka WordPress starter theme underscore master separately
Change in your framework separately
Send pull request for wherever you want to contribute
my_theme1, my_theme2 are not versions but separate softwares. my_theme1 as example can have nth versions. Here are sample steps :
cd ~
mkdir parentrepo
cd parentrepo/
git init .
mkdir child1
mkdir child2
cd child1/
git init .
echo "FirstChildRepo content" > child1repofile.txt
git add .
git commit -a -m "Adding FirstChildRepo content"
cd ../child2/
echo "SecondChildRepo content" > child2file.txt
cd ..
echo "parentrepofile" > parentFile.txt
git add .
git commit -a -m "Adding Parent Repo content"
# verify whether working independently
cd ~/parentrepo/
git log
cd ~/parentrepo/Child1Repo/
git log
# try cloning parent, verify the contents
cd ~
git clone parentrepo/
cd parentrepo/
ls -a
./ ../ .git/ child1/ child2/ parentfile.txt
cd child1/
ls -a
./ ../
Work after this step to clone, update in the way whatever like others written.
You can "auto update" too. Add files named post-checkout & post-merge to .git/hooks directory of the needed repositories and add this into each of them:
#!/bin/sh
git submodule update --init --recursive
I have the following custom script step during my build:
mkdir -p "${CONTENTS_FOLDER_PATH}/Frameworks"
cp "${SRCROOT}/testing.1.dylib" "${CONTENTS_FOLDER_PATH}/Frameworks"
The script runs successfully, but when I check the bundle the Frameworks directory does not exist.
Should not not work as I expect? (Frameworks folder created with the testing.1.dylib in it).
Edit: Added screenshot of the runscript.
How about trying the following:
dst=${CONFIGURATION_BUILD_DIR}/${CONTENTS_FOLDER_PATH}/Frameworks
mkdir -p "${dst}"
cp "..." "$dst"
(I found your example and adapted it as above to copy a dylib into the 'Frameworks' folder of my framework).