Docker
Docker is a tool introduced to make it easier for you to create, deploy, and run applications using containers. Containers package your application with all important components (libraries, dependencies) and ship them as one package. You can run your application on any machine.
Docker Architecture
- Registry: Hosts public and official images.
- Images: Downloaded from the registry directly or implicitly when starting a container.
- Containers: Instances of images. Multiple containers for a single image are possible.
- Docker Daemon: Creates, runs, monitors containers, builds and stores images.
- Client: Talks to the daemon via HTTP.
Important Terms
- Layer: Read-only files to provision the system
- Image: A read-only layer that is the base of the machine
- Container: A runnable instance of the image
- Registry/Hub: Central place where images live
- Docker Machine: VM to run Docker containers
- Docker Compose: VM to run multiple containers as a system
List Docker CLI commands
docker
docker container --help
Display Docker version and info
docker --version
docker version
docker info
Execute Docker image
docker run hello-world
To create and run a container:
docker run --name container_name docker_image
Flags used:
-d
: Detached container or start-p
: Publish port-v
: Mount volumes--rm
: Remove container after exit--network
: Connect to network-e
: Set environment variables
List Docker images
docker image ls
List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls --all
docker container ls -aq
Remove containers
https://linuxhandbook.com/docker-stop-container/
docker stop container_ID_or_name
docker rm container_id_or_name
https://linuxhandbook.com/remove-docker-containers/
remove all containers, stop the running ones first and then remove them:
docker ps -q | xargs docker stop
docker ps -q | xargs docker rm
Useful for running my docker containers
docker image build -t insulter .
docker container run --name insulter -d -p 802:80 insulter2
docker image push $dockerId/insulter:v1.1
$source="D:\Files\databases".ToLower(); $target="c:\data" # Windows
source="$(pwd)/databases" && target='/data' # Linux
mkdir ./databases
docker container run --mount type=bind,source=$source,target=$target -d -p 8012:80 diamol/ch06-todo-list
curl http://localhost:8012
ls ./databases
docker volume create my-vol
docker container run --rm -it -v "d:\files:/data"
### Deck of things
docker image build -t deckofmanythings .
docker container run --name deckofmanythings -d -p 5000:5000 deckofmanythings
docker image push $dockerId/insulter:v1.1
Ship
To pull an image from the registry:
docker pull image:3.4
Retag a local image with a new name:
docker tag image:3.4 myrepo/myalpine:3.4
Log in to a registry:
docker login my-registry.com:8000
Push an image to a registry:
docker push myrepo/myalpine:3.4
Build
To build the image from the docker file and tag it:
docker build -t myapp:1.0 .
List all images that are locally stored:
docker images
Delete an image from the docker store:
docker rmi alpine:3.4
Docker Swarms
Orchestrate
Initialize swarm mode and listen to a specific interface:
docker swarm init --advertise-addr 10.1.0.2
Join an existing swarm as manager node:
docker swarm join --token <manager-token> 10.1.0.2:2377
Join a swarm as worker node:
docker swarm join --token <worker-token> 10.1.0.2:2377
List all nodes in the swarm:
docker node ls
Create a service from an image and deploy 3 instances:
docker service create --replicas 3 -p 80:80 --name web nginx
Scale a service:
docker service scale web=5
List tasks of a service:
docker service ps web
Services
List all services running in a swarm:
docker service ls
List tasks of a service:
docker service ps <service_name>
Service logs:
docker service logs stack_name.service_names
Scale services:
docker service scale stack_name.service_name=replicas
Clean Up
Clean up unused/dangling images:
docker image prune
Clean up images not used in containers:
docker image prune -a
Prune the entire system:
docker system prune
To leave a swarm:
docker swarm leave
Remove a service:
docker service rm <service_name>
Kill all running containers:
docker kill $(docker ps -q)
Stop all containers:
docker stop $(docker ps -q)
Remove all images:
docker rmi $(docker images -q)
Interaction Within a Container
Run a command in the container:
docker exec -it container_name command.sh
Follow the container log:
docker logs -f container_name
Save a running container as an image:
docker commit -m "commit message" -a "author" container_name username/image:tag
Source: IntelliPaat Docker Training Course