Last active
March 25, 2022 09:56
-
-
Save jgeek/36ad640384905706f947 to your computer and use it in GitHub Desktop.
docker commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/wsargent/docker-cheat-sheet | |
# find PID if a container: | |
docker inspect --format '{{ .State.Pid }}' CONTAINER_ID | |
# Get the latest Docker package | |
$ wget -qO- https://get.docker.com/ | sh | |
# lists dockers images | |
$ sudo docker images | |
# run an image | |
$ sudo docker run <image_name> | |
# DockerFile example | |
FROM docker/whalesay:latest | |
RUN apt-get -y update && apt-get install -y fortunes | |
CMD /usr/games/fortune -a | cowsay | |
# takes the Dockerfile in the current directory, and builds an image called docker-whale on your local machine | |
$ docker build -t docker-whale . | |
# tag an image | |
docker tag image_id docker_hub_account_name/image_namge:version_label/tag | |
$ docker tag 7d9495d03763 maryatdocker/docker-whale:latest | |
# delete image | |
$ docker rmi -f <image_id> | |
------------------------- | |
Up and Running Docker book notes: | |
installing docker: | |
Ubuntu Linux 14.04 (64-bit) | |
$ sudo apt-get update | |
$ sudo apt-get install docker.io | |
$ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker | |
runnig docker server (daemon): | |
$ sudo docker -d -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375 | |
#To enable the docker server to start when the system boots type: | |
$ sudo update-rc.d docker.io defaults | |
# start docker service immediately: | |
$ service docker.io start | |
#shows docker processes | |
$ sudo docker ps | |
#creates running container in the background from the | |
#image with the example/docker-node-hello:latest tag, and then map port 8080 in the | |
#container to port 8080 on the Docker host. | |
$ sudo docker run -d -p 8080:8080 example/docker-node-hello:latest | |
# stopping container | |
$ sudo docker stop <image_id> | |
# sending evironment variable to docker process | |
$ $ docker run -d -p 8080:8080 -e WHO="Mohammad" | |
# logi/ logout. ~/.docker/config.json caches authentication data | |
$ sudo docker login | |
$ sudo docker logout | |
# If we were trying to log in to something other than the Docker Hub registry | |
$ sudo docker login someregistry.example.com | |
# creating local docker registry | |
sudo docker run -d -p 5000:5000 -v ~/workspace/docker/registry:/tmp/registry -e SETTINGS_FLAVOR=dev -e STANDALONE=true -e MIRROR_SOURCE=https://registry-1.docker.io -e MIRROR_SOURCE_INDEX=https://index.docker.io registry | |
# push to registry | |
$ docker push localhost:5000/myfirstimage | |
# docker images are stored at /var/lib/docker | |
# see if kernel support resources (cpu,ram & ...) limitations | |
$ docker info | |
# docker container label | |
$docker run -d --name labels -l deployer=Ahmed -l tester=Asako ubuntu:latest sleep 1000 | |
#filter containers based of lable | |
$docker ps -a -f label=deployer=Ahmed | |
# docker stress load | |
$ docker run --rm -ti progrium/stress --cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 120s | |
# if using above command with -c 512 , stress test is done by using half of available cpu time. | |
# in docker 1024 is the number that Docker assigns to represent the full pool | |
$ docker run --rm -ti -c 512 progrium/stress --cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 120s | |
# CPU pinning : we have restricted two CPU-bound processes to a single CPU. | |
$ docker run --rm -ti -c 512 --cpuset=0 progrium/stress --cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 120s | |
# memory: just use 512m memory and 512m swap | |
$ docker run --rm -ti -m 512m progrium/stress --cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 120s | |
# set swap size seperately; 512m memory and 256m swap. setting -1 for swap, disable it for this process. | |
$ docker run --rm -ti -m 512m --memory-swap=768m progrium/stress --cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 120s | |
# also we can limit resources for docker and containers with ulimit | |
# enable resouce accounting on ubunto to avoid 'WARNING: No swap limit support' | |
$ sudo vim /etc/default/grub | |
then set : GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1" | |
# auto restrating container: --restart=on-failure:3 retry 3 times, --restart=no, --restart=always | |
$ docker run -ti --restart=on-failure:3 -m 200m --memory-swap=300m progrium/stress --cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 120s | |
# stopping/killing/starting the container. stop sends SIGTERM unix signal to the container process. | |
$ docker stop/start/kill <container_id> | |
# force stop the container after a timeout (in seconds). after timeout, SIGKILL is send to conatiner process | |
$ $ docker stop -t 25 | |
# killing a container | |
$ docker kill <container_id> | |
# pausing / unpausing a container | |
$ docker pause / unpause <container_id> | |
# listing all containers | |
$ docker ps -a | |
# deleting a container | |
$ docker rm <container_id> | |
# deleting an image | |
# docker rmi <image_id> | |
# To delete all of the containers on your Docker hosts | |
$ docker rm $(docker ps -a -q) | |
# to delete all the images on your Docker host | |
$ docker rmi $(docker images -q -) | |
# To remove all containers that exited with a nonzero state, you can use this filter: | |
$ docker rm $(docker ps -a -q --filter 'exited!=0') | |
# to remove all untagged images | |
$ docker rmi $(docker images -q -f "dangling=true") | |
# to pull a specific version of an image from Docker Hub or any registry | |
# based on Docker’s Registry 2.0 codebase by using the digest attached to the desired image | |
$docker pull ubuntu@sha256:2f9a...82cf | |
# inspect a container | |
$ docker isnpect <contianer_id> | |
# get container shell | |
$ docker exec -ti <container_id> /bin/bash | |
# run a bash on remote docker and get result on local terminal | |
$ docker run <image_id / container_id> /bin/bash -c "/bin/....." | |
# run image as daemon | |
$ docker run -d -ti ubuntu | |
# log tools : Spotify, Logspout | |
# http://sirile.github.io/2015/06/26/elasticsearch-logstash-kibana-and-logspout-on-docker.html | |
# monitoring docker : | |
$ docker stats <container_id> | |
# cAdvisor | |
$ docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --publish=3030:3030 --detach=true --name=cadvisor google/cadvisor:latest | |
# run container in detached mode (background) | |
$ docker run -d <image_name> | |
# atach a running container (bring to foreground) | |
$ docker attach --sig-proxy=false <container_id> | |
# push to registry without login | |
$ vim /etc/default/docker | |
# DOCKER_OPTS="--insecure-registry docker_registry_address:port" | |
# run docker command without sudo | |
# Add the docker group if it doesn't already exist: | |
$ sudo groupadd docker | |
# Add the connected user "${USER}" to the docker group. Change the user name to match your preferred user: | |
$ sudo gpasswd -a ${USER} docker | |
# Restart the Docker daemon: | |
$ sudo service docker restart | |
# If you are on Ubuntu 14.04 and up use docker.io instead: | |
$ sudo service docker.io restart | |
#Either do a newgrp docker or log out/in to activate the changes to groups. | |
# location of local images: | |
# /var/lib/docker | |
#prevent container from exit after its job is done | |
$ docker run -d image_name sleep infinity | |
$ docker run -d -t image_name | |
$ docker run -d image_name tail -f /dev/null | |
# find ip of container | |
$docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' {container_id} | |
docker net conflict | |
sudo vim /var/snap/docker/796/config/daemon.json | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment