Creation of Docker Image
manage docker images
Image management#
What is docker image?#
Listing images#
- Use
docker images
command
Repository
means a storage for a docker image.
- Usually, we call the string after
/
of repository to the name of an image.
- An image means a single template formed by hash.
REPOSITORY |
TAG |
IMAGE |
ID |
CREATED |
SIZE |
ubuntu |
latest |
adafef2e596e |
7 |
months ago |
73.9MB |
ubuntu |
18.04 |
d27b9ffc5667 |
7 |
months ago |
64.2MB |
mongo |
4.2.3 |
bcef5fd2979d |
11 |
months ago |
386MB |
Pruning docker images#
- Sometime, we want to remove all unused images due to lack of storage.
prune
deletes all images not to be containerized.
- You can remove all volumes not to be mapped to any container.
- Remove all volumes not to be mounted
- Similarly, you can prune unused containers by using
container
.
DONE Modification of docker image#
- Life cycle of docker image
- Creating docker image.
- Executing some changes in the container.
- Committing the changes of container to the image.
- Pushing the image to the repository.
Creating your own docker image#
Creating base container#
- Use
archlinux
image as a base container.
docker run -it --name archlinux archlinux
Changing some contents in the container#
echo "new file" >> /root/readme.md
exit
Committing and push the image#
- You can use
commit
sub-command to store your change in a image.
docker commit -m "{{message}}" {{container-name}} {{image-repository}}:{{tag-name}}
docker commit -m "my own image" archlinux hackartist/my-own-image:latest
images
sub-command help us to list images.
REPOSITORY |
TAG |
IMAGE |
ID |
CREATED |
SIZE |
hackartist/my-own-image |
latest |
3b7256abb727 |
8 |
minutes ago |
406MB |
hackartist/docker-dns |
latest |
a006dfefbbbf |
7 |
months ago |
422MB |
run
하위명령어를 통해서 컨테이너 생성 후 확인
docker run -it --name myarch hackartist/my-own-image:latest
cat /root/readme.md
DONE Building docker image#
간단한 예제 코드 개발#
go
개발 언어로 헬로우 월드를 개발
- 상세 내용은 개발 섹션을 참고
- 파일 이름은
main.go
로 지정
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
예제 코드 실행#
go mod init {{package name}}
은 의존성 관리 및 GOPATH
가 아닌 곳에서 빌드를 위한 프로젝트 관리
go build
를 통해서 해당 프로그램을 빌드
go mod init github.com/hackartists/dockerize
go build -o app
./app
Dockerfile 활용#
docker build
를 통해서 이미지 생성
-f
옵션으로 Dockerfile
이름 설정
./Dockerfile
은 기본 설정이라서 생략 가능
-t
옵션으로 레포지토리 및 태그 이름을 지정
.
은 도커 컨텍스트 디렉토리를 의미
- 지정된 디렉토리 이하로 모든 파일을 도커 빌드를 위해서 로딩함.
.dockerignore
를 통해서 불필요한 디렉토리를 제외할 수 있음.
docker build -f Dockerfile -t hackartist/go-hello-world .
Dockerfile 스크립트#
- 빌드를 위한 최초 이미지 선택
- 위의 예제는
go
언어를 사용하기 때문에 golang:latest
이미지를 활용
- 빌드 커맨드 수행
FROM golang:latest
ADD . /workdir
WORKDIR /workdir
RUN go build -o app
CMD ["/workdir/app"]
도커 업로드(push) 하기#
- 생성한 도커 이미지를 해당 레포지토리에 업로드
- 도메인 이름이 없는 경우 자동으로
docker.io
가 추가됨.
docker push hackartist/go-hello-world