Frequently used Docker commands on Ubuntu

Basic Docker Commands

  • docker version

    • Displays Docker version information.
    bash 复制代码
    docker version
  • docker info

    • Provides more detailed information about the Docker installation.
    bash 复制代码
    docker info
  • docker run

    • Runs a command in a new container.
    bash 复制代码
    docker run hello-world
  • docker ps

    • Lists running containers. Use -a to list all containers (running and stopped).
    bash 复制代码
    docker ps
    docker ps -a
  • docker stop

    • Stops one or more running containers.
    bash 复制代码
    docker stop <container_id_or_name>
  • docker start

    • Starts one or more stopped containers.
    bash 复制代码
    docker start <container_id_or_name>
  • docker restart

    • Restarts a running container.
    bash 复制代码
    docker restart <container_id_or_name>
  • docker rm

    • Removes one or more containers.
    bash 复制代码
    docker rm <container_id_or_name>
  • docker rmi

    • Removes one or more images.
    bash 复制代码
    docker rmi <image_id_or_name>

Image Management

  • docker images

    • Lists the Docker images available locally.
    bash 复制代码
    docker images
  • docker pull

    • Pulls an image or a repository from a registry.
    bash 复制代码
    docker pull ubuntu
  • docker build

    • Builds Docker images from a Dockerfile.
    bash 复制代码
    docker build -t myimage .

Network Management

  • docker network ls

    • Lists networks.
    bash 复制代码
    docker network ls
  • docker network create

    • Creates a new network.
    bash 复制代码
    docker network create my-network
  • docker network rm

    • Removes one or more networks.
    bash 复制代码
    docker network rm my-network

Docker Compose Commands

  • docker-compose up

    • Builds, (re)creates, starts, and attaches to containers for a service.
    bash 复制代码
    docker-compose up
  • docker-compose down

    • Stops and removes containers, networks, volumes, and images created by up.
    bash 复制代码
    docker-compose down

These commands are quite common for daily use in Docker environments and are essential for managing Docker containers and images effectively. Remember to replace placeholders (like <container_id_or_name> or <image_id_or_name>) with actual values from your Docker environment.

With GPU

When you have already created and started a Docker container with NVIDIA GPU support, using it through a terminal involves a similar process to accessing any Docker container, as described previously. The difference lies in ensuring that the container was properly set up to use the NVIDIA GPU, which involves having the appropriate NVIDIA Docker configurations.

Below are detailed steps on how to access and use your NVIDIA GPU-enabled Docker container from a terminal:

1. Verify GPU Access in the Container

Before diving into accessing the container, it's useful to first confirm that your container has access to the GPU. You can check this by running a command like nvidia-smi inside the container:

bash 复制代码
docker exec -it <container_name_or_id> nvidia-smi

This command should output information about the GPU, indicating that the container has access to it. If it does, you can proceed to interact with the container normally.

2. Accessing the Container

To access the container, you use the docker exec command to start an interactive shell session:

bash 复制代码
docker exec -it <container_name_or_id> /bin/bash

Replace <container_name_or_id> with the actual name or ID of your container. You can find this by listing all running containers with docker ps.

3. Running GPU-Accelerated Programs

Inside the container, you can execute any installed GPU-accelerated programs. For example, if you have TensorFlow installed in a container configured for GPU, you can start a Python session and import TensorFlow to verify it recognizes the GPU:

python 复制代码
import tensorflow as tf
print(tf.config.list_physical_devices('GPU'))

This Python code should list the available GPUs if TensorFlow is set up correctly to use the GPU.

4. Exiting the Container

To exit the container terminal without stopping the container, you can simply type exit or press Ctrl-D.

Example Session

Here's a quick recap of how the flow might look:

  1. List Containers (to find your specific container):

    bash 复制代码
    docker ps
  2. Check GPU Access (using nvidia-smi):

    bash 复制代码
    docker exec -it my_gpu_container nvidia-smi
  3. Access the Container:

    bash 复制代码
    docker exec -it my_gpu_container /bin/bash
  4. Run Python and Check TensorFlow GPU (inside the container):

    bash 复制代码
    python
    >>> import tensorflow as tf
    >>> print(tf.config.list_physical_devices('GPU'))
  5. Exit When Done:

    bash 复制代码
    exit

Troubleshooting

If the nvidia-smi command does not show the GPUs or if TensorFlow does not recognize the GPU, ensure that:

  • Your container was started with the --gpus all flag or similar GPU specification.
  • The NVIDIA Docker runtime is correctly installed and configured on your host system.
  • The Docker image you are using is CUDA-capable and has the necessary NVIDIA libraries.

By following these steps, you can effectively use and interact with your NVIDIA GPU-accelerated Docker container from the terminal.

相关推荐
淡水猫.2 分钟前
Fakelocation Server服务器/专业版 ubuntu
运维·服务器·ubuntu
yunfanleo2 小时前
docker run m3e 配置网络,自动重启,GPU等 配置渠道要点
linux·运维·docker
ac.char3 小时前
在 Ubuntu 上安装 Yarn 环境
linux·运维·服务器·ubuntu
梅见十柒3 小时前
wsl2中kali linux下的docker使用教程(教程总结)
linux·经验分享·docker·云原生
入 梦皆星河4 小时前
在 Ubuntu/Debian 上安装 Go
ubuntu·golang·debian
IT果果日记4 小时前
ubuntu 安装 conda
linux·ubuntu·conda
Python私教4 小时前
ubuntu搭建k8s环境详细教程
linux·ubuntu·kubernetes
梓仁沐白9 小时前
ubuntu+windows双系统切换后蓝牙设备无法连接
windows·ubuntu
意疏17 小时前
【Linux 篇】Docker 的容器之海与镜像之岛:于 Linux 系统内探索容器化的奇妙航行
linux·docker
墨鸦_Cormorant17 小时前
使用docker快速部署Nginx、Redis、MySQL、Tomcat以及制作镜像
redis·nginx·docker