A Docker széles körben elfogadott, és nagyszerű eszköz egy alkalmazás felhőbe (vagy más Docker-kész infrastruktúrára) történő telepítéséhez. Hasznos a helyi fejlődés szempontjából is. Gyorsan elindíthatja a komplex alkalmazásokat, elszigetelve fejlesztheti, és így is nagyon jó teljesítményt nyújt.
Íme a legfontosabb parancsok a Docker hatékony használatához a mindennapi üzleti tevékenységben.
Sorolja fel az összes Docker-képet
docker images

Esetemben 3 képet telepítettem:
- MySQL, 8.0.19 verzió, egy címkével a legújabb verzió
- és Cassandra a legújabb verzióval.
Ha többet szeretne megtudni a képről, akkor megnézheti:
docker inspect mysql:latest

Ez visszaadja az információk listáját. Alternatív megoldásként a képazonosítót is felhasználhatja az információk megszerzéséhez:
docker inspect 3a5e53f63281
A kimenet elsöprő lehet. Ezért van egy praktikus lehetőség bizonyos információk szűrésére:
docker inspect --format='{{.RepoTags}} {{.Config.Image}}' 3a5e53f63281

Távolítsa el a Docker Images alkalmazást
Egyetlen kép eltávolítható:
docker rm mysql:latest

Esetemben a kép még mindig mysql-lel van ellátva: 8.0.19 . Ezért a teljes eltávolításhoz el kell távolítanom egy másik verziócímkét is:
docker rm mysql:8.0.19

A kép közvetlen eltávolításához könnyebb törölni a képet képazonosító szerint:
docker image rm 3a5e53f63281 -f

Az -f opció kényszeríti a végrehajtást, mert különben hibaüzenetet kapna, ha a képre több címke hivatkozik.
Indítsa el a Docker Image alkalmazást
Egy képet az előtérben lehet elindítani:
docker run cassandra
Ha a kép nem létezik, akkor letöltésre kerül. A végrehajtást a CTRL + C billentyűkombinációval állíthatja le . A háttérben futtathatja a -d opció hozzáadásával is :
docker run -d mysql

Ha a tároló a háttérben indul, akkor megkapja a tárolóazonosítót.
Alapértelmezés szerint a tároló elszigetelten fut. Ezért nem lesz képes kommunikálni vele, és az aktuális könyvtárban nincsenek fájlok.
Egy konténer portjainak továbbítása
Portokat továbbíthat a -p beállítás használatával, például egy olyan oldalra, amely a tárolóból van kitéve:
docker run -p 8080:80 nginx
Ez az NGINX-tároló tárol egy webszervert a 80-as porton. A -p 8080: 80 használatával a 8080 helyi port továbbításra kerül a 80 konténer portra.
Jelentkezzen be egy konténerbe
Néha hasznos bejelentkezni egy konténerbe. Ez csak akkor lehetséges, ha a konténerbe van telepítve egy shell. Ha nem erről van szó, hibaüzenetet kap.
Először indítsa el a tartályt leválasztva, és adjon nevet:
docker run -d --name my_container nginx
Ez visszaadja a tárolóazonosítót. Most végrehajthat egy héjat a tárolóban, és az -i és -t opciókkal csatolhatja hozzá a bemenetet és a kimenetet :
docker exec -it my_container bash
A tároló neve helyett a visszaküldött tároló azonosítóját is használhatja az összes következő művelethez. Néha a bash nem érhető el. Ezért megpróbálhat elindítani egy alaphéjat is:
docker exec -it my_container sh
Sorolja fel a futó tárolókat
A tároló elindítása után láthatja az összes futó tároló végrehajtását:
docker ps

Az -a hozzáadásával a kilépett tárolók is fel lesznek sorolva:
docker ps -a

Helyi mappa megosztása egy tárolóval
Sometimes it is useful to sync files between the container and the local filesystem. You can do it by running a container and using the -v option. On Linux and macOS, you can share a local temporary folder with a container by:
docker run --name=my_container -d -v $(pwd)/tmp:/var/log/nginx -p 8080:80 nginx
On windows you can run:
docker run --name=my_container -d -v %cd%/tmp:/var/log/nginx -p 8080:80 nginx
Stop running containers
It is possible to stop a running container by:
docker stop my_container
Stopping a container stops all processes but keeps changes within the filesystem.
Start a stopped container
A stopped container can be started by:
docker start my_container
Remove a container
To remove a stopped container, you can execute:
docker rm my_container
To stop and remove the container in one command, you can add the force option -f.
docker rm -f my_container
Create a volume and share it with multiple containers
An independent volume named SharedData can be created by:
docker volume create --name SharedData docker run --name=my_container -d -v SharedData:/var/log/nginx -p 8080:80 nginx docker run --name=my_container_2 -d -v SharedData:/var/log/nginx -p 8080:80 nginx
Both containers will have a shared folder, and files will be synced between both containers.
Remove a volume
To remove a volume, all containers that use the volume need to be removed.
docker rm -f my_container docker rm -f my_container_2 docker volume rm SharedData
Remove stopped containers and unused images
A safe tidy-up command is:
docker system prune -a
Remove all unused volumes
All unmounted volumes can be removed by:
docker volume prune
Conclusion
Creating containers, logging into containers, forwarding ports, and sharing volumes are the most important commands of your Docker command line interface. They build the foundation of systems like Kubernetes and enable us to create and run applications in isolation.
I hope you enjoyed the article. If you like it and feel the need for a round of applause, follow me on Twitter.
I am a co-founder of our revolutionary journey platform called Explore The World. We are a young startup located in Dresden, Germany and will target the German market first. Reach out to me if you have feedback and questions about any topic.
Happy Docker exploring :)
References
- Docker command line documentation
//docs.docker.com/engine/reference/commandline/docker/