Some Useful Docker Commands

Overview

The blog collects some useful Docker commands.

Basic

Concept of Docker container and Docker image:

  • Docker container is a running instance of a Docker image, providing an isolated environment that can be started, stopped and detelted.
  • Docker image is a static, read-only file containing everything needed to create containers. It serves as the source file for starting a Docker container.

Check Docker Username

Check the username and registry of the logged in Docker account.

1docker system info | grep -E 'Username|Registry'

Docker Registry

If have multiple Docker registries configured, you can check the authentication details stored in your Docker configuration file.

1cat ~/.docker/config.json  # On Unix-based systems

To login or logout a docker registry:

1docker login [REGISTRY]
2docker logout [REGISTRY]
3gcloud auth configure-docker [gcloud-registry] # If it is Gcloud registry

Launch containers

1# Start containers in `docker-compose.yml` file (with -d for detach mode)
2docker-compose up
3# Stop containers in `docker-compose.yml` file
4docker-compose down

Enter a running docker

1docker exec -it <container_id> /bin/bash

Copy files from docker to external path

1docker cp <container_id>:/path/to/your/file /path/on/host/system/

Image Operation

1# Pull a docker by its registry
2docker pull <image-name>

Remove Image

1# List locally available Docker images (IMAGE ID)
2docker images
3# Delete a Docker image locally by image ID
4docker rmi <image-id>

To delete all of them from system, use below command:

1docker rmi $(docker images -q)

Remove multiple tags iamge

Remove multiple-tag images from one repository.

If you want to remove all images from a specific repository with different tags, use the following script.
Firstly use docker images to review the ones want to remove. The output is like below:

1REPOSITORY              TAG       IMAGE ID         CREATED      SIZE
2registry-A             0.0.1        xxxx        25 hours ago    7.5GB
3registry-A             0.0.2        xxxx        25 hours ago    7.5GB
4registry-A             0.1.0        xxxx        25 hours ago    7.5GB
5...              ...          ...          ...            ...

In the case, all images from repository registry-A need to be removed. Save below script to a file e.g. remove_tags.sh.

 1 #!/bin/bash
 2
 3 read -p "Enter the name of the repository: " REPOSITORY
 4
 5 # Get all tags for the repository (exclude headers)
 6 tags=$(docker images --format "{{.Tag}}" $REPOSITORY)
 7
 8 # Loop through each tag and remove the corresponding image
 9 for tag in $tags; do
10     docker rmi $REPOSITORY:$tag
11     echo "$REPOSITORY:$tag removed"
12 done

To use it, run bash remove_tags.sh in Terminal, then provide the repository name when prompted. In the example case is registry-A. This script will remove all tags from the specified repository one by one.

Save Image to .tar File

Firstly use docker images to review the image you want to save, and then use following command to save:

1docker save -o /path/to/<filename>.tar image-id

The image-id can be replaced by REPOSITORY:TAG format. For a clearer filename, recommend naming it use a REPOSITORY:TAG format.

Load a Docker image from a .tar file.

1docker load -i /path/to/filename.tar

Retag Docker Image

1docker tag <source-repository:tag> <new-repository:tag>

Container Operation

Basic commands

1# List currently running Docker containers (CONTAINER ID)
2docker ps
3# List currently running Docker containers including stopped ones
4docker ps -a
5# View the logs of a specific container
6docker logs <container-id>
7# Stop a running container
8docker stop <container-id>

Enter a Docker with interactive mode

1docker run -it your_image_name /bin/bash

Stop All Running Containers

It is quite common to have a couple of containers running in the background. Sometimes, it is hardly to be aware of them if you don't check. To stop them all at once, use the following command. Consider adding this to an alias for convenience.

1docker stop $(docker ps -a -q)

Remove Containers

1# Remove one container by name
2docker rm container_name
3# Remove all containers
4docker rm $(docker ps -a -q)

Docker-Compose File Example

Launch ROS1 roscore and play a local rosbag in a ROS2 system envrionment.

 1version: '3.3'
 2
 3services:
 4  roscore:
 5    image: ros:noetic-ros-core
 6    container_name: roscore_container
 7    restart: unless-stopped
 8    network_mode: host
 9    command: >
10      /bin/bash -c "
11        source /opt/ros/noetic/setup.bash;
12        roscore
13      "      
14
15  bag:
16    image: ros:noetic-ros-core
17    container_name: bag_container
18    restart: unless-stopped
19    network_mode: host
20    command: >
21      /bin/bash -c "
22        source /opt/ros/noetic/setup.bash;
23        rosbag play /launch/**.bag
24      "      
25    volumes:
26      - /home/data:/launch

comments powered by Disqus