Skip to content

Instantly share code, notes, and snippets.

@Nairit11
Last active April 27, 2024 07:59
Show Gist options
  • Save Nairit11/59d7ef2beca03b6a770e4c278e4b4aa9 to your computer and use it in GitHub Desktop.
Save Nairit11/59d7ef2beca03b6a770e4c278e4b4aa9 to your computer and use it in GitHub Desktop.
Cheat Sheet for Docker

DOCKER CHEAT SHEET

Run an Image

docker run image_name

To run a specific version use a tag. By default tag is 'latest'

docker run image_name:version

Give a container name while running an image

docker run --name container_name image_name

Run an Image in Detached mode(in background)

docker run -d image_name

Above command returns ID of the container. To attach to the container use

docker attach container_ID

Providing first few letters of the ID works fine.

To view logs of a container running in detached mode use

docker logs container_name

Run an Iamge in an interractive way with promts printed in the terminal

docker run -it image_name

Map docker container port to docker host port

docker run -p host_port:container_port image_name

Now the application running on the docker container at http://CONTAINER_IP:CONTAINER_PORT can be accessed from outside the container at http://HOST_IP:HOST_PORT

Persist container data in docker host(Volume Mapping)

docker run -v external_host_directory:container_directory image_name

List all running Containers

docker ps

List all current and previously run Containers

docker ps -a

Stop a running Container

docker stop container_name

Remove a stopped container

docker rm container_name

List all images

docker images

Remove an Image not currently used by any running container

docker rmi image_name

Download an image

docker pull image_name

Executing a command in a container

docker exec container_name command

Example: docker exec nginx_container cat /etc/hosts

Inspect details ofa container

docker inspect container_name

Set an Environment Variable

docker run -e ENV_VAR_NAME=VALUE image_name

Find value of a Environment Variable

docker inspect container_name | grep ENV_VAR_NAME

Build an Image

docker build Dockerfile -t account_name/image_name

Push Image to Docker Hub

docker push account_name/image_name

Running docker container in a specific network(Default is Bridge)

docker run image_name --network=network_name

Create a isolated network

docker network create --driver bridge --subnet SUBNET_IP_RANGE --gateway GATEWAY_ADDR custom_isolated_network_name

List all available networks

docker network ls

Create a volume in Docker host

docker volume create volume_name

Map a location in a container to an external volume

docker run -v PATH_TO_EXTERNAL_VOLUME:PATH_IN_CONTAINER image_name

If external volume is not already created, docker creates it in /var/lib/docker/volumes directory.

Can also be written as

docker run --mount type=bind, source=PATH_TO_EXTERNAL_VOLUME, target=PATH_IN_CONTAINER image_name

Start a docker compose stack written in a docker-compose.yml file

docker-compose up

Link a docker container while running an image

docker run --link NAME_OF_CONTAINER_TO_BE_LINKED:CONTAINER_THE_IMAGE_IS_LOOKING_FOR image_name

Connect to Docker on a remote host

docker -H=remote_host_IP_addr:remote_host_PORT run image_name

Docker Swarm

To start a Swarm Manager(Master)

docker swarm init

This returns a token for workers/slaves to join

To add a worker/slave host to the manager

docker swarm join --token <token>

To add multiple replicas of a service

docker service create --replicas=NUM_OF_REPLICAS image_name

To be run on the Swarm Manager.

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