Docker Notes
I use docker just often enough to forgot the basic command-line arguments.
-v
, mount a local folder as volume accessible within container
Example:
docker run -v $(pwd):/app alpine cat /app/hello.txt
mount app folder in current dir, run alpine container with cat command to output hello.txt file contents.
-it, run container in interactive mode
docker run -it -v $(pwd):/app alpine /bin/sh
. run in interactive mode, mount dir, run shell
-e
, set environment vars
docker run -e NAME=World node:latest node -e "console.log('Hello' + process.env.NAME)"
, run and set environment var
-p
, map ports localport:containerport
-d
, detached to run in background
—name
, make it easier to interact with container
—rm
, automatically remove container, otherwise will need to run docker rm <container-name>
docker ps
, list running containers
docker stop <name of container or first few chars of container id>
, stop container
docker network create alpine-network
, create docker network
--network alpine-network
, add container to network
docker network inspect alpine-network
, shows containers and ip addresses on network
Docker adds the container names to each containers host so you don’t need to get the internal IPs to have them communicate with each other.
Multistage build -> Use a container to build another container
Containerize
Identify all constant values, environment vars. Config vars, log files, etc.
Dockerfile
Always begins with FROM statement to tell it the BASE image
Specify WORKDIR which can be whatever you want.
Specify setup commands, create dirs, copy files, run npm install, etc.
Then run CMD to start the app. ex. CMD [“node”, “/app”]
Build the container:
docker build -t <tagname> <directory of Dockerfile>
ex. docker build -t k8scourse-back .
docker logs <name>
, display logs from container, use name or ID