修改源为国内源:
{
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
获取镜像:
sudo docker pull ubuntu:22.04
22.04: Pulling from library/ubuntu
37aaf24cf781: Pull complete
Digest: sha256:9b8dec3bf938bc80fbe758d856e96fdfab5f56c39d44b0cff351e847bb1b01ea
Status: Downloaded newer image for ubuntu:22.04
docker.io/library/ubuntu:22.04
运行镜像:
-it
:这是两个参数,一个是-i
:交互式操作,一个是-t
终端。我们这里打算进入bash
执行一些命令并查看返回结果,因此我们需要交互式终端。--rm
:这个参数是说容器退出后随之将其删除。默认情况下,为了排障需求,退出的容器并不会立即删除,除非手动docker rm
。我们这里只是随便执行个命令,看看结果,不需要排障和保留结果,因此使用--rm
可以避免浪费空间。
sudo docker run -it --rm ubuntu:22.04 bash
root@95f006668b0c:/# cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.3 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
罗列本地镜像:
sudo docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 22.04 c6b84b685f35 7 weeks ago 77.8MB
ubuntu latest c6b84b685f35 7 weeks ago 77.8MB
hello-world latest 9c7a54a9a43c 5 months ago 13.3kB
centos latest 5d0da3dc9764 2 years ago 231MB
使用 Dockerfile 定制镜像:
FROM ubuntu
MAINTAINER yuanjc
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN apt-get update
RUN apt-get install -y vim
RUN apt-get install -y net-tools
RUN apt-get install -y iputils-ping
EXPOSE 80
CMD /bin/bash
sudo docker build -f Dockerfile -t tierenubuntu:1.0.0 .
[+] Building 52.8s (10/10) FINISHED docker:default
=> [internal] load .dockerignore 0.7s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.9s
=> => transferring dockerfile: 254B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:latest 0.0s
=> [1/6] FROM docker.io/library/ubuntu 0.2s
=> [2/6] WORKDIR /usr/local 0.7s
=> [3/6] RUN apt-get update 14.0s
=> [4/6] RUN apt-get install -y vim 22.9s
=> [5/6] RUN apt-get install -y net-tools 5.0s
=> [6/6] RUN apt-get install -y iputils-ping 6.1s
=> exporting to image 1.7s
=> => exporting layers 1.6s
=> => writing image sha256:c94620ba610324dd6a001d21d524ae8f0de9942079de4d147bbf9f34a4c73796 0.0s
=> => naming to docker.io/library/tierenubuntu:1.0.0 0.1s
当尝试使用-v 参数进行挂载目录的时候:如下有两个目录同时挂载的时候,
当尝试使用-v 参数进行挂载目录的时候:如下有两个目录同时挂载的时候docker run -it -v $path_to_host_data:$new_path_in_docker $path1_to_host_data:$new_path1_in_docker deep_sleepnet:1.0 /bin/bash
出现如下错误,docker: invalid reference format: repository name must be lowercase.
查看了半天,发现是忘记加 -v参数了(🤦♂️)修改后如下:docker run -it -v $path_to_host_data:$new_path_in_docker -v $path1_to_host_data:$new_path1_in_docker deep_sleepnet:1.0 /bin/bash
这里也提醒一下自己,注意docker镜像命名规范(必须为小写)