Docker Guide

Key Concepts

  • An image is a blueprint to be used for creating container instances. Images are NOT ran directly, they’re only used for creating containers.

  • A container is an instance of an image. Removing a container removes the instance only, but keeps the image available for future creation of new containers.

  • Docker VS Docker-Compose

  • Docker --volume vs --mount

    • --mount type=bind,source="$(pwd)",target=/app : binds a local directory to the target location inside the container.

    • --mount type=volume,source="$(pwd)",target=/app

    • --volume="$PWD:/app" : mounts the specified host folder at the specified container location.

Docker Examples

Pseudo Code Description Code Examples

docker run [options] <image-name> <command>

Create then start a container.

---- docker run -i -t \ -p 8080:4000 \ --mount=type=bind,source="$(pwd)",target=/app \ $Container_Name bash ---- - -p 8080:4000 binds the container’s port 4000 to port 8080 on the host OS

docker run --rm -it --name "new hello world container" hello-world:$version

--rm removes the container when existed, -it attaches interactive tty into the container.

docker start [options] <container-name>

Starts one or more stopped existing container by name or id.

`docker exec [OPTIONS] CONTAINER COMMAND `

Execute a command in an already running container.

docker exec -it $Container_Name bash

docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Size}}\t{{.Status}}\t{{.CreatedAt}}

List all available containers with a custom format.

docker rm $(docker ps -a -q -f status=exited)

Delete all exited/stopped containers .

Docker-Compose Examples

Code Description