๐Ÿณ Docker Cheat Sheet

godoriยท2019๋…„ 4์›” 28์ผ
10
post-thumbnail

๐Ÿšข 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

# ๐Ÿ‹ 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
profile
Love ๐ŸŽง Play ๐ŸŽฎ Drink ๐Ÿบ

6๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2019๋…„ 4์›” 28์ผ

ํ˜น์‹œ VSCODE๋ฅผ ์‚ฌ์šฉํ•˜์‹ ๋‹ค๋ฉด, Docker extension์„ ์ถ”์ฒœํ•ด๋“œ๋ฆฝ๋‹ˆ๋‹ค.
์ฐธ๊ณ  (https://code.visualstudio.com/docs/azure/docker)

1๊ฐœ์˜ ๋‹ต๊ธ€
comment-user-thumbnail
2019๋…„ 4์›” 28์ผ

๋‹ค์Œ์€ Docker Compose Cheat Sheet ๋„ ์ข€ ์จ์ฃผ์„ธ์š” (๋ป”๋ป”)

3๊ฐœ์˜ ๋‹ต๊ธ€