2018年8月19日 星期日

Docker - basic command

01. Install
   ref: HereInstall 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 a Image (ex: 16.04)
   $sudo docker pull ubuntu:16.04

04. List Container
       $ docker ps -a
   List Images
       $ docker image ls

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

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

07. Rename Container
       $ docker rename current_name new_name

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

09. Copy a file from hostPC to docker
       $ docker cp source_file C_ID:/home/user_name
       (like scp command)
   Copy a file from docker to hostPC
       $ docker cp C_ID:/home/user_name/123.txt ./
       (like scp command) 
 
10. 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

Dockerfile
FROM ubuntu:16.04

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=image_name:tag_name . --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


沒有留言:

張貼留言