Skip to content

Instantly share code, notes, and snippets.

@YaronMiro
Last active November 12, 2017 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YaronMiro/c5e907a128ee6a96facc8181570799a2 to your computer and use it in GitHub Desktop.
Save YaronMiro/c5e907a128ee6a96facc8181570799a2 to your computer and use it in GitHub Desktop.
Docker Cheetsheet

DockerCommands

Bash

  • Run a container with a default bash command.

    Example:

    docker container run -it --name container_name ubuntu bash

    Description:

    docker container run [OPTIONS] --name CONTAINER_NAME ubuntu COMMAND

  • Start an existing container with a default bash command.

    Example:

    docker container start -ai container_name

    Description:

    docker container start [OPTIONS] --name CONTAINER_NAME

  • Exit an existing container from bash mode.

    Example:

    exit

  • Execute a bash command on an existing container that is still in process.

    Example:

    docker container exec -it container_name bash

    Description:

    docker container exec [OPTIONS] --name CONTAINER_NAME COMMAND


Build

Build a Docker image from a Dockerfile.

Example:

docker image build -t yaronmiro/node:6.11.5-alpine .

Description:

docker image build [OPTIONS:TAG] => IMAGE_NAME DOCKERFILE_PATH


Detach

Run a Docker container process as background process on the CLI.

-d / --detach

Example:

docker container run -d


Inspect

Display detailed information of a target container.

Example:

docker container inspect container_name => output a json format object of the container.

docker container inspect container_name --format '{{ .NetworkSettings.IPAddress }}' => Get the container IP Address.


List

Display a list of containers'{{ .NetworkSettings.IPAddress }}'

aliases => ps, list

Example:

docker container ls => Only list the current running continers.

docker container ls -a => List all of the existing continers on the machine.


Name

Define a unique fixed name for the container process (Alternative identifier usage over a random genarated container ID).

-n / --name

Example:

docker container run --name container_name


Publish

Define the incoming connection from the host machine into the container by defining a port relationship.

-p / --publish

Example

docker container run --publish 80:80 => Define a single port mapping.

docker container run --publish 80:80 --publish 3000:3000 => Define a multiple port mapping. HOST_PORT : CONTAINER_PORT => Arguments description.


Remove image

docker image prune -f => Removes all history images from build process (repository => <none>)


Remove container

Remove one or more containers by specifing a single or multiple containers names or Ids.

rm

Example:

docker container rm container_name => Remove a single container.

docker container rm container_1 container_2 => Remove multiple containers.

docker container rm -f container_name => Remove by force a current running container.


Remove auto.

Remove a container automatically after exit.

--rm

Example:

docker container run --rm -it --name container_name nginx bash


Stats

Display a live monitor of container preformance.

Example:

docker container stats => Displays a list of all the current running container stats.

docker container stats container_name => Displays a target container stats.


Tag

Create a taged image.

Example:

docker image tag image image_with_a_tag_name


Top

Display the running processes inside the target container

Example:

docker container top container_name


Network

Docker network commands.

Example:

docker network ls => List all networks

docker network inspect network_name => Get a JSON format network details.

docker network create my_app_net => Create a new docker network.

docker network connect --network may_app_net container name => Connect a target container to specific network.

docker network disconnect --network may_app_net container name => Disconnect a target container from specific network.

docker container run --network may_app_net ngnix => Run a new container that is attached to a specific network.

docker container exec -it container_A ping container_B => Test connection bwtween containers on the by using the ping command.

docker container run -d --name search_Two --network search_net --net-alias search elasticsearch:2 => Create a container with an alias DNS search


Pull

Download an image from docker hub.

pull

Example:

docker image pull image_name


Push

Upload an image to docker hub.

push

Example:

docker image push image_name


Volume

  • docker volume ls => get a list of all the volumes.

  • docker volume inspect volume_name => get a list of all the volumes.

  • Volumes Binding name.

    docker container run --name my_container -v db:/var/lib/postgresql/data => Create a name binding for the volume.

    docker container run --name my_container -v [VOLUME_NAME]:[VOLUME_IMAGE_PATH]

  • Volumes Mount Binding.

    docker container run --name my_container -v project/app:/var/lib/postgresql/data => Create a name binding for the volume.

    docker container run --name my_container -v [HOST_DIRECTORY_PATH]:[VOLUME_IMAGE_PATH]

  • docker container inspect db_old => Inspect a the section that points to the {{ .Mounts }} config.


Clean docker data shortcuts.

docker kill $(docker ps -q) => KIll on the running docker process.

$ docker rm -v $(docker container ls -q -a) => Remove all of the containers.

$ docker rmi $(docker image ls -q) => Remove all the images.

$ docker rmi $(docker volumes ls -q) => Remove all the volumes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment