Skip to content

Instantly share code, notes, and snippets.

@Sanix-Darker
Last active September 5, 2019 19:43
Show Gist options
  • Save Sanix-Darker/b9eaff3c52c756fbc679927c380f4fed to your computer and use it in GitHub Desktop.
Save Sanix-Darker/b9eaff3c52c756fbc679927c380f4fed to your computer and use it in GitHub Desktop.
[SHELL, DOCKER]Mon docker commands
docker ps — Lists running containers. Some useful flags include: -a / -all for all containers (default shows just running) and —-quiet /-q to list just their ids (useful for when you want to get all the containers).
docker pull — Most of your images will be created on top of a base image from the Docker Hub registry. Docker Hub contains many pre-built images that you can pull and try without needing to define and configure your own. To download a particular image, or set of images (i.e., a repository), use docker pull.
docker build — The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. Use the -t flag to label the image, for example docker build -t my_container . with the . at the end signalling to build using the currently directory.
docker run — Run a docker container based on an image, you can follow this on with other commands, such as -it bash to then run bash from within the container. Also see Top 10 options for docker run — a quick reference guide for the CLI command. docker run my_image -it bash
docker logs — Use this command to display the logs of a container, you must specify a container and can use flags, such as --follow to follow the output in the logs of using the program. docker logs --follow my_container
docker volume ls — This lists the volumes, which are the preferred mechanism for persisting data generated by and used by Docker containers.
docker rm — Removes one or more containers. docker rm my_container
docker rmi — Removes one or more images. docker rmi my_image
docker stop — Stops one or more containers. docker stop my_container stops one container, while docker stop $(docker ps -a -q) stops all running containers. A more direct way is to use docker kill my_container, which does not attempt to shut down the process gracefully first.
Use them together, for example to clean up all your docker images and containers:
kill all running containers with docker kill $(docker ps -q)
delete all stopped containers with docker rm $(docker ps -a -q)
delete all images with docker rmi $(docker images -q)
To learn about deleting containers in more depth check out: Clean out your Docker images, containers and volumes with single commands
----------------------------------------------------------------------------------------------------------------------------
Below is a list of commands that I use often when performing container management and cleanup. This list does not include docker-compose commands, but rather some of the quick one-liners that I use for managing my docker containers, images, and volumes. I will try to keep this updated when I think of more.
List all running containers
$ docker ps
Kill all running containers
$ docker kill $(docker ps -aq)
Shell into an ubuntu container (for testing stuff) and delete container after I exit the session
$ docker run -it --rm ubuntu bash
Shell into a running container to tweak or modify things
$ docker exec -it my-container bash
Build and tag an image using the Dockfile in the current directory
$ docker build -t tatemz/my-image ./
Remove all untagged images
$ docker rmi $(docker images -q -f dangling=true)
Remove all orphaned volumes
$ docker volume rm $(docker volume ls -qf dangling=true)
Remove containers whose names were auto-generated
$ docker rm $(docker ps -aq -f name=admiring -f name=adoring -f name=affectionate -f name=agitated -f name=amazing -f name=angry -f name=awesome -f name=backstabbing -f name=berserk -f name=big -f name=boring -f name=clever -f name=cocky -f name=compassionate -f name=condescending -f name=cranky -f name=desperate -f name=determined -f name=distracted -f name=dreamy -f name=drunk -f name=eager -f name=ecstatic -f name=elastic -f name=elated -f name=elegant -f name=evil -f name=fervent -f name=focused -f name=furious -f name=gigantic -f name=gloomy -f name=goofy -f name=grave -f name=happy -f name=high -f name=hopeful -f name=hungry -f name=infallible -f name=jolly -f name=jovial -f name=kickass -f name=lonely -f name=loving -f name=mad -f name=modest -f name=naughty -f name=nauseous -f name=nostalgic -f name=peaceful -f name=pedantic -f name=pensive -f name=prickly -f name=reverent -f name=romantic -f name=sad -f name=serene -f name=sharp -f name=sick -f name=silly -f name=sleepy -f name=small -f name=stoic -f name=stupefied -f name=suspicious -f name=tender -f name=thirsty -f name=tiny -f name=trusting -f name=zen)
# ----------------------------------------------------------------------------------------------------------------------------------
# Lancer un container Hello-World
$ docker run hello-world
# Lancer un container docker sous ubuntu
$ docker run -it ubuntu bash
# Lister les container docker
$ docker ps
# Pour Tuer un container docker
$ docker rm ID_DOCKER
# Ou le nom que Docker lui a donner
$ docker rm sad_chatter...
# Creer le fichier 'Dockerfile'
# FROM python:2.7-slim
# WORKDIR ./app
# COPY . /app
# RUN pip install -r requirements.txt
# EXPOSE 80
# ENV NOM sanix
# CMD ["python", "app.py"]
# On build Notre image:
$ docker build -t sanix_docker .
# Pour lancer notre docker app, on a donc:
# Simplement:
$ docker run sanix_docker
# En mappant le port 80
$ docker run -p 80:80 sanix_docker
# Pour composer plusieurs container docker
$ vi docker-compose.yml
# Et on met ce contenue a l'interieur
version: "3"
services:
sanix_docker:
image: sanix_docker
depends_on:
- redis
ports: ['80:80']
networks: ['monreseau']
redis:
image: redis
ports:
- "6379:6379"
networks: ['monreseau']
networks:
monreseau:
# Puis on lance le docker-compose
$ docker-compose up
# Pour supprimer les containers et delete le reseau qui a ete creer ou autre:
$ docker-compose down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment