Docker Cheat-sheet

Docker Cheat-sheet

Install docker

apt install

Uninstall docker

apt-get remove docker docker-engine

Export container to TAR file

docker export containerNameOrID > /home/user/my_file_name_$(date +%d-%m-%Y_%H-%M-%S).tar

Import container from TAR file

docker import /home/user/my_file_name.tar
docker import /home/user/my_file_name.tar RepoName:TagName

Pull image from

docker pull imageName
docker pull imageName:latest
docker pull imageName:1.2.3

Follow container logs

docker logs -f --since 1m containerNameOrID

Extract logs for a given time frame

docker logs --since 2020-11-14T08:00:00 --until 2020-11-14T11:00:00 containerNameOrID

Go inside the container

docker exec -it containerNameOrID /bin/bash


docker exec -it containerNameOrID /bin/sh

See images in local PC

docker images

List runnning containers

docker ps

List all containers

docker container ls --all

Start a container

docker start containerNameOrID

Stop a container

docker stop containerNameOrID

Remove container

docker container rm containerNameOrID

Build an image

docker build . -t myImageName

Delete an image by force

docker image rm --force myImageNameOrID

Inspect the layers of an image

docker history --human --format "{{.CreatedBy}}: {{.Size}}" myImageNameOrID

Run an image in a container in detached mode

docker run -p LocalPort:InternalPort -d myImageNameOrID

Run an image in a container in detached mode in the host network

docker run -p LocalPort:InternalPort -d myImageNameOrID --network host

Run an image in a named container and accessing the instance from the host without the container's IP

docker run -d --name newContainerName -e url=http://localhost:externalAppPort -p externalAppPort:internalAppPort imageName
docker run -d --name some-ghost -e url=http://localhost:3001 -p 3001:2368 ghost

Containers location


Follow logs

docker logs -f ContainerNameOrID --since "1m" | egrep '(String1|String2)'

Kill all containers

sudo docker kill $(sudo docker ps -q --all)

Stop all containers

sudo docker stop $(sudo docker ps -q --all)

Remove all containers

sudo docker rm $(sudo docker ps -q --all)

Execute command inside container (check Node.JS version inside container)

docker exec myContainerID node --version

Install Docker Compose

sudo curl -L "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

# Optional, just in case of failure after installation you should do:
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

# Test with:
docker-compose --version