Docker-compose 4

在[docker-compose-3]中完成一個新的image建立


$ docker images 
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE 
code_icdc latest 220f9d464171 17 hours ago 304.4 MB

可以更改image tag


$ docker tag code_icdc:latest code_icdc:v.01
$ docker images 
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
code_icdc latest 220f9d464171 17 hours ago 304.4 MB
code_icdc v.01 220f9d464171 17 hours ago 304.4 MB

接著,

可以利用dockerfile和compose 使這個新建的image code_icdc 複製產生多組同樣功能images…

準備一個dockerfile

Dockerfile2


FROM code_icdc:v.01
MAINTAINER Jeff Yang
ENV IPMI_GET_IP_S 1
ENV IPMI_GET_IP_E 10

這裡設定一組Environment Variables,來測試複製的image

docker-image.yml


build:
 container_name: envTest
 build: ./
 dockerfile: Dockerfile2
 ports: 
     - 9083:9080
 volumes:
     - /home/jeff/code/test_dockerEnv:/home/code/test_dockerEnv
 restart: always 
 stdin_open: true
 tty: true

$ docker-compose up -d -f docker-image.yml

完成image後,檢視現在多了一個新的testdockerenv_build image


$ docker images 
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
testdockerenv_build latest 48e05900d52b 7 seconds ago 304.4 MB
code_icdc latest 220f9d464171 17 hours ago 304.4 MB
code_icdc v.01 220f9d464171 17 hours ago 304.4 MB

測試程式,簡單的印出環境變數

goEnv.go

1
2
3
4
5
6
7
8
9
10
11
12
13
package main
import (
"fmt"
"os"
"bufio"
)
func main() {
fmt.Println("IPMI_GET_IP_S:", os.Getenv("IPMI_GET_IP_S"))
fmt.Println("IPMI_GET_IP_E:", os.Getenv("IPMI_GET_IP_E"))
//getchar()
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
}

接著,進入container


$ docker exec -it envTest bash

查看supervisord是否有啟動


[root@a6d077cfdd7c home]# supervisorctl status
ipmi RUNNING pid 9, uptime 0:00:42
ipmi_monitor RUNNING pid 22, uptime 0:00:36
ipmicdc:0 STOPPED Not started
ipmicdc:1 STOPPED Not started
ipmicdc:2 STOPPED Not started
ipmicdc:3 STOPPED Not started
ipmicdc:4 STOPPED Not started

執行程式,將印出環境變數


[root@a6d077cfdd7c home]# cd ./code/test_dockerEnv/
[root@a6d077cfdd7c test_dockerEnv]# ./goEnv
IPMI_GET_IP_S: 1
IPMI_GET_IP_E: 10
[root@a6d077cfdd7c test_dockerEnv]# echo $IPMI_GET_IP_E
10

若不需要產生新image,

也可以直接執行docker run,利用現有image產生container


$ docker run -e "IPMI_GET_IP_S=1" -e "IPMI_GET_IP_E=10" -itd --name envTest --restart=always -p 9083:9080 -v /home/jeff/code/test_dockerEnv:/home/code/test_dockerEnv code_icdc:v.01 
90cd3ba79bc4a9b705c823e0ba488e04ad7efc07c7181ef6b28cd14e5dc1d31c

$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
code_icdc latest 220f9d464171 18 hours ago 304.4 MB
code_icdc v.01 220f9d464171 18 hours ago 304.4 MB
docker.io/centos centos7 60e65a8e4030 13 days ago 196.6 MB
docker.io/centos latest 60e65a8e4030 13 days ago 196.6 MB

相同步驟確認環境變數


$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90cd3ba79bc4 code_icdc:v.01 "/bin/sh -c $MAJOR_PA" 17 seconds ago Up 16 seconds 0.0.0.0:9083->9080/tcp envTest

$ docker exec -it envTest bash
[root@90cd3ba79bc4 /]# cd /home/code/test_dockerEnv/
[root@90cd3ba79bc4 test_dockerEnv]# ./goEnv
IPMI_GET_IP_S: 1
IPMI_GET_IP_E: 10