Here we go… my second docker related post. I am quite in love with it, so expect some more stuff about it 🙂 I will put down some of the Docker Cheat Sheet command, that I use on the daily base when working with Docker. If you think, I should include some of yours Docker Cheat Sheet, don’t hesitate and paste them into the comment area below.
To retrieve the list of currently running containers, we can use:
docker ps -a
Result will be something like that
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1aedf69cdb86 mageinferno/magento2-nginx:1.11-1 "/usr/local/bin/st..." 5 hours ago Exited (255) About an hour ago 443/tcp, 0.0.0.0:8000->80/tcp mageattachment_app_1 7829a01b3650 mageinferno/magento2-php:7.0-fpm-1 "docker-php-entryp..." 5 hours ago Exited (255) About an hour ago 9000/tcp mageattachment_phpfpm_1 04bf83cd6795 percona:5.7 "docker-entrypoint..." 5 hours ago Exited (255) About an hour ago 0.0.0.0:8001->3306/tcp mageattachment_db_1
Ok, we have a list of running containers. When switching from project to project I usually stop and remove the running containers to start clean. Here is how I do it:
//stopping all the running containers docker stop $(docker ps -a -q) //removeing all containers docker rm $(docker ps -a -q)
What about images… sometimes we need to delete those to. First we can retrieve the list of available images with:
docker images -a
Example of the result:
REPOSITORY TAG IMAGE ID CREATED SIZE percona 5.7 742e34741b00 11 days ago 394MB wordpress latest d3f0cddf9493 3 weeks ago 408MB mysql 5.7 3e3878acd190 3 weeks ago 412MB phpmyadmin/phpmyadmin latest 3405a88394e3 2 months ago 111MB mageinferno/magento2-php 7.0-fpm-1 2444719145be 9 months ago 590MB mageinferno/magento2-nginx 1.11-1 593fd7e2ce1e 9 months ago 182MB
To delete an image we need to specify it’s ID. For example (deleting image mageinferno/magento2-nginx):
docker rmi 593fd7e2ce1e
If you have modified files like dockerfile, .env, you should re-build the image, for changes to take effect:
docker-compose build --no-cache
Let’s say we have the following volumens
f8917ce784af6e44ca528d80e4df2e9c16420992de90d8a125183a0147e93c92 fad6b268c50178373ab38d5386cf37f58f56e00a35c37da29e2b697d37525182 magetest_appdata magetest_dbdata
Those can be displayed with the following command:
docker volume ls -q
If we would like to delete volumes that are related to the image magetest the we can do this with:
docker volume rm $(docker volume ls -q | egrep 'magetest')
Or if we want to delete those volumes that are not related to the image magetest (just apply the regexp that suits your needs):
docker volume rm $(docker volume ls -q | egrep -v 'magetest')
You can easily connect into the running container (let’s say you kind of can SSH into it). First retrieve the container ID:
docker ps -a
After that enter the following command:
docker exec -i -t [container ID] bash
I often use the following shortcut
docker exec -it $(docker ps -f name=php -q) bash
This will connect you to the container with the image name that contains the strinh ‘php’.
So, here we go… you are now logged into the container shell. At this point, you may ask: “How can I transfer files from container into the local machine?”. Ok, for that we don’t need to be logged into the running container. Just use thi command:
docker cp [container ID]:/var/www/html.tar.gz ./html.tar.gz
That’s all for now. The cheat sheet will be updated as I will dive deeper in Docker…. so, stay tuned 😉
Want to apply those learned Docker Cheat Sheet on your docker docker develoment environment containers? Here are some of my post to setup a Docker development environment