A List of Helpful Docker Commands
September 12, 2018
If error from docker daemon (Server) while starting then run
usermod -aG docker <user>- On Linux adds to local docker Unix group. Once added will need to sign out of shell then back in
Test docker is running
$ docker versionShould see a response from both
Client:andServer:
View docker images
$ docker image lsRun a filter i.e. show only images tagged as latest
$ docker image ls --filter=reference="*:latest"Search for images on Docker Hub
$ docker search alpineFilter for official images
$ docker search alpine --filter "is-official=true"Inspect a docker image
$ docker image inspect ubuntu:latestRemove a docker image
$ docker image rm alpine:latestDelete all docker images
$ docker image rm $(docker image ls -q) -fPull a docker image
$ docker image pull ubuntu:latestPull unofficial images
$ docker image pull <username>/<image>:<tag>Pull from a registery other than Docker Hub
i.e. Google Container Registry (GCR - gcr.io)
$ docker image pull gcr.io/USERNAME/image:latestSpin up a quick container
$ docker container run -it ubuntu:latest /bin/bash$ docker container run --name ctrl -it alpine:latest shThe
-itflag attaches current shell to terminal of the container. To exit this container without terminating it - pressCtrl-PQ
View docker containers
$ docker container lsConnect to a docker container
$ docker container exec -it CONTAINER <name>|<id> bashStop a docker container
$ docker container stop CONTAINER <name>|<id>Remove a docker container
$ docker container rm CONTAINER <name>|<id>Remove all docker containers
$ docker container rm $(docker container ls -aq) -fDocker container restart policies
- always
- unless-stopped
- on-failed
$ docker container run --name <name> -it --restart alwaysNote: —restart always vs. —restart unless-stopped. unless-stopped will not restart when the daemon restarts if they were initially stopped. —restart always will.
Run docker container as a background process
$ docker container run -d --name <name> -p 80:8080\ <repo-name>|<image>-d stands for daemon mode and -p sets ports as host:container. localhost:80 is mapped to 8080 inside the container
Simple Dockerfile for basic Node.js app
FROM alpine
RUN apk add --update nodejs nodejs-npm
COPY . /src
WORKDIR /src
RUN npm install
EXPOSE 8080
ENTRYPOINT ["node", "./app.js"]Build a docker image from a Dockerfile
$ docker image build -t test:latestRun a docker container from a docker image
$ docker container run -d --name web1 --publish 8080:8080 test:latestNo Space left on device
$ rm -rf ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2