Skip to content

Instantly share code, notes, and snippets.

@devcfgc
Last active March 10, 2024 17:14
Show Gist options
  • Save devcfgc/c54c1fa0792bc086fb2cf4ba94a9c982 to your computer and use it in GitHub Desktop.
Save devcfgc/c54c1fa0792bc086fb2cf4ba94a9c982 to your computer and use it in GitHub Desktop.
docker commands

DOCKER

After installing

sudo usermod -aG docker $USER`                                     # add your user to the docker group
docker run -it <IMAGE:TAG> /bin/bash                               # run container in interactive mode and open a bash shell
docker run -d <IMAGE:TAG>                                          # run container in detach mode

Commands

docker build .                                                       # Build an image from path . (where dockerfile is located)
docker tag <IMAGE_ID> <TAG>                                         # Tag image by id
docker images -a                                                    # list all images
docker container ls                                                 # list running containers
docker container ls -a                                              # list all containers
docker ps -a                                                        # list all containers
docker ps -a -q
docker ps -a --format="table {{.Names}}\t{{.Image}}\t{{.Ports}}"    # NAMES IMAGE PORTS
docker ps -a |  grep "pattern"                                      # list containers base on a pattern
docker ps -a -f status=exited                                       # list containers in a exited status
docker stop $(docker ps -a -q)                                      # stop all containers

docker rm <CONTAINER_ID>                                            # remove container by id
docker rm $(docker ps -a -q)                                        # remove all containers
docker rmi <IMAGE_ID>                                               # remove image by id
docker rmi $(docker images -a -q)                                   # remove all images
docker images | grep "pattern" | awk '{print $1}' | xargs docker rm # remove images base on a pattern
docker ps -a | grep "pattern" | awk '{print $3}' | xargs docker rmi # remove containers base on a pattern
docker rm $(docker ps -a -f status=exited -q)                       # remove images in a exited status

docker exec -it <CONTAINER NAME> bash                               # this will create a new Bash session in the container
docker container exec <CONTAINER NAME> poweshell <COMMANDS>         # execute comands with powershell windows containers

docker container ls -lq                                             # get container ID of last container
docker container logs $(docker container ls -lq)                    # show output of last container
docker container diff $(docker container ls -lq)                    # show the differences between the container and the image

docker container prune                                              # remove all stopped containers

docker system df                                                    # check sizes
docker system prune                                                 # cleanup your host system

Show containers ip

docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' <<container-id>>
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
docker inspect <CONTAINER NAME> | grep IPAddress
docker inspect <CONTAINER NAME>                                     # get all details

Networking

docker network inspect bridge
docker network ls                                                   # List all container networks

Networking connect containers

docker container run -d -P --name  <CONTAINER NAME> <IMAGE:TAG>

DOCKER COMPOSE

docker-compose up                                                   # run all containers
docker-compose up -d                                                # run all containers in the background
docker-compose ps                                                   # check which containers are running
docker-compose logs -t client                                       # check the output of the client in its logs

DOCKERFILE BEST PRACTICES

WINDOWS

docker rmi $(docker images --filter "dangling=true" -q --no-trunc)  # remove unnecessary images

Use single backslash

  • Use the escape comment (windows)
    # escape=`
    FROM chocolateyfest/iis
    COPY iisstart.htm C:\inetpub\wwwroot
    
  • Use PowerShell - The default shell in Windows containers is cmd.exe
    # escape=`
    FROM mcr.microsoft.com/windows/servercore:ltsc2019
    RUN powershell -Command Invoke-WebRequest 'http://foo.com/bar.zip' `
                            -OutFile 'bar.zip' -UseBasicParsing
    RUN powershell -Command Expand-Archive bar.zip -DestinationPath C:\
    
  • Switch to PowerShell:
    Use the SHELL instruction to set PowerShell as default shell, so you don't have to write powershell -Command in each RUN instruction. Use $ErrorActionPreference = 'Stop' to abort on first error. Use $ProgressPreference = 'SilentlyContinue' to improve download speed.
    FROM mcr.microsoft.com/windows/servercore:ltsc2019
    SHELL ["powershell", "-Command", `
    "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment