DockerContainerCreate.js
5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import React, { useState, useEffect } from 'react'
import { useHistory, Redirect } from 'react-router-dom'
import {
Container,
Heading,
Button,
Segment,
Message,
Divider,
Form,
Input,
Transition,
Select,
Checkbox,
FormControl,
FormLabel,
FormErrorMessage,
FormHelperText,
} from '@chakra-ui/react'
import http from '../utils/http'
const DockerContainerCreate = () => {
const [images, setImages] = useState([])
const [image, setImage] = useState('')
const [containerName, setContainerName] = useState('')
const [ssh, setSsh] = useState('')
const [port, setPort] = useState(0)
const [randomPort, setRandomPort] = useState(false)
const [isSuccess, setSuccess] = useState(false)
const [isLoading, setLoading] = useState(false)
const [error, setError] = useState('')
const history = useHistory()
const loadImages = () => {
setLoading(true)
http.get('/docker/image/list').then(response => {
const { status, message } = response.data
const images = response.data.data
setLoading(false)
if (status) {
setImages(images)
} else {
setError(message)
console.log('[ ERROR ]', message)
}
}).catch(error => {
console.error(error)
})
}
const getImageDropdownOptions = () => {
const defaultImage = {
key: 999,
value: 'default',
text: 'default image',
}
return [defaultImage].concat(images.map((image, key) => {
return {
key: key,
value: image.Id,
text: image.RepoTags.length > 0 ? image.RepoTags[0] : ' - ',
}
}))
}
const createContainer = () => {
setLoading(true)
setError('')
console.log('[ DEBUG ] image :', image)
http.post('/docker/container/create', {
name: containerName,
image: image,
port: port,
random_port: randomPort,
}).then(response => {
const { status, message } = response.data
const { username, password, port } = response.data.data
setLoading(false)
if (!status) {
setError(message)
} else {
setSuccess(true)
setSsh(
`ssh ${username}@${window.location.hostname} -p ${port} # (password : ${password})`)
}
// setTimeout(() => setSuccess(false), 10000);
}).catch(error => {
console.error('[ ERROR ]', error)
})
}
const handleImage = (e, data) => {
setImage(data.value)
}
const handleContainerName = (e) => {
setContainerName(e.target.value)
}
const handlePort = (e) => {
setPort(e.target.value)
}
const handleRandomPort = () => {
setRandomPort(!randomPort)
}
useEffect(() => {
loadImages()
}, [])
return (
<Container>
<Divider hidden/>
<Segment raised>
<Heading size='huge'>Create your own docker container</Heading>
<Divider hidden/>
<FormControl id="container">
<FormLabel>Container Name</FormLabel>
<Input
type="email"
placeholder='Container Name'
/>
<FormHelperText>컨테이너 이름을 입력해주세요.</FormHelperText>
</FormControl>
<div>
<Input
label='Container Name'
placeholder='Container Name'
onChange={handleContainerName}
/>
<Select label='Image'>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</Select>
{/*<Form.Group>
<Form.Dropdown
label='Image'
selection
options={getImageDropdownOptions()}
defaultValue={getImageDropdownOptions()[0]}
onChange={handleImage}
/>
</Form.Group>*/}
<Input
type='number'
label='SSH Port'
placeholder='22'
disabled={randomPort}
onChange={handlePort}
/>
{/*<Form.Group>
<Form.Input
type='number'
label='SSH Port'
placeholder='22'
disabled={randomPort}
onChange={handlePort}
/>
</Form.Group>*/}
<Checkbox
label='Use random port'
checked={randomPort}
onChange={handleRandomPort}
/>
<Button
color='violet'
onClick={createContainer}
content='Create'
/>
<Button
content='Container List'
onClick={() => history.push('/docker/container/')}
/>
</div>
{/*
<Transition.Group
duration={300}
>
{isSuccess && (
<>
<Divider/>
<Message
success
content={ssh}
/>
</>
)}
{error && (
<>
<Divider/>
<Message
error
content={error}
/>
</>
)}
</Transition.Group>*/}
</Segment>
</Container>
)
}
export default DockerContainerCreate