Docker Commands
On this page
Find Docker Usage
docker system df
docker system prune -a --volumes
ā¼ļø WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all volumes not used by at least one container
- all images without at least one container associated to them
- all build cache
Build image
docker build -t frontend --build-arg NPM_AUTH_TOKEN=TOKEN .
Download newer image
docker pull <image name>
Display all running containers
docker ps -a
check running containers
docker ps
dive into docker container
docker exec -it dreamy_nash /bin/bashdocker exec -it 1d3912322644 /bin/bash
How to clean your Docker data
Cleaning Up Your Docker Environment
#!/bin/bash# Delete all containersdocker rm $(docker ps -a -q)# Delete all imagesdocker rmi $(docker images -q)# Kill a particular container.docker kill [container]
- docker kill \$(docker ps -q): Kill all containers that are currently running.
- docker rm [container]: Delete a particular container that is not currently running.
- docker rm \$(docker ps -a -q): Delete all containers that are not currently running.
- docker rmi \$(docker images -a -q): Delete all images
To start a shell in a container:
docker exec -ti \$(CONTAINER) /bin/bash
exec run a command in a running container Exec -interactive Keep STDIN open even if not attached Exec -t: tty allocate a pseudo TTY not genuine but having the appearance of
ADD vs COPY Although ADD and COPY are functionally similar, generally speaking, COPY is preferred. Thatās because itās more transparent than ADD. COPY only supports that basic copying of local files into the container, while ADD has some features ( like local-only tar extraction and remote URL support) that are not immediately obvious.
If you have multipleĀ DockerfileĀ steps that use different files from your context,Ā COPYĀ them individually, rather than all at once. This ensures that each stepās build cache is only invalidated (forcing the step to be re-run) if the specifically required files change. For example:
COPY requirements.txt /tmp/RUN pip install --requirement /tmp/requirements.txtCOPY . /tmp/
Results in fewer cache invalidations for theĀ RUNĀ step, than if you put theĀ COPY . /tmp/Ā before it. Because image size matters, usingĀ ADDĀ to fetch packages from remote URLs is strongly discouraged; you should useĀ curlĀ orĀ wgetinstead. That way you can delete the files you no longer need after theyāve been extracted and you donāt have to add another layer in your image. For example, you should avoid doing things like:
ADD http://example.com/big.tar.xz /usr/src/things/RUN tar -xJf /usr/src/things/big.tar.xz -C /usr/src/thingsRUN make -C /usr/src/things allAnd instead, do something like:RUN mkdir -p /usr/src/things \&& curl -SL http://example.com/big.tar.xz \| tar -xJC /usr/src/things \&& make -C /usr/src/things all