Docker Basic Commands
Docker Basic Commands
Docker Networking:
● docker container port {container} —> to get the port number on which we are
mapping the traffic
● docker container inspect --format '{{ .NetworkSettings.IPAddress }}' {conainerId}
—> to get IP address of a container
● docker network ls - to get the list of N/W’s
● docker network inspect {networkName} —> we can see images in this command
output id they r mapped
● docker network create {networkName} —> to create a network, which will create
with default driver bridge, we can use —driver to specify the driver, otherwise
default option it will take(bridge)
● docker container run -p 81:80 --name nginx2 --network {networkName} -d nginx
(to create with specific network}
● docker network connect {netwrokId} {containerId}, use disconnect in place of
connect to disconnect from a network
Docker networkd:DNS
● docker exec -it {containerName} ping {anotherContainer}— using this we can
ping another a container from other container(prerequistie: they both should be in
same network) using built in DNS network
● docker container run --network-alias search --net test -d elasticsearch:2 —> to
create a container with DNS alias(—network-alias) and with network test
● docker container run --rm --net test alpine nslookup search —> it will search for
nslookup DNS entry “search” inside that n/w
● If we run “docker container run --rm --net test centos curl -s search:9200” it will
give different different outputs between two DNS servers
Docker Images:
● docker image history {image_name} —> will list the changes in a image..not
changes in container, whatver we do a change on a image it will create a new
layer
● docker image inspect nginx —> returns the metadata of that image
● docker image tag mongo (new name ex:deekshith:mango} —> to tag an existing
image with different name
● cat ~/.docker/config.json —this file will have all the credentials(In Mac it will store
in keychain)
● docker image push {imageName} —> to push to docker hub after login using
“docker login” command
DockerFIle:
● docker image build -t customnginx .
Docker Volumes:
● docker volume prune —> to cleanup unused volumes
● docker image inspect mysql
● docker volume ls —> will list the volumes
● docker volume inspect {volumeName} —> will give info about that volume
● docker container run -d --name mysql -e
MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql
mysql(named volume)
● docker volume create —> required to do this before “docker run” to use custom
drivers and labels
Bind Mount:
● docker container run -d --name nginx -p 80:80 -v $(pwd):/usr/share/nginx/html
nginx —> mounting a directory
Docker Compose:
● docker-compose up —> to start (use docker-compose -d to start the service in
background)
● docker-compose down —> to stop
● docker-compose logs —> to see the logs
● docker-compose top -> to see the list of processes
● docker-compose down -v —> to remove named volumes
● docker-compose down —rmi —> Iyt will remove the image at the time of
removing compose
Docker Swarm:
● “docker info” command output, we can see whether Swarm is acive or not. By
default it will be inactive.
● docker swarm init —> to initilize docker swarm
● docker node ls —> it will give the node(manager) which got created
● docker service —help(service in swarm replaces docker run)
● docker service create alpine(image) ping 8.8.8.8 —> to create a service with
alpine image and we are using ping in that one to just check the service
● docker service ls —> to list the services
● docker service ps {service_name} —> to get more o/p
● docker service update {serviceName} --replicas 3 —> to run 3 replicas
● docker service update --help(it will have more options compared to docker
container update --help)..using this we can do more things like scaling containers
and changing conf etc.
● docker service rm {serviceName}—> It will also deletes the containers which r
started through this service
● add “—driver overlay” at the end of “docker network create” to create a swarm
wired bridge network where containers across hosts on same virtual networkwill
communicate each other
● docker service create --name postgres --network mydrupal -e
POSTGRES_PASSWORD=mypass postgres(to create a service with custom
● docker service create --name drupal -p 80:80 --network mydrupal drupal, After
running these commands you can access the application in all the nodes
swarm-app-1 assignment:
● docker service create --name vote --replicas 2 -p 80:80 --network frontend
Docker stack:
● docker stack deploy -c example-voting-app-stack.yml voteapp where
“example-voting-app-stack.yml” is yaml file and voteapp is name of the stack,
use the same command if you make any changes to the yml file, it will take new
ones
● check docker stack —help for more options
Swarm Secrets:
● docker secret create psql_user psql_user.txt —> to create a new secret
“psql_user”, you should have a file with name psql_user.txt already
● echo "myDBpAssWORD" | docker secret create psql_pass - -> second way, here
we are passing via CLI using “-“ at last
● docker secret ls , docker secret inspect
● docker service create --name psql --secret psql_user --secret psql_pass -e
POSTGRES_PASSWORD_FILE=/run/secrets/psql_pass -e
POSTGRES_USER_FILE=/run/secrets/psql_user postgres, It will map the values
of the secret files into the env variables. Now we can see the secrets data inside
the container in “/run/secrets/“ directory. Refer the documentation of this postgres
official image for env variables
● docker service update --secret-rm. —> to remove the secret
● docker stack deploy -c docker-compose.yml mydb —> for stack
Docker healthcheck:
● docker container run --name p2 -d --health-cmd="pg_isreay -U postgres || exit 1"
postgres:9.6 where “pg_isreay -U postgres || exit 1” is the healthcheck command
for postgres and now we can see healthcech in status in docker container ls
comand, it will be in default starting stage for 30 sec. We can see this
healthcheck in docker inspect command
● docker service create --name postgres2 --health-cmd="pg_isready -U postgres ||
exit 0" postgres , it will be in starting stage for 30 sec, then only it will go to
running state
Docker System:
● docker system df —> will tell you the space used by images,container,local
volumes
● docker system prune —> will remove all the things except the running container
and its volumes.
● docker image prune, docker container prune …we can use this also
● docker system prune -a —>Remove all unused images not just dangling ones
● “export DOCKER_STACK_ORCHESTRATOR='swarm' “to switch orchestrator
from Kubernetees to swarm mode