DockerContainerCreate.js
4.26 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
import React, { useState, useEffect } from 'react'
import { useHistory, Redirect } from 'react-router-dom'
import {
Container,
Heading,
Button,
Segment,
Message,
Form,
Input,
Transition,
Select,
Checkbox,
FormControl,
FormLabel,
FormErrorMessage,
FormHelperText,
VStack,
Stack,
StackDivider,
Box,
Text, AlertDialogOverlay, AlertDialogContent, Alert, AlertIcon, AlertDialog,
} from '@chakra-ui/react'
import {
Divider,
} from 'semantic-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) {
setSuccess(true)
setTimeout(3000, () => {
history.push('/docker/container/list')
})
} else {
setError(message)
}
// 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/>
<Stack
divider={<Divider hidden/>}
spacing={4}
align="stretch"
>
<Heading>Create Docker Container</Heading>
<FormControl id="container" isRequired>
<FormLabel>Container Name</FormLabel>
<Input
type="email"
placeholder='Container Name'
/>
</FormControl>
<FormControl id="image">
<FormLabel>Image</FormLabel>
<Select placeholder="생성할 컨테이너의 OS를 설정해주세요">
<option value="ubuntu 18.04">Ubuntu 18.04.5 LTS</option>
<option value="ubuntu 20.04">Ubuntu 20.04.2 LTS</option>
</Select>
</FormControl>
<Button size="md" colorScheme="teal" mt="24px"
onClick={() => createContainer()}
>
Create Container
</Button>
</Stack>
<AlertDialog
isOpen={isSuccess}
onClose={() => setSuccess(false)}
>
<AlertDialogOverlay>
<AlertDialogContent>
<Alert status="success">
<AlertIcon />
Successfully created !
</Alert>
</AlertDialogContent>
</AlertDialogOverlay>
</AlertDialog>
</Container>
)
}
export default DockerContainerCreate