Docker containers managing cheat sheet
# Docker overview
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker's methodologies for shipping, testing, and deploying code, you can significantly reduce the delay between writing code and running it in production. Documentation.
# Basic most used commands:
Start container:
docker start <container_id>
Stop container:
docker stop <container_id>
Restart container:
docker restart <container_id>
Open bash shell in container:
docker exec -it <container_id> bash
Open database shell (psql) in containerized PostgreSQL:
docker exec -it <database_container_name> psql -U <database_user> -W <database_name>"database_container_name" - name of container, described in docker-compose.yaml file.
With «docker system prune» command you may remove unused objects (containers, networks, etc...):
user@localhost:~$ docker system pruneDocker daemon ask about confirmation. Here -a or --all option remove all unused images not just dangling ones, -f or --force option do not prompt for confirmation, --volumes prune anonymous volumes. Documentation.
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- unused build cache
Are you sure you want to continue? [y/N]
Build with specifying Dockerfile:
docker build -f my.Dockerfile .
# Networking
Browse all netwoks:
docker network ls
Get container's IP address:
docker inspect <container_id> | grep IPAddress
# Export and import containers
Save container in tar file for future transfer:
docker save <container_name> > image_name.tar
Import image from archive:
docker load < image_name.tar
Run imported container with custom name, host network and attached volume:
docker run -d --network=host -v /path/on/host/machine:/path/in/container <container_name>
Support me on Patreon
#cheat-sheet #docker #linux