DEV Community

JL
JL

Posted on • Edited on

(WIP) Useful Docker Commands

Quickly Debug a Docker File

docker build . -t <name> 
docker image list
docker run -td <image>
docker ps
docker exec -it <container> /bin/bash
Enter fullscreen mode Exit fullscreen mode

OS Arch (CPU) type
Checking the type of docker image/container

dpkg --print-architecture
Enter fullscreen mode Exit fullscreen mode

Checking if your docker builder/node supports certain types

docker buildx ls
Enter fullscreen mode Exit fullscreen mode

Clean up
To stop all Docker containers, simply run the following command in your terminal:

docker kill $(docker ps -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
Enter fullscreen mode Exit fullscreen mode

To clear the cache of builder, use following:

docker builder prune
Enter fullscreen mode Exit fullscreen mode

Create multi-platform image
List all buildx on the machine
docker buildx ls

NAME/NODE       DRIVER/ENDPOINT STATUS  BUILDKIT PLATFORMS
default *       docker
  default       default         running 20.10.21 windows/amd64, linux/amd64
desktop-linux   docker
  desktop-linux desktop-linux   running 20.10.21 linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
Enter fullscreen mode Exit fullscreen mode

run docker context use desktop-linux to switch to context desktop-linux

Use specified buildx
docker buildx use desktop-linux

Check what the current buildx is
docker buildx inspect --bootstrap

Name:   default
Driver: docker

Nodes:
Name:      default
Endpoint:  desktop-linux
Status:    running
Buildkit:  20.10.21
Platforms: linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
Enter fullscreen mode Exit fullscreen mode

Time to build an image
docker buildx build . -t <image_name> --platform linux/arm64

Inspect further on an instance of this image
docker run --rm <image_name> uname -m

WARNING: The requested image's platform (linux/arm64) does not match the detected host platform (linux/amd64) and no specific platform was requested
aarch64
Enter fullscreen mode Exit fullscreen mode

Ref:
https://www.docker.com/blog/multi-arch-images/

*Conditional Logic in Docker Build *
By using ARG and ENV

FROM node:alpine

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app
ENV PORT 3000
ARG DOCKER_ENV
ENV NODE_ENV=${DOCKER_ENV}
RUN if [ "$DOCKER_ENV" = "stag" ] ; then  echo   your NODE_ENV for stage is $NODE_ENV;  \
else  echo your NODE_ENV for dev is $NODE_ENV; \
fi 
Enter fullscreen mode Exit fullscreen mode

when you build this Dockerfile with this command

docker build --build-arg DOCKER_ENV=stag -t test-node .
You will see at layer

---> Running in a6231eca4d0b your NODE_ENV for stage is stag
When you run this docker container and run this command your output will be


/usr/src/app # echo $NODE_ENV

stag
Enter fullscreen mode Exit fullscreen mode

Top comments (0)