In this blog, we’ll look at how Docker –Link Container. By binding the containers, Docker containers provide a secure channel for communicating with each other.
Running a WordPress blog using two linked containers
I want to run a containerized WordPress site, but I don’t want to run a MySQL database in the same container as WordPress. They want to keep the concept of separation then take into account the various components of your application and isolate them as much as possible.
Note: must be installed docker in your machine.
Start two containers. One runs WordPress on the official Docker Hub image, One is running a MySQL database. The two containers are linked with the optional –link option for the Docker CLI. Start by getting the latest images of WordPress and MySQL.
$ docker pull wordpress
$ docker pull mysql
$ docker images
Start the MySQL container, name it using the -name CLI option,
MYSQL_ROOT_PASSWORD via environment variable:
//$ docker run --name mysqlwp -d mysql it won’t run success because need to be set
password
//$ docker run --name mysqlwp -e MYSQL_ROOT_PASSWORD=wordpressdocker -d mysql
$docker run -d --name mysqlwp -e MYSQL_ROOT_PASSWORD=wordpressdocker mysql mysqld
--default-authentication-plugin=mysql_native_password
Now you can run your WordPress container based on the WordPress: latest image. Will be linked Add to the MySQL container with the –link option. This means that Docker will be set automatically. Upgrade your network to get the ports provided by the MySQL container are reachable inside the WordPress container:
$ docker run --name wordpress --link mysqlwp:mysql -p 80:80 -d wordpress
Both containers need to run in the background using port 80 of the WordPress container Map to host port 80:
$ docker ps
Open your browser and access http: //<IPaddress> to see the WordPress installer screen. In the language selection window. After setting up WordPress, it will be as follows. A fully functional WordPress site that runs in two linked containers.
If your WordPress container isn’t running and you see the error in your WordPress logs, do the following procedure:
MySQL connection error: (2054) Unknown authentication method requested by the server-client
Warning: mysqli :: __ Construct (): Unknown authentication method requested by the server. The default input code client [caching_sha2_password] on line 22
Warning: mysqli :: __ Construct (): (HY000 / 2054): Server requested authentication method unknown to the client with standard input code on line 22
First login SQL container using docker exec and
$ mysql -u root -p
You should be able to log in with ‘root’ or any other password.
Execute the following commands:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'wordpressdocker';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'wordpressdocker';
FLUSH PRIVILEGES;
Come out form the container ctrl+p+q
Open browser and use http://<serverip>
Two images, WordPress and MySQL, are WordPress and MySQL community. Each page of Docker Hub has the container configuration started with these images.
What’s interesting is that you can create a database and a user with the appropriate permissions.
Manipulate this database with some environment variables: MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD. In the previous example, WordPress is running as a root MySQL user and this is far from best practice. It’s better to create WordPress the database and its users look like this:
$ docker run --name mysqlwp -e MYSQL_ROOT_PASSWORD=wordpressdocker \
-e MYSQL_DATABASE=wordpress \
-e MYSQL_USER=wordpress \
-e MYSQL_PASSWORD=wordpresspwd \
-d mysql
Once the database container is running, run the WordPress container and specify the database table you defined:
$ docker run --name wordpress --link mysqlwp:mysql -p 80:80 \
-e WORDPRESS_DB_NAME=wordpress \
-e WORDPRESS_DB_USER=wordpress \
-e WORDPRESS_DB_PASSWORD=wordpresspwd \
-d wordpress
For more information on WordPress and MySQL Docker images and how to use them, see the links below.