2018年8月19日 星期日

Docker - basic command


Dockerfile, tag
FROM ubuntu:amd64/ubuntu:24.10 //先確認tag存在

RUN apt-get update
RUN apt-get -y install vim
RUN apt-get -y install sudo
RUN apt-get -y install locales

# Set the locale
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

RUN useradd -G sudo -u 1000 --create-home ubuntu
RUN echo "ubuntu:ubuntu" | chpasswd
RUN echo "root:ubuntu" | chpasswd

ENV HOME /home/ubuntu
WORKDIR /home/ubuntu
$ docker build -t=amd64/ubuntu:24.10 --no-cache


run.sh
#!/bin/bash

xhost +local:
docker run -it  \
        --privileged \
        -v /tmp/.X11-unix:/tmp/.X11-unix \
        -v $HOME:/home/ubuntu/host_home \  (-v host:container, 如果Dockerfile有用cmd,cmd的路徑必需在host有一份)
        -v /home/ubuntu:/home/ubuntu/ \ (共用家目錄)
        -e DISPLAY=$DISPLAY \
        --user 1000 \
        image_name:tag_name

xhost -local:
Parameter:
a. --privileged: use the option to docker run. This removes most of the restrictions normally placed on a Docker container
b. -v: host file path mapping to docker container



01. Install
ref: Here - Install Docker CE with Ubuntu/Mint
( If the installation is successful, the command of ifconfig will show docker0 )
$ run docker command without sudo:
$ sudo groupadd docker
$ sudo gpasswd -a $USER docker


(only for myself)
$ sudo mv /var/lib/docker/ /home/$USER/Docker
$ sudo ln -s /home/$USER/Docker /var/lib/docker

reboot

02. Remember to install QEMU package if wanting to install ARM Ubuntu/Linux
$ sudo apt-get install qemu-user-static

03. Get an Image (ex: amd64/ubuntu:24.10)
$sudo docker pull amd64/ubuntu:24.10

04. List the Image and the Container
Container:
    $ docker ps -a
    
List Images
    $ docker image ls

05. Run
a. create a new container or refer to the bellow Dockerfile
       $sudo docker run -t -i ubuntu:16.04
b. Enter container
       $ sudo docker start C_ID (ContainerID)
       $ sudo docker attach C_ID (ContainerID)
       
c. Enter container (Run at least once)
       $sudo docker exec -i -t C_ID /bin/bash


06. Delete
a. Delete Container
       $ docker stop C_ID || true && docker rm C_ID || true
       (C_ID = Container ID)
b. Delete Image
       $ docker rmi image_ID

07. Rename
a. Rename Container
    $ docker rename current_name new_name

b. Rename Image/tag
    $ docker tag current_name:curretn_tag_name new_name:new_tag

08. Copy
a. Copy a file from hostPC to docker
$ docker cp source_file C_ID:/home/user_name
       (like scp command)
       
b. Copy a file from docker to hostPC
       $ docker cp C_ID:/home/user_name/123.txt ./
       (like scp command)

09. Find the image ID and parent ID (dependency)
$ docker inspect --format='{{.Id}} {{.Parent}}' $(docker images --filter since=Image_ID -q)
       or
$ docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=Image_ID -q)
$ docker rmi {parent_id}
       or
$ docker rm REPOSITORY:TAG

ref:
1. iT邦
2. Mount linux image in docker container
3. 清風軟件測試

沒有留言:

張貼留言