I’m experimenting with docker at the moment and built this simple wordpress setup in about 5 minutes.
It consists of:
- a mariadb container
- a phpmyadmin container (used for testing the mariabd container and not really required)
- the wordpress container itself
mysql container:
docker run -d --name mariadb-test -e MYSQL_ROOT_PASSWORD=geheim --network test-net -v /home/nkalle/varlibmysql/:/var/lib/mysql -p 13306:3306 mariadb
The above command will spawn a mariabd container from the official image (if you are interested in building your own image, check this out) with a root password (change it), in a network called test-net, with persistent storage on the local filesystem mapped to /var/lib/mysql (so we won’t lose all our data when the conatiner is stopped) and a portbinding of 13306 on localhost (mapping 3306, the standard port for mysql).
phpmyadmin container: docker run -d --name pma -p 8080:80 --network test-net -e PMA_HOST=mariadb-test phpmyadmin/phpmyadmin
Next we’ll spawn a phpmyadmin container from the official image, in the same virtual net and again with persistent storage. Also we define the mysql host phpmyadmin shall bind to.
Try it with: http://localhost:8080, after the next step you will see the wordpress database structure here.
wordpress container: docker run -d --name "wp-test" --network test-net -v /home/nkalle/wp-html:/var/www/html -p 8081:80 -e WORDPRESS_DB_PASSWORD=geheim -e WORDPRESS_DB_HOST=mariadb-test wordpress
Finally we’ll spawn our wordpress container, referencing our database host and password.
Log in on http://localhost:8081 and complete the wordpress installation process with a few simple clicks (asking you to choose a language, and a password and you’re done).
How to stop/start the containers
When you are done playing around with your new conatiners, you may want to stop them and maybe re-use them again later.
docker stop wp-test
wp-test
docker stop pma
pma
docker stop mariadb-test
mariadb-test
docker start mariadb-test
mariadb-test
docker start pma
pma
docker start wp-test
wp-test
stop the database last and start it first, so you don’t lose any data.
A few commands to remember
When you are working with docker (or some of the above didn’t went as expected), one will find the following commands to be useful:
docker ps
shows started containers (ps -a also shows a history of container already closed, exited, etc.)
docker image ls
shows all docker images on your host
docker inspect <imagename/id>
shows data about the selected container (including status, network information, etc.)
docker logs <imagename/id>
show the logs for the selected container
docker exec <options> <command>
executes command in a running docker instance, e.g.:
docker exec -i -t mariadb-test bash
this starts a bash in our mariadb-test instance and opens an input stream (-i) that acts like a terminal (-t)
This post was largely inspired by reading “Docker. Das Praxisbuch für Entwickler und Dev-Ops Teams”
https://www.rheinwerk-verlag.de/docker_4599/