๐ข Run Container
docker run [image:version]
# -d: detached mode (background demon)
# -it: interactive mode (execute a shell)
# -p [HOST_PORT]:[DOCKER_PORT]: expose port
# --rm: remove after it exits
# --name [NAME]: name the container
docker stop [container]
Examples:
# ๐ณ Run 16.04 version of ubuntu, port mapped 3000 to 80 in detached mode.
docker run ubuntu:16.04 -d -p 3000:80
# ๐ณ Run latest version of node, executing bash shell. Remove the container after it exits.
docker run --rm -it node /bin/bash
# ๐ณ Run alpine based node, executing sh shell, named "alpine-node-test"
docker run -it node:alpine /bin/sh --name alpine-node-test
/bin/sh
: Alpine Linux has no bash shell. Instead, use "/bin/sh" to execute shell.
Execute Existing Container
docker ps -a
docker run [CONTAINER ID or NAME]
docker exec -it [CONTAINER ID or NAME]
Examples:
# ubuntu container name: frosty_pascal
docker run frosty_pascal
docker exec -it frosty_pascal /bin/bash
๐ณ List
docker ps # List the running containers
docker ps -a # List all containers
docker images # List all images
-a
ย : for all. works even if your container is not running
-q
ย : for quiet. output only the ID
-f
ย : forย filter.
Filter Examples:
# ๐ฅขFilter by container name containing "SIMPLE_HELLO"
docker ps -aqf "name=SIMPLE_HELLO"
# 8dfd515ee14c
# ๐ฅขFilter by container name containing "MY_CONTAINER"
docker ps -aqf "name=MY_CONTAINER"
# 28903bfffc21
# bcad3db3806b
# 17df15565d55
# ๐ฅขFilter by container name only containing "MY_CONTAINER"
docker ps -aqf "name=^MY_CONTAINER$"
# 17df15565d55
๐ Build Images
docker build -t [IMG_NAME:TAG_NAME] [PROJECT_PATH]
โ Remove Containers & Images
docker rm [CONTAINER_ID]
docker rmi [IMAGE_ID]
docker rm $(docker ps -aq) # Remove all containers
docker rmi $(docker images -q) # Remove all images
docker rmi $(docker images -q) -f # โ ๏ธ force option if image has dependency
๐ Dockerfile
Create Project Dir
mkdir simple-docker && cd simple-docker
touch Dockerfile && vim Dockerfile
Write Dockerfile
FROM node:10-alpine
WORKDIR /app
CMD ["echo", "Hello, world!"]
Build Image & Run Container
docker build -t myrepo:mytag .
docker images
docker run myrepo:mytag
ํน์ VSCODE๋ฅผ ์ฌ์ฉํ์ ๋ค๋ฉด, Docker extension์ ์ถ์ฒํด๋๋ฆฝ๋๋ค.
์ฐธ๊ณ (https://code.visualstudio.com/docs/azure/docker)