Skip to content

ahmadbinshafiq/docker-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 

Repository files navigation

Docker-cheatsheet

Build a Docker image

$ docker build -t [image_name]:[tag] .

Run a Docker container specifying a name

$ docker run --name [container_name] [image_name]:[tag]

Fetch the logs of a container

$ docker logs -f [container_id_or_name]

Run a command in a running container

$ docker exec -it [container_id_or_name] bash

Show running containers

$ docker ps

Show all containers

$ docker ps -a

Show Docker images

$ docker images

Stop a Docker container

$ docker stop [container_id_or_name]

Remove a Docker container

$ docker rm [container_id_or_name]

Remove a Docker image

$ docker rmi [image_id_or_name]

Docker jupyter notebook

$ docker run -it --rm -p 8080:8080 image
$ jupyter notebook --ip 0.0.0.0 --no-browser --allow-root

Docker copy file from host to root

$ docker file/location/in/base/os container-id:/file-location

Docker tensorflow run

Need nvidia-docker installed to run gpu on containers
Nvidia-docker: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#docker
$ docker run --runtime=nvidia -it --rm tensorflow/tensorflow:devel-gpu-py3 bash

Docker volume mount to local host

Create a volume from terminal
$ docker volume create --driver local --opt type=none --opt device=/home/user/test --opt o=bind test_vol
Create a volume in docker-compose file

    bind-test: \
      driver: local \
      driver_opts: \
        type: none \
        o: bind \
        device: /home/user/test \

NOTE: In docker compose, volume doesn't create a local directory on the host. You have to make sure that the directory exists otherwise the mount will fail

Accessing cv2 windows on Docker

$ xhost + && docker run --rm -ti --net=host --ipc=host -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --device /dev/dri:/dev/dri -v volume/to/mount docker-image:tag

Docker save dependencies installed in an image

First find container_id from docker ps
$ docker commit [container_id]:[tag - optional]

Push Docker images to Docker Hub

$ docker tag [image_name] [docker_hub_username/image_name]
$ docker push [docker_hub_username/image_name]

Open interactive terminal of Docker images (Ubutu, PyTorch, etc)

$ docker run -it -v [absolute_path_to_our_files]:/[terminal_dir_name] --rm [image_name]

Get running docker container IP Address

$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_id>

Remove all none images

$ docker rmi $(docker images -f dangling=true -q )

To remove all volumes attached

$ docker-compose down -v

Remove all stopped docker containers from cache

$ docker rm docker ps --no-trunc -aq``

Save a docker image to tar file

$ docker save -o <tar_filename>.tar <image_name:tag>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published