Docker 基础介绍

Docker安装、快速使用、常用命令、VScode配置

Docker Install

  1. WSL2
  2. Ubuntu

Attension

  1. 配置需要替换 http://proxy.example.com:8080/ 为本地代理,例如 http://127.0.0.1:7890/
  2. 避免每次使用时都要使用 sudo 权限,可创建docker用户组

Start-Up

使用 Docker 有两种思路

  • 将 Docker 作为虚拟机使用,开发环境与代码均保存在 image 中
  • 将开发环境保存在 image 中,代码保存在宿主机中,通过 volume 挂载
  1. 创建新的容器并进入
1
2
3
4
5
6
7
8
9
10
11
## pull image (user/name:version) -- from official: ubuntu:24.04
dcoker pull ubuntu:24.04
docker images
## i: interactive
## t: terminal / pseudo-tty
## d: detached
## --name: container name
docker run -itd --name container_name ubuntu:24.04
docker ps -a
docker exec -it container_id /bin/bash
docker run -itd imgae_name
  1. 安装编译环境
1
2
3
apt update
apt install -y build-essential
apt install gfortran vim wget
  1. 拷贝文件并安装
1
2
3
4
5
6
## user terminal
docker cp ~/download/cmake-3.31.3.tar.gz my_container:/home/download
## container terminal
apt install libssl-dev
tar -zxvf cmake-3.31.3.tar.gz
./bootstarp && make
  1. 打包环境并加载
1
2
3
4
5
6
## commit
docker commit container_id my_image:my_tag
docker save -o my_image.tar.gz my_image:my_tag
## load
docker load -i my_image.tar.gz
docker run -itd --name my_image_2 my_image:my_tag
  1. 退出容器
1
exit

Command

查看帮助 docker [COMMAND] --help

Docker Container

以下是container常用命令

Command Description
docker ps -a list container: running/exited…
docker inspect <cid> list all information
docker run -itd --name <cname> <iname> create and run container from an image
docker run -p 8080:80 <iname> publish container port to host
docker run -v /host:/cont <iname> bind mount a volume
docker run --rm -v /host:/cont <iname> remove container after stop
docker exec -it <cid> /bin/bash execute in a running container
docker stop <cid> stop running container
docker restart <cid> restart container
docker rm <cid> remove container
docker kill <cid> kill running container
docker start <cid> start container
docker pause <cid> pause all processes within container
docker top <cid> display running processes of container
docker port <cid> list port map
docker cp src <cid>:dest copy files/folders (host => container)
docker cp <cid>:src dest copy files/folders (container => host)
docker create --name <cname> <iname> create container from an image
docker export <cname> -o cname.tar export container to tar file
docker rename <cname> <new_cname> rename container
docker container list -a docker ps -a

NOTE

  1. 容器映射端口或挂载卷必须在容器启动时指定;
  2. docker container 可进行命令筛选;
  3. createrun 均可以基于image创建一个新的容器,create 的容器需要通过 start 命令启动;

Docker Image

以下是 image 常用命令

Command Description
docker images list images
docker inspect <iid> list all information
docker rmi <iid> remove image
docker tag iname:tag iname:new_tag create tag
docker pull <repository>/<iname>:<tag> pull image from Docker Hub
docker push <repository>/<iname>:<tag> upload image to Docker Hub
docker search <iname> search image in Docker Hub
docker commit <cid> <iname> create a image from container
docker save -o iname.tar <iname> save image to tar archive
docker load -i iname.tar load image from tar archive
docker image list docker images

NOTE

  1. docker image 可进行命令筛选;
  2. docker tag 创建标签后会新生成一个 image;

Dockerfile

VSCode

  1. 安装 Dev - Containers 插件
  2. Remote Explorer 中选择 Dev Containers 而非 WSL Targets
  3. 选择 Attach ... 进入 Docker 容器并选择路径

Reference

0%