Docker Shortcuts¶
Install Docker¶
Installing Docker on Debian based distro¶
sudo apt-get install docker
Allow non-root users to run docker¶
sudo usermod -aG docker $USER
Installing Docker on Windows¶
Go to https://hub.docker.com/editions/community/docker-ce-desktop-windows, download and install it. Once Docker is installed, use Docker Interactive Window or Powershell to use the following commands.
Login to docker¶
To use Docker’s public images, you must have a Docker ID or account, go to https://hub.docker.com/ and register your account if you don’t have one as yet. Otherwise, run the following command:
docker login
Checking if there’s any container running¶
docker ps
List all containers, event if they aren’t running
docker ps -a
Running a container¶
Docker run creates a new container from the docker image and starts the container docker run tutum/hello-world
docker run --name web1 tutum/hello-world
Running a docker container on a particular port¶
# docker run --name web2 -p exposePORTt:localPort tutum/hello-world
docker run --name web2 -p 8080:80 tutum/hello-world
Running docker containers in the background using daemon¶
Use -d or –detach to run a container in the background.
docker run -d --name web3 -p 8080:80 tutum/hello-world
docker run -d --name web4 -p 8081:80 tutum/hello-world
Get logs for a container¶
docker logs web3
for dlog in `docker ps -a -q`; do docker logs $dlog; done
Starting a docker container¶
docker start web3
Removing a container¶
# docker rm <container-name>
docker rm web3
docker rm $(docker ps -a -q)
Stop and remove all containers
docker rm $(docker ps -a -q) & docker stop $(docker ps -a -q)
List docker images¶
docker images
Moving Images from one host to another¶
You will need to save the Docker image as a tar file:
docker save -o <path for generated tar file> <image name>
Then copy your image to a new system with regular file transfer tools such as cp, scp or rsync(preferred for big files). After that you will have to load the image into Docker
docker load -i <path to image tar file>
Remove a docker image¶
docker rmi wordpress
if you do docker images and see <none>:<none> images in the list, these are dangling images and needs to be pruned, else they will unnecessary allocate your disks space. Run the following comand to remove dangling images
docker rmi $(docker images -f "dangling=true" -q)
docker rmi $(docker images -q)
docker system prune -a
It will output:
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all networks not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y