Skip to content

Instantly share code, notes, and snippets.

@pnijem
Last active August 1, 2022 10:33
Show Gist options
  • Save pnijem/9645ec06476b38b41058ae8f6ec5a402 to your computer and use it in GitHub Desktop.
Save pnijem/9645ec06476b38b41058ae8f6ec5a402 to your computer and use it in GitHub Desktop.
Docker Useful Commands
Create and Start a container from an image

docker run <image_name> e.g. docker run nginx docker run = docker create + docker start

List containers

docker ps To include all containers (including stopped ones) run: docker ps #####a

Stop a container

docker stop <container_name>

Remove a container

docker rm <container_name>

Remove image

docker rmi <image_name>

Pull image

docker pull <image_name>

Execute a command on a docker container

docker exec #####it <container_name> <command> e.g. docker exec my_container cat /etc/hosts

Run a docker container in a detached mode

docker run #####d <container_name>

Attach a detached docker container

docker attach <container_name> docker attach <first_4_digits_container_id>

List docker images

docker images

Run a specific version of an image (tag)

docker run <image_name>:<image_version> e.g. docker run redis:4.0

Map standard input (STDIN) from my host to the docker container

docker run -it <container_name>

-t terminal -i interactive

Port mapping: docker host to docker container

docker run -p <docker_host_port>:<docker_container_port> <container_name> e.g. docker run -p 80:5000 <container_name>`

Volume mapping (mounting)

docker run -v <external_dir>:<container_dir> e.g. docker run -v /opt/datadir:/var/lib/mysql mysql

Inspect Container

docker inspect <container_name>

Container logs

docker logs <container_name>

Build an image

docker build . #####t <username>/<image_name>

Tag an image

docker build #####t <your_docker_id>/<project_repo>:<image_version>

Push an image to dockerhub

docker push <image_name>

Login to dockerhub

docker login

Login to private registry

docker login private-registry.io

Run a container with environment variable

docker run -e <env_variable_key=env_variable_value> <image_name>

Override the ENTRYPOINT via Run command

docker run --entrypoint <new_entrypoint_vlaue> <image_name> <other_values>

Link between containers

docker run -d --name=<host_name> --link <container2_name>:<host2_name> <container_name> e.g. docker run -d --name=vote --link redis:redis voting#####app

Note: links are not needed starting from version 2 of docker-compose file

Restrict the amount of memory/cpu for a certain container

docker run --cpus=.5 ubuntu This will ensure that the container will not take more than 50% of the host CPU at any given time

docker run --memory=100m ubuntu This will ensure that the container will not take more than 100m of the host memory at any given time

Create a Volume

docker volume create <volume_name>

Run a container specifying a volume ##### Volume Mounting

docker run -v <volume_name>:<volume_path> <container_name>

Note: this operation mounts a volume from the volumes directory Note: even if the container is destroyed, the data will remain valid.

List volumes

docker volume ls

Bind mounting

docker run -v <full_path_of_external_dir>:<volume_path> <container_name> docker run --mount type=bind, source=<full_path_of_external_dir>, target=<volume_path> <container_name>

Note: mount any directory from the docker host

Show the history of an image

docker history <image_name>

Show docker disk usage

docker system df docker system df -v

Create docker Network

docker network create ##########driver bridge ##########subnet <ip_address> <network_name>

List docker Networks

docker network ls

Inspect docker network

docker inspect <cotainer_name_id>

Getting command prompt in a Container

docker exec -it <container_id> <command_prompt>

Rotating Docker Container Logs

docker run --log-opt max-size=10m --log-opt max-file=5 my-app:latest

This sets a maximum of five log files with a max size of 10 Mb each. So at most 50 Mb of logs for that container.

Container Metrics

docker stats <container_id>

Copy file out from the container

docker cp <container_id>:/path/to/useful/file/local-path

Docker restart

docker run --restart <argument>

Argument Description

no This is the default value, it means that the containers would not be restarted on-failure This would restart the container in case that there is an error and the container crashes always Always restart the container if it stops unless-stopped The container would always be restarted unless it was manually stopped

Show System Disk Usage

docker system df

Remove all 'none' images

docker rmi $(docker images | grep none | awk '{print $3}')

Inspect Docker Image

docker image inspect --format IMAGE <IMG_NAME> docker image inspect -f IMAGE <IMG_NAME>

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