Skip to content

Instantly share code, notes, and snippets.

@buonzz
Last active March 6, 2022 11:18
Show Gist options
  • Save buonzz/c30d83bb7107d0ff3d012b582d1f6103 to your computer and use it in GitHub Desktop.
Save buonzz/c30d83bb7107d0ff3d012b582d1f6103 to your computer and use it in GitHub Desktop.
docker-cheatsheet
# install it
curl -sSL https://get.docker.com/ | sh
# ubuntu 16 + mounter folder in volume
https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-docker-ce-1
https://docs.docker.com/compose/install/
https://linuxconfig.org/how-to-move-docker-s-default-var-lib-docker-to-another-directory-on-ubuntu-debian-linux
# if you are using docker other than root
sudo usermod -aG docker <user>
# build an image
docker build .
# build and tag an image
docker build -t user/myapp:1.0.2 -t user/myapp:latest .
# prune images, with no containers
docker image prune -a
# prune containers
docker container prune
# prune volumes
docker volume prune
# list all containers even the non-running one
docker ps -a
# run mysql server in the background with mypassword as root password
docker run --detach --name=test-mysql --env="MYSQL_ROOT_PASSWORD=mypassword" mysql
# check the logs
docker logs test-mysql
# get the IP address of the container
docker inspect test-mysql
# run a wordpress container, then link the mysql server
docker run --detach --name test-wordpress --link test-mysql:mysql wordpress
# access the container in shell
docker exec -it test-wordpress bash
# pass parameters to mysql
docker run \
--detach \
--name=test-mysql \
--env="MYSQL_ROOT_PASSWORD=mypassword" \
--publish 6603:3306 \
mysql \
--max-connections=200 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci
# initialize swarm
docker swarm init --advertise-addr <MANAGER-IP>
# view current state of swarm
docker info
# list nodes in swarm
docker node ls
# add a node in swarm
docker swarm join-token worker # get the token from manager first, then login to the other node and execute it
# deploy a service to swarm
docker service create --replicas 1 --name helloworld alpine ping docker.com
# inspect service
docker service inspect --pretty helloworld
# copy files inside container
docker cp <containerId>:/file/path/within/container /host/path/target
# stop all containers
docker stop $(docker ps -aq)
# remove all containers
docker rm $(docker ps -aq)
# remove all images
docker rmi $(docker images -q)
# clear cache
docker builder prune
see more at https://github.com/wsargent/docker-cheat-sheet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment