This Repository contain the cheatsheet for all the docker commands
To search an existing image at registry.hub.docker.com/
$ docker search <image_name>
$ docker search ubuntu
This command will list following details of the images in the system
$ docker images
When You launch a container It eill give You a hexadecimal string, The 12 digits of that hexadecimal string is Container Unique ID
- To launch a Container from the Latest Version of Image
$ docker run <option_name> <image_name>
Note Various docker run flavor with various option
$ # To launch an image in background -d option
$
$ docker run -d <image_name>
$ docker run -d ubuntu
$ # To Map Host Port To the Container Port - The port on the host is mapped to 0.0.0.0
$
$ docker run -p <host-port>:<container-port> <image_name>
$
$ # or To bind a particular Ip
$
$ docker run -p <ip_addr>:<host-port>:<container-port> <image_name>
$
$ # or To bind random Port of Host Machine
$
$ docker run -p <container-port> <image_name>
$ # To list port alloted
$
$ docker port -p <frinedly_name | container_id> <container_port>
$ # To run Container with different name
$ docker run --name <different_name> <image_name>
$ # container are designed to be build stateless, That is if You recreate a container the data will be removed
$ # You can bind the Host directory with Your Container directory (or volumes) This lets you persist your data
$ docker run -v <host-dir-path>:<container-dir-path>
$ docker run -v $PWD:<container-dir-path>
$ # To use data from other docker container
$ docker run --volume-from <container_name> <image_name>
-
To launch a specific Version of the image:
$ docker run <option_name> <image_name>:<vesion_number>
This command will list following information Container ID, Image, command, Created, Ports, Names(Friendly_name)
$ docker ps
$ docker inspect <friendly_name | container_id >
To view what was the output that a container that docker run as background, This is same as running the docker container in Foreground
$ docker logs <friendly_name | container_id >
Note: Docker Container are Sanboxed, It means the host directly processes can't directly access the services running in docker. If a service running in Docker container need to be accessed by a process not running in a container, then the port needs to be exposed via the Host. This can be done either in the Dockerfile or the way as shown in 2 )
Before reading this command read, How to Write Dockerfile? You can create a Docker Image From a Dockerfile using this command
$ docker build -t <image_name>:<tag or version_number> <build_destination_folder>
Note: You can remove an image only and only if the image has no referenced container, until you pass the forcefully command
$ docker rmi <image_id | friendly_name>
Note: You can remove an image only and only if the image has no referenced container
$ docker kill <image_id | friendly_name>
$ docker system prune
Docker create command is used to create a new container(generally data container).
$ docker create <option_name> <image> <command_optional> <arguments>
$ docker create -v /data --name my_data_container ubuntu
It is a two way copy command it copies the data from src to destination as mentioned below.
$ docker cp <options_name> <container>:<src_in_container> <dest_path> or <->
$ docker cp <option_name> <src_in_system> or <-> <container>:<dest_path>
To export a container in tar format
$ docker export <option_name> <container_name>
$ docker export --output="file.tar" <container_name>
$ docker export <container_name> > file.tar
To export a container in tar format
$ docker export <option_name> <container_name>
$ docker export --output="file.tar" <container_name>
$ docker export <container_name> > file.tar