Useful commands for docker (Windows)

Sometimes, novice users might need a few useful commands to deal with docker images.

Build Dockerfile

Navigate terminal to the folder where is Dockerfile and execute build:

docker build -t myImageName .

(additionally, you might add -f Dockerfile after myImageName)

How to open Terminal (bash)

If you use Windows and you will need to enter bash (to install other extensions or do whatever you want in terminal), after running the container, you can enter there from cmd, using command:

Windows:
FOR /F "tokens=*" %g IN ('docker ps --filter "ancestor=YOUR_IMAGE_NAME_HEREEEE" -q') do (docker exec -ti %g bash)

Linux:
docker exec -ti $(docker ps -aqf "ancestor=YOUR_IMAGE_NAME_HEREEEE") bash 

Mounting custom folder

If you want to run it on custom port or want to mount your local folder as www-root (or even a separate plugin cases) for wordpress, then you can run the new container by:

docker run -d -p 8080:80 -v "D:\\your-folder\\":/var/www/html/ -it wp-php8

Connecting to local Mysql server

If you have a case, that you want to connect the docker WP to local mysql server (i.e. to xamp, wampserver or alike…) then enter mysql console and create & grant privileges to new user:

CREATE USER 'myuser1'@'*' IDENTIFIED BY 'myuser1';
GRANT ALL PRIVILEGES ON *.* TO 'myuser1'@'*';

and in docker WP installation, you should put database host as 192.168.1.xxx (correct that with your network ip. Note, you might need to append the port too, i.e. :3306 ).

 

Commit changes from container into image

Typically, when you restart docker, you will need to re-run the container from image, so all changes made into image will be lost. To avoid that, you can commit changes (made in running container) into the base-image:

docker commit containerID imageName
(to find ID, type docker ps )

Export (Backup) full image

docker save myImageName -o savedFileName.tar
docker load -i savedFileName.tar

OR

docker export your_containerid -o savedFileName.tar
docker import savedFileName.tar newimagename:tagname
(check difference: https://stackoverflow.com/questions/22655867/ )

1 thought on “Useful commands for docker (Windows)”

  1. Pingback: Docker & PHP-8 (zts) & Apache & Swoole+Parallel – Puvox – Blog

Leave a Comment

Your email address will not be published. Required fields are marked *

WordPress › Error

There has been a critical error on this website.

Learn more about troubleshooting WordPress.