How to use Wordpress in Docker Container on your Windows Laptop.

I am using “Docker for Windows” software to run dockers on my Windows 10 laptop. You can get “Docker for Windows” by clicking on this link .
If you have Windows 7 download Docker Toolbox for Windows with Virtualbox.
Once you’ve installed Docker Toolbox, install a VM with Docker Machine using the VirtualBox provider:
docker-machine create --driver=virtualbox default
docker-machine lseval "$(docker-machine env default)"

How to create docker containers?

Once you have installed pre-requisite software create a docker-compose.yml file in a text editor.
If you are using windows simply copy/paste the below in a text file and rename the file to docker-compose.yml . Ensure that the extension of file is changed from .txt to .yml .

version: '3'

     image: mysql:5.7
       - db_data:/var/lib/mysql
     restart: always
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

       - db
     image: wordpress:latest
       - "8000:80"
     restart: always
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
    db_data: {}

After this just go to the directory which has your docker-compose.yml file and run below command in CMD prompt i.e. windows command line or docker shell prompt.
docker-compose up -d
Above command will run your containers in detached mode. So they won’t shutdown automatically, you can go inside and explore them.
Now run below command to see the list of running containers. Here it shows that we have 2 containers and they are up since 3 days.
docker container ls
45dfdfd78306 wordpress:latest "docker-entrypoint..." 3 days ago Up 3 days>80/tcp mywordpress_wordpress_1
c598nvcvc190 mysql:5.7 "docker-entrypoint..." 3 days ago Up 3 days 3306/tcp mywordpress_db_1
To check the list of networks created by docker run:-
docker network ls
784dgfgda0bf bridge bridge local
09811ere912d host host local
51dddfdfd38a mywordpress_default bridge local
bbbdatyura8c none null local
To check the details of a particular network, run:
docker network inspect mywordpress_default
It will show you IP, subnets and MAC address associated with your containers.
Now if you want to login to your Linux docker use below command. By this you will login to the docker and after this you can work normally as you do in a  normal Linux VM.
docker exec -it c598nvcvc190 bash
If you want to access the wordpress website from browser type localhost:8000 in browser. It will show your website. You can access it’s admin page by localhost:8000/wp-admin .
If you want to check connection to your mysql container from php container you can do so by creating a php file.
So create a file testmyconnection.php in the wordpress container in directory /var/www/html/ and paste below content in it. You will have to put your own user id and password in the file.
If you want to test with root user you can get the root user login credentials from docker-compose.yml file.
<?php$con = mysqli_connect("db","youruser","yourpassword","wordpress");if(! $con ) { die('Could not connect: ' . mysqli_error()); } echo 'Connected successfully'; mysqli_close($con);?>
Give the testmyconnection.php file execution rights using below comamnd. For testing purpose i am giving it full rights. You should restrict the rights as per your requirement.
chmod 777 testmyconnection.php
Now you can test the connection by opening the  below link in browser .
It will show you “Connected successfully” message if all is fine.
Hope this article helped you get started on dockers. Do let me know your opinions in comment section.

No comments:

Post a Comment