Docker Client Basics
Docker Client Basics
Run a bash shell in temporary Ubuntu Container and install packages
docker pull ubuntu:latest
docker run -it --rm --entrypoint=/bin/bash ubuntu:latest
apt-get update
apt-get install curl -y
exit
Run a bash shell in already started container
docker run --name ubuntu-latest -it --rm --entrypoint=/bin/bash ubuntu:latest
docker exec -it ubuntu-latest /bin/bash
Passing environment variable from host system to docker container
Passing a value as environment variable
docker run -it --rm --env MY_VALUE=123456 --entrypoint=/bin/bash ubuntu:latest
Passing an environment variable which is already defined in the host system
docker run -it --rm --env MY_VALUE --entrypoint=/bin/bash ubuntu:latest
Attach a host directory to a docker container
Using absolute path
docker run -it --rm -v /host/machine/sourcecode:/docker_container/sourcecode_directory --entrypoint=/bin/bash ubuntu:latest
Using relative path from current directory
docker run -it --rm -v `pwd`/sourcecode:/docker_container/sourcecode_directory --entrypoint=/bin/bash ubuntu:latest
docker run -it --rm -v /host/machine/sourcecode:/docker_container/sourcecode_directory --entrypoint=/bin/bash ubuntu:latest
Using relative path from current directory
docker run -it --rm -v `pwd`/sourcecode:/docker_container/sourcecode_directory --entrypoint=/bin/bash ubuntu:latest
ls -al
ls -al /docker_container
ls -al /docker_container/sourcecode_directory
Change the default working directory to start at the mounted volume
docker run -it --rm -v `pwd`/sourcecode:/docker_container/sourcecode_directory -w /docker_container/sourcecode_directory --entrypoint=/bin/bash ubuntu:latest
pwd
Run postgres database
Pull postgres server
docker pull postgres:latest
Start postgres server
docker run -it --rm --name postgres_latest_local -e POSTGRES_DB=mypostgresdb -e POSTGRES_USER=mypostgresuser -e POSTGRES_PASSWORD=mypostgrespassword -p 5432:5432 postgres:latest
Connect to postgres server
docker exec -it postgres_latest_local psql -h localhost -U mypostgresuser mypostgresdb
Sample query
select * from pg_catalog.pg_tables;
Define, Start, Attach and follow logs of a container
Define a Container:
docker create --name orientdb-2.0.18 -p 2424:2424 -p 2480:2480 -e ORIENTDB_ROOT_PASSWORD=rootpwd orientdb:2.0.18
Start a Container:
docker start orientdb-2.0.18
Attach to a container:
docker exec -it orientdb-2.0.18 /bin/bash
Follow logs of a container
docker logs --follow orientdb-2.0.18
Stop, Remove a Container and Remove an Image
Stop a Container:
docker stop orientdb-2.0.18
Remove a Container:
docker rm orientdb-2.0.18
Remove an image:
docker rmi orientdb-2.0.18
Check Container Logs:
docker logs -t -f orientdb-2.0.18
List Container:
docker ps -a
List Images:
docker images
Inspect an image or running container
Inspect an image by tag:
docker inspect ubuntu:latest
Inspect an image by image id:
docker inspect <image-id>
Inspect a running container by id:
docker inspect <container-id>
Extract the config from the inspection result:
This commands highlights some key information like Cmd, Entrypoint, Environment Variable, Volumes etc
docker inspect ubuntu:latest | jq -M -r '.[].Config'
Creating tags:
Create a tag from existing tag
docker tag ubuntu:latest org.example/ubuntu:latest
Create a tag from image id
docker tag <image_id> com.example/ubuntu:latest
Clean up old/dangling volumes:
docker volume ls -qf dangling=true | xargs docker volume rm
Stop all running containers:
docker ps -q | xargs docker stop
Remove all stopped containers:
docker ps -aq | xargs docker rm
Remove all images:
docker images -aq | xargs docker rmi -f
Monitor docker container CPU and memory usage
docker stats
Cleanup unused data from docker
Interactive mode with confirmation
docker system prune --all --volumes
Non-interactive mode
docker system prune --all --volumes --force
Get docker disk information
docker system df
Get IP address of docker container
docker inspect <container name or id> | grep -oE "\"IPAddress\"\s*:\s*\"\b([0-9]{1,3}\.){3}[0-9]{1,3}\b\""
Networking
List available networks
docker network ls
List containers attached to the network
docker network inspect <<network_name>>
Create a bridge network
docker network create --driver bridge <<network_name>>Delete a bridge network
docker network rm <<network_name>>Run multiple standalone containers in a network
docker run -it --rm --network <<network_name>> --name local-pg-server -p 5432:5432 -e POSTGRES_DB=local_pg_server -e POSTGRES_USER=local_user -e POSTGRES_PASSWORD=local_password postgres:11.5-alpine
docker run -it --rm --network <<network_name>> --name local-pg-cli -e PGPASSWORD=local_password postgres:11.5-alpine psql -h local-pg-server -p 5432 -d local_pg_server -U local_user
docker run -it --rm --network <<network_name>> --name local-pg-cli -e PGPASSWORD=local_password postgres:11.5-alpine psql -h local-pg-server -p 5432 -d local_pg_server -U local_user
Comments
Post a Comment