Is Docker installed?
Check if your installation is ok and spin up Docker as well:
docker -D info
Using Docker
Now it's time to download and install docker images. Using the official images of MariaDB and Drupal. If you don’t specify the absolute URL to the image it will be searched and downloaded from Docker Hub. Tag 'latest' will be used by default.
docker pull mariadb
Now it's Drupal turn. Again pull the latest version. If you specify the tag: 9.3.3 'drupal:9.3.3' it will download that version instead.
docker pull drupal:latest
Take a look at the Dockerfile of this image. You will see that it’s inherited from the official PHP docker image with Apache2. Then Apache’s rewrite module is enabled, additional libraries and PHP extensions are installed, opcache settings are updated and finally Drupal itself is downloaded.
If you want to see your images:
docker images
Run a container with MariaDB
By using key v (volume) we tell Docker to create a new mount from the host where our data will be stored. By default, the data will be stored inside of the container and this means it will be lost after rebuilding the container (you might need it to apply MariaDB updates).
In the output, we see the hash / identifier of the running container.
docker run -e MYSQL_ROOT_PASSWORD=admin -e MYSQL_DATABASE=drupal9 -e MYSQL_USER=drupal9 -e MYSQL_PASSWORD=drupal9 -v mariadb:/var/lib/mysql -d --name mariadb mariadb
Adjust the following variables from the above command:
- MySQL root password: admin
- Database name: drupal9
- MySQL user: drupal9
- MySQL password: drupal9
There you go! We are basically done - in one line you have everything set up. (If you don't have any images installed, docker will ask if you want to download it). Let's have a look at what we just created:
--name
mariadb creates a docker container named mariadb
-e
is a flag for environment variables. We added the MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER and MYSQL_PASSWORD for your mariadb
The last part mariadb is the docker image we will use (if you use another database you should change this)
Now you have a docker container named mariadb running with a MySQL database. The database has the name, username and password set to drupal9.
Run a container with Drupal
Using the link just created MariaDB and the binding to the 80-th port. The link is just a record in /etc/hosts specifying IP of the container with MariaDB in a virtual network created by Docker during its installation. However, remember, if you changed the name from the previous command, ensure you update it here too.
docker run --name drupal9 --link mariadb:mysql -p 80:80 -d drupal:latest
An additional command prompt
-p
is the flag for setting the port. 80:80 maps the internal docker port to the external port so we can have a door from the outside to the docker image.
Error bind: address already in use
When attempting the above command did you receive the following error?
docker: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:80: bind: address already in use.
You can try port 8080.
Error: port already allocated
docker: Error response from daemon: driver failed programming external connectivity on endpoint drupal9 ({HASH}): Bind for 0.0.0.0:8080 failed: port is already allocated.
You can try port 8083.
Success!
Want to have a look at your containers:
docker ps
Before I continue... a little more about Docker commands
if you want to run your database in the background as a daemon. Without the d flag, you need to have your container running in a separate terminal at all times to access your database. You can start and stop the container by running docker start {docker_name} or docker stop {docker_name}
-d
If you want to ensure you use the latest {name} image:
docker postgres:latest docker mariadb:latest docker drupal:latest
Pull the latest {name} image:
docker pull mariadb
docker pull postgres
docker pull drupal
List all images on your computer:
docker images
See all created containers (the ones having an assigned port is the running containers).
docker ps -a
Remove a docker container
docker rm {docker_ID} or docker rm <docker_name>
Wipe all database data from your docker container. Even though you have several docker containers running you will still access the same data as it is written to the same place
docker volume prune
Viewing your Drupal site locally
You need to access the container. To access via bash in a running container, enter the following:
docker exec -t -i {container_name} /bin/bash
Replace {container_name} with the container ID. Your working Drupal site should be in /Users/{your_name}/Sites/{your_directory}
To exit from a container, just type:
exit
Stop a Docker container
To stop a container running then you'll ned to use the name of the container
docker container stop drupal9 docker container stop mariadb
Delete Docker images
docker images
docker rmi -f {image_id}
Replace {image_id} with your own