docker build with a tag
docker build -t wingkwong/example:latest .
Run a simple container using the hello-world image:
docker run hello-world
Run a container using a specific image tag:
docker run nginx:1.15.11
Run a container with a command and arguments:
docker run busybox echo hello world!
Run an Nginx container customized with a variety of flags:
- -d: Run container in detached mode. The docker run command will exit immediately and the container will run in the background
- --name: A container is assigned a random name by default, but you can give it a more descriptive name with this flag
- --restart: specify when the container should be automatically restarted
- no(default): never restart the container
- on-failure: only if the container fails (exits with a non-zero exit code)
- always: always restart the container whether it succeeds or fails. Also starts the container automatically on daemon startup
- unless-stopped: always restart the container whether it succeeds or fails, and on daemon startup, unless the container was manually stopped
docker run -d --name nginx --restart unless-stopped -p 8080:80 --memory 500M --memory-reservation 256M nginx
List any currently running containers:
docker ps
List all containers, both running and stopped:
docker ps -a
Stop the Nginx container:
docker container stop nginx
Start a stopped container:
docker container start nginx
Delete a container (but it must be stopped first):
docker container rm nginx
Downgrade to a previous version:
sudo systemctl stop docker
sudo apt-get remove -y docker-ce docker-ce-cli
sudo apt-get update
sudo apt-get install -y docker-ce=5:18.09.4~3-0~ubuntu-bionic docker-ce-cli=5:18.09.4~3-0~ubuntu-bionic
docker version
Upgrade to a new version:
sudo apt-get install -y docker-ce=5:18.09.5~3-0~ubuntu-bionic docker-ce-cli=5:18.09.5~3-0~ubuntu-bionic
docker version
Check the current default logging driver:
docker info | grep Logging
Edit daemon.json to set a new default logging driver configuration:
sudo vi /etc/docker/daemon.json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "15m"
}
}
Restart docker
sudo systemctl restart docker
Run a docker container, overriding the system default logging driver settings:
docker run --log-driver json-file --log-opt max-size=50m nginx
Install Docker Engine:
sudo apt-get update
sudo apt-get -y install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install -y docker-ce=5:18.09.5~3-0~ubuntu-bionic docker-ce-cli=5:18.09.5~3-0~ubuntu-bionic containerd.io
sudo usermod -a -G docker cloud_user
Initialize Docker Swarm
docker swarm init --advertise-addr <manager-private-ip>
View the current state of the swarm:
docker info
List the current nodes in the swarm and their status:
docker node ls
Get a join token from the manager. Run this command on the swarm manager:
docker swarm join-token worker
Copy the docker swarm join command provided in the output and run it on both workers:
docker swarm join --token <token> <manager-private-ip>:2377
Docker Swarm Backup (on manager)
sudo systemctl stop docker
sudo tar -zvcf backup.tar.gz -C /var/lib/docker/swarm
sudo systemctl start docker
Docker Swarm Restore (on manager)
sudo systemctl stop docker
sudo rm -rf /var/lib/docker/swarm/*
sudo tar -zxvf backup.tar.gz -C /var/lib/docker/swarm/
sudo systemctl start docker
docker node ls
View file system layes in an image. Nginx as an example
docker image history nginx
Delete an iamge. Nginx as an example
docker image rm nginx:1.14.0
Download an image
docker image pull nginx:1.14.0
List images on the system:
docker image ls
docker image ls -a
Force Deleteion of an image used by a container
docker run -d --name nginx nginx:1.14.0
docker image rm -f nginx:1.14.0
Delete an iamge. Nginx as an example
docker image rm nginx:1.14.0
Locate a dangling image and clean it up. Nginx as an example
docker image ls -a
docker container ls
docker container rm -f nginx
docker image ls -a
docker image prune
Inspect image metadata. Nginx as an example
docker image inspect nginx:1.14.0
docker image inspect nginx:1.14.0 --format "{{.Architecture}}"
docker image inspect nginx:1.14.0 --format "{{.Architecture}} {{.Os}}"
Enable Docker Swarm autolock
docker swarm update --autolock=true
Unlock the swarm using the unlock key
docker swarm unlock
Get the current unlock key
docker swarm unlock-key
Rotate the unlock key
docker swarm unlock-key -rotate
Disable autolock
docker swarm update --autolock=false
Start Compose application with detached mode:
docker-compose up -d
Stop Compose application
docker-compose down
List the Docker Compose container
docker-compose ps
Deploy a new Stack to the cluster using a compose file
docker stack deploy -c <compose_file> <stack>
List current stacks
docker stack ls
List the tasks associated with a stack
docker stack ps <stack>
List the services associated with a stack
docker stack services <stack>
Delete a stack
docker stack rm <stack>