---
# System prepended metadata

title: Basic Docker operation
tags: [Infra]

---

# Basic Docker operation
###### tags: `Infra`
docker version
docker info
docker pull nginx
docker images
### RUN/CREATE/START
docker create -it --name {name} busybox
docker start {name}
docker stop {id}
docker run -it --name c2 ubuntu bash　直接進去bash
進去:
exit, container 隨之結束
ctrl+p ctrl+q 出來
hostname
note: onedash 單字母 twodash 多字母
### attach/exec/detach
- 進去馬上detach
docker run -itd --name c3 busybox
docker exec c3 hostname
docker exec c3 ls
- attach
docker attach c3 (進去)

### logs/inspect
docker run -itd --name ya busybox ping ptt.cc
docker logs -f ya
docker inspect ya
### stop/kill
docker stop c1
docker kill c1
stop 

### rm/rmi
docker ps -aq
docker rm -f `docker ps -aq`
docker rmi {imagename}

### pause/unpause/restart
docker pause c1
docker unpause c1
docker restart c1
docker rm -f c1

## Push to harbor
docker login {url}
docker tag {imagename} {url}
docker images
docker push {urlname}

#### it: 準備要操作他的
整理一下今天有使用到的 Docker 指令如下：
docker search 搜尋 Docker Image 有哪些的指令
docker pull 指 Docker Image 下載回 local
docker images 顯示出在 local 下載了哪些 Docker Image
docker run 使用 Docker Image 啟動執行 Docker Container
docker ps 顯示了啟動了哪些的 Docker Container
### Search:
```
docker search ubuntu -f is-official=true
```
### Image pull:
```
docker pull ubuntu
```
### 確認image:
```
docker images
```
### 進入container
```
docker run -it ubuntu /bin/bash
```
### current status
```
docker ps -a
```
### Exit docker 
```
退出 docker container 的terminal 需要輸入ctrl + p之後再輸入 ctrl + q 的按鍵
```

## Dockerfile

以上的 Dockerfile 主要有用到的指令說明如下
FROM： 使用到的 Docker Image 名稱，今天使用 CentOS

MAINTAINER： 用來說明，撰寫和維護這個 Dockerfile 的人是誰，也可以給 E-mail的資訊

RUN： RUN 指令後面放 Linux 指令，用來執行安裝和設定這個 Image 需要的東西

ADD： 把 Local 的檔案複製到 Image 裡，如果是 tar.gz 檔複製進去 Image 時會順便自動解壓縮。Dockerfile 另外還有一個複製檔案的指令 COPY 未來還會再介紹

ENV： 用來設定環境變數

CMD： 在執行 docker run 的指令時會直接呼叫開啟 Tomcat Service
```dockerfile=
FROM centos:7
MAINTAINER jack

RUN yum install -y wget

RUN cd /

ADD jdk-8u152-linux-x64.tar.gz /

RUN wget http://apache.stu.edu.tw/tomcat/tomcat-7/v7.0.82/bin/apache-tomcat-7.0.82.tar.gz
RUN tar zxvf apache-tomcat-7.0.82.tar.gz

ENV JAVA_HOME=/jdk1.8.0_152
ENV PATH=$PATH:/jdk1.8.0_152/bin
CMD ["/apache-tomcat-7.0.82/bin/catalina.sh", "run"]
```

### 2. Build Docker Image
```shell=
docker build -t mytomcat . --no-cache
docker run mytomcat
```
IP:
docker exec 進入 docker container查詢 IP 
docker exec -it {container-id} /bin/bash
Port mapping:
```shell=
docker run -p 8080:8080 mytomcat
```

## Push to hub
```shell=
docker tag ${Image Name} DockerHub帳號/Image Name
docker login
docker push jackyohhub/mytomcat

docker rmi -f jackyohhub/mytomcat
docker rmi -f mytomcat

docker pull jackyohhub/mytomcat
```

![](https://i.imgur.com/0dikPRy.png)

### Use inspect to find container layers

docker inspect --format='{{json.GraphDriver}}' ub1 | jq

### deal data
- bind mount
![](https://i.imgur.com/WSQb4QM.png)
- docker volume
![](https://i.imgur.com/ZFfiOCU.png)

- names volume
docker volume create [vol_name]
- anonymous volume
docker run -mount

### Docker volume
- Create volume
    - `docker volume create [volume name]`
- List existing volumes
    - `docker volume ls`
- Mount colume to containers
    - `docker run --mount source[source_name], target=/target/path [image]`
- Remove volume
    - `docker colume rm [volume name]`

###  bind mounts
```shell=
mkdir source
docker run -it --rm --mount type=bind, source=`pwd`/source,target=/target busybox:latest
```
- In host: `watch ls source`
- In container: `cd /target; touch testfile; echo "test" > testfile`

## Image creation

### build image
- docker build [OPTIONS] PATH | URL | -
    - $ docker build -t test:latest -f Dockerfile
    - $ docker build -t test:latest .
#### build time cache
`docker build --no-cache `

### Dockerfile
- 1 instruction -> 1 layer
- describe how image looks like

- **FROM**: Specify the base image
    - `FROM <image>[:<tag>]`
- **RUN**: Execute commands in a new layer on top of current image and commit the results.
    - `RUN <command> [:arg]`
- **CMD**: Set the command to be executed in Container(Only last CMD will take effect)
- **ENTRYPOINT**: Configure a container as an 
![](https://i.imgur.com/716Q5wG.png)

- **EXPOSE**: Expose the specified network ports of a container at runtime
- **ENV**: Set environment variable for the running container
- **ADD**: Copy local files, folders or remote file URLs from source to the fs of the image at path

- **COPY**: versus to ADD

![](https://i.imgur.com/nloFp6s.png)

- **VOLUME**: Create anonymous volumes to target path when a container is created
`VOLUME ["<target path>]"`
- **USER**: Set the user name & group to use when running the image.
- **WORKDIR**: To folder,


docker run --rm --mount "type=bind,source=$(PWD),target=/myapp" -w "/myapp" gcc:4.9 gcc --static hello.c -o hello.out 


### COMMIT
docker commit --change 'CMD ["/hello.out"]' h1 hello:v2

Examine
docker inspect hello:v2
docker run -name h2 hello:v2

## Network
### create 
docker network create NETWORK

docker run -it --name apple busybox
### Connect
docker network connect data-newtwork apple

### init layer
- /etc/hosts
    - --add-host [hostname]