How to use Docker

Start a container from an image

docker run -d -p 80:80 docker/getting-started

-d detached mode (terminal will not get stuck)

MVP

A TODO list manager.

Build image for repo

docker build -t my-start .

-t: human readable tag

(base) base ❯ docker build -t my-start .
Sending build context to Docker daemon   65.3MB
Step 1/5 : FROM node:12-alpine
 ---> 0206ff8a5f9e
Step 2/5 : WORKDIR /app
 ---> Using cache
 ---> 3b15d0c4afaf
Step 3/5 : COPY . .
 ---> 5dd7a446815f
Step 4/5 : RUN yarn install --production
 ---> Running in e455cbe79316
yarn install v1.22.5
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.2.9: The platform "linux" is incompatible with this module.
info "fsevents@1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 153.07s.
Removing intermediate container e455cbe79316
 ---> 08067c9dc3fb
Step 5/5 : CMD ["node", "src/index.js"]
 ---> Running in 7f6f53911c7d
Removing intermediate container 7f6f53911c7d
 ---> b99638721dc1
Successfully built b99638721dc1
Successfully tagged my-start:latest
docker run -dp 3000:3000 my-start

port mapping is useful to see the app in host.

Share imge

You should have an account in docker hub first.

docker tag getting-started YOUR-USER-NAME/getting-started

YOUR-USER-NAME: docker ID.

docker push YOUR-USER-NAME/getting-started

Container filesystem

Each container has its own scratch space. Any changes won’t be seen in another container, even if they are using the same image.

Operations like create, update and delete will be lost when the container is removed. Volumes are the filesystem to keep it!

Docker automatically create a space in disk. You can access it by the given name. But the mountpoint is hidden.

docker volume create todo-db
docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started

Tip:

docker volume inspect todo-db
[
    {
        "CreatedAt": "2019-09-26T02:18:36Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/todo-db/_data",
        "Name": "todo-db",
        "Options": {},
        "Scope": "local"
    }
]

Control the exact mountpoint on the host.

docker run -dp 3000:3000 \
    -w /app -v "$(pwd):/app" \
    node:12-alpine \
    sh -c "yarn install && yarn run dev"

-w: set working directory

-v: mount current directory in host to /app

Docker compose

https://docs.docker.com/compose/gettingstarted/

Tips

remove all live containers

docker rm -f $(docker ps -a -q)