Ubuntu 使用 docker 筆記_Part2

使用 docker 前,建議可以將使用者加入 docker 群組,可以省去一直打 sudo 的麻煩。

一、安裝 docker

安裝指令

#Install docker
$curl -sSL https://get.docker.com/ | sudo sh

#Install docker-compose
$sudo curl -L "https://github.com/docker/compose/releases/download/1.28.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$sudo chmod +x /usr/local/bin/docker-compose
$sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
$docker-compose --version
docker-compose version 1.28.5, build 1110ad01

Use docker without sudo

reference: How can I use docker without sudo ?

# 1.Add the docker group if it doesn't already exist:
$sudo groupadd docker
groupadd: group 'docker' already exists

# 2.Add the connected user "[USER]" to the docker group.
# $sudo gpasswd -a [USER] docker
# Change the user name to match your preferred user if you do not want to use your current user:
$sudo gpasswd -a [USER] docker
Adding user [USER] to group docker.

$newgrp docker
$docker run hello-world

二、Use DockerFile example

1. vim DockerFile

這是一個要 build driver 的範例

# The container download from dockerhub https://hub.docker.com/_/ubuntu/
# which is official ubuntu dockerhub account
FROM ubuntu:18.04
#FROM ubuntu:14.04.5

MAINTAINER Yuyan "yuyan.tsai@advantech.com.tw"
LABEL Description="This dockerfile is ubuntu-VCOM building environment"
LABEL Vendor="Advantech"
LABEL Version="1.0"

VOLUME /root/shared_data

# Install libraries
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install vim -y
RUN apt-get install gcc -y
RUN apt-get install make -y
RUN apt-get install wget -y
RUN apt-get install build-essential -y
RUN apt-get install linux-headers-`uname -r` -y; exit 0
RUN apt-get install libssl-dev -y
RUN apt-get install dkms -y
# auto completion tool
RUN apt-get install bash-completion -y
# killall tool
RUN apt-get install psmisc -y
# ping tool
RUN apt-get install iputils-ping
# htop tool (optional)
RUN apt-get install htop -y

# add user (optional)
RUN apt-get -y install sudo
RUN useradd -m user && echo "user:user" | chpasswd && adduser user sudo

WORKDIR /root

note:

  • bash-completion 會有login logout 的問題哦,所以在執行docker 的時候要用 “bash l” 替代 單純的”/bin/bash”。當然也可以用另外的替代方案,進入後下 source /etc/profile.d/bash_completion.sh 讓此套件有效。

2. build container by using Dockerfile

#sudo docker build -t [main-resp]/[sub-resp]:[tag] ./
$docker build -t yuyan/build_env:ver1.0 ./

$docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
yuyan/build_env     ver1.0              496420cf7797        3 days ago          465MB
ubuntu              18.04               c3c304cb4f22        8 weeks ago         64.2MB
hello-world         latest              bf756fb1ae65        5 months ago        13.3kB
ubuntu              14.04.5             132b7427a3b4        3 years ago         188MB

3. run container

有兩種常用的用法,一種是用完即拋,在 docker run 的指令多補個 –rm,在離開 container 後就會,順便把 container 刪掉

# 簡易
$docker run -ti [main-resp]/[sub-resp]:[tag] bash -l
or
# 進階
$docker run -ti --rm --privileged -v [path_to_folder]:/root/shared_data [main-resp]/[sub-resp]:[tag] bash -l

-v: Bind mount a volume.
-ti: Allocate a pseudo-tty. +  Keep STDIN open even if not attached.
--rm: automatically clean up the container and remove the file system when the container exits.
--privileged: Give extended privileges to this container (enable access to host OS hardware. ex.insmod)
[path_to_folder]: Host OS folder path
[main-resp]/[sub-resp]:[tag] : named whatever ex. yuyan/build_env:ver1.0

三、常用指令

Run docker service

$service docker start

List the docker image

$docker images

Remove docker image

$docker rmi [image_id]

Remove all the docker images

$docker rmi -f $(docker images -q)

List the docker container (get container id)

$docker ps -a

Remove the docker container

$docker rm [container ID]

Remove all the docker container

$docker rm $(docker ps -aq)

Build docker image

$docker built -t [main-resp]/[sub-resp]:[tag] [PATH_TO_DOCKERFILE]

ex. docker build -t yuyan/build_env:ver1.0 ./

Run the docker image

$docker run -ti -v [PATH_TO_FOLDER]:/root/shared_data [main-resp]/[sub-resp]:[tag] /bin/bash

Save the docker image modification

$docker commit -m="COMMIT_MESSAGE" -a="AUTHOR" [container_id] [main-resp]/[sub-resp]:[tag]
ex. docker commit -m="Update 202006019" -a="yuyan" [container_id] yuyan/build_env:ver1.1

Restart the stopped docker container

$docker start -ai [container_id]
or
$docker start -ai [container_name]

List container

$docker ps -a
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS                      PORTS               NAMES
3307a856267e        yuyan/build_env:ver1.0   "bash -l"         3 days ago          Exited (0) 14 seconds ago                       trusting_hofstadter

Change image tag

# example "eki-1528dr/build_env:ver1.1" => "docker rmi eki-1528dr/build_env:1.1"

> docker image tag eki-1528dr/build_env:ver1.1 eki-1528dr/build_env:1.1

# remove old tag
> docker rmi eki-1528dr/build_env:ver1.1
Untagged: eki-1528dr/build_env:ver1.1

Change container name

# example "yenv-cns" => "ybak-cns"

> docker rename yenv-cns ybak-cns

clean docker disk usage

# remove unuse image
docker image prune -a

# remove docker build cache (14.6G)
docker builder prune

  目錄