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

๐Ÿณ 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:

image.png

# ๐Ÿฅข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

asciicast

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