Skip to content

Instantly share code, notes, and snippets.

@overtomanu
Last active April 30, 2023 16:56
Show Gist options
  • Save overtomanu/6d1215eecfa2080e68894fee0bced3cf to your computer and use it in GitHub Desktop.
Save overtomanu/6d1215eecfa2080e68894fee0bced3cf to your computer and use it in GitHub Desktop.
Docker commands
Description Command Comments
check if docker daemon is running systemctl status docker
start docker daemon in systemd unix systems sudo systemctl start docker
start docker daemon manually dockerd
Display system wide information regarding the Docker installation docker info
hello world test docker run hello-world
run by mapping ports docker run -p 9090:80 --name hwNginx1 kitematic/hello-world-nginx "hwNginx1 is the container name,
kitematic/hello-world-nginx is image name
for -p option, first one is host port which is mapped to docker port specified after colon(:)"
>> from podman registry docker run -p 9090:80 --name hwNginx1 quay.io/aptible/nginx
>>run by specifying environment variable docker run -e MYVAR=myprop --name hwNginx1
>>run by mapping storage volumes docker run -v HOST_PATH:CONTAINER_PATH kitematic/hello-world-nginx
get interactive shell inside container docker exec -it hwNginx1 sh
attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name docker attach CONTAINER_NAME_OR_ID to detach use CTRL-p CTRL-q key sequence
>> docker attach example "docker run -d --name topdemo ubuntu /usr/bin/top -b
docker attach topdemo"
see container instance configuration docker container inspect hwNginx1
stop running container docker stop hwNginx1
>>stop all containers docker stop $(docker ps -q)
start stopped container docker start hwNginx1
list local images docker images
>>alternative command docker image ls
list running containers docker container ls
>>this command also works docker ps
list all containers including stopped ones docker container ls -a
>>this command also works docker ps -a
see container logs docker logs hwNginx1
>>tail container logs (get live container logs in the terminal) docker logs -f hwNginx1
see help for docker command docker --help
>>eg: docker run --help
remove container permanently docker rm hwNginx1
>>remove all stopped containers docker rm $(docker ps -q --filter status=exited)
>>remove all containers docker rm -f $(docker ps -a -q)
remove local image docker image rm kitematic/hello-world-nginx
>>alternative command docker rmi kitematic/hello-world-nginx
>>delete all images docker rmi $(docker images -q)
>>delete untagged (dangling) images docker rmi $(docker images -q -f dangling=true)
>>delete unused images older than 3 months ( interactive ) "docker image prune -a --filter ""until=$((3 * 730))h""" use -f for non interactive
run container and delete after exit docker run --name auto_deleted_container --rm hello-world Clean up (--rm)
run a docker container if not already running docker start nginx
display detailed information about image docker image inspect hello-world
suspend all processes in container docker pause hwNginx1
resume all processes in container docker unpause hwNginx1
docker stop vs pause "docker pause sends SIGSTOP signal to the container process,
whereas docker stop will send SIGTERM signal to the process, after a grace period will receive SIGKILL"
listen to docker events docker events "docker events
2021-05-17T11:50:43.261440400+05:30 container pause cbacf5758ec7235b59a92a85be5591dc85d9ca5b401b89b8e77ba7d1e7dfcd65 (image=kitematic/hello-world-nginx, name=hwNginx1)
2021-05-17T11:51:05.117315400+05:30 container unpause cbacf5758ec7235b59a92a85be5591dc85d9ca5b401b89b8e77ba7d1e7dfcd65 (image=kitematic/hello-world-nginx, name=hwNginx1)"
list volumes of docker container docker inspect -f '{{ .Mounts }}' containerid docker inspect -f '{{ .Mounts }}' $(docker ps -a
remove all dangling volumes docker volume rm $(docker volume ls -f dangling=true -q)
build image from docker file in current directory docker build -t imageTag .
run mongodb container docker run -p 27017:27017 --name mongodbcntnr1 -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret -v /Users/manobhat/MyFiles/program_files/mongodb-data:/data/db -d mongo
connecting to running mongodb in container docker exec -it mongodbcntnr1 bash
>> run mongo client inside container bash "mongo --host localhost \
-u mongoadmin \
-p secret \
--authenticationDatabase admin \
admin" "mongo --host $(hostname) \
-u mongoadmin \
-p secret \
--authenticationDatabase admin \
admin"
copy file inside container to outside "#9a9e056e2639 is container id, got by executing docker ps -a OR use container name (easier option)
docker cp 9a9e056e2639:/scratch/software/mw_local/FMWTOOLS_12.2.1.5.0_GENERIC_210715.1527.2193.S_PATCHES4FA_FMW12C_PLATFORMS_JDK64/fadocker/mw_home_standalone/user_projects/domains/fusion_domain/servers/AdminServer/adr/diag/ofm/fusionapps/ORA_FSCM_SERVICESAPP/incident/incdir_29/readme.txt /scratch/public_shared/" docker cp $(docker ps -a tail -1
copy file from outside machine to docker container docker cp -r /scratch/tmp/EQUK_TEST_Metadata $(docker ps -a tail -1
create a network docker network create my-net
remove a network docker network rm my-net
create a container and connect it to network "docker create --name my-nginx \
--network my-net \
--publish 8080:80 \
nginx:latest"
connect a running container to network docker network connect my-net my-nginx
disconnect container from a network docker network disconnect my-net my-nginx
podman machine commands (mac and windows)
list all vms for running containers podman machine list
initialize a default virtual machine podman machine init
If you are using podman behind a proxy then set HTTP_PROXY env variable and then do podman machine start, then proceed with podman-compose up
start default linux vm for running containers podman machine start
stop default linux vm for running containers podman machine stop
start containers as configured in docker-compose.yaml docker-compose up podman-compose -f docker-compose.yaml up
>> specifying compose file docker-compose -f docker-compose-test.yml down
>> specifying project name docker-compose -f 2docker-compose.yaml -p boss-project2 up
stop and remove containers as configured in docker-compose.yaml docker-compose down podman-compose -f docker-compose.yaml down
>> or press ctrl+c in the running docker-compose terminal
>> in podman podman-compose -f docker-compose.yaml up
podman-compose -f docker-compose.yaml down
#viewing containers and using default bridge network
docker network ls
docker network inspect podman
docker network inspect bridge
docker run -dit --name alpine1 alpine ash
docker run -dit --name alpine2 alpine ash
docker attach alpine1
ip addr show
ping -c 2 google.com
ping -c 2 alpine2
docker container stop alpine1 alpine2
docker container rm alpine1 alpine2
#user defined network
#User-defined bridges provide automatic DNS resolution between containers.
#Containers on the default bridge network can only access each other by IP addresses
docker network create --driver bridge alpine-net
docker network inspect alpine-net
docker run -dit --name alpine1 --network alpine-net alpine ash
docker run -dit --name alpine2 --network alpine-net alpine ash
docker run -dit --name alpine3 alpine ash
docker run -dit --name alpine4 --network alpine-net alpine ash
docker network connect bridge alpine4
docker network inspect bridge
docker network inspect alpine-net
docker container attach alpine1
#below pings should be successful
ping -c 2 alpine2
ping -c 2 alpine4
ping -c 2 alpine1
ping -c 2 google.com
#bad address for below
ping -c 2 alpine3
#cannot ping alpine3 even by its ip address
docker container attach alpine4
#below pings should be successful
ping -c 2 alpine1
ping -c 2 alpine2
ping -c 2 alpine4
#bad address for below
ping -c 2 alpine3
#but can ping alpine3 by its ip address
#since alpine4 is connected to both bridge and alpine-net networks
#cleanup
docker container stop alpine1 alpine2 alpine3 alpine4
docker container rm alpine1 alpine2 alpine3 alpine4
docker network rm alpine-net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment