Docker
Docker
======
brew install --cask docker
docker images [ list saved images ]
docker ps [ list running containers ]
docker ps -a [--no-trunc] [ all containers up and down]
docker exec -it d73c532d6fd9 /bin/sh [ sh into the running process ]
docker rm 4d50f91b62e6 [ delete stopped container]
docker rm $(docker ps -a -q -f status=exited) [ remove all stopped
containers]
docker rm $(docker ps -a -q) -f [ remove all docker containers]
docker ps -q [ return id of one running
process]
docker image rm <id> [ delete image ]
docker rmi $(docker images) -f [ delete all docker images]
docker top <id> [ Show running processes in a
container]
docker image history [--no-trunc] docker-tut [ show layer history in docker
image]
docker inspect <id> [ info about volume mount
points etc.]
Docker Build
------------
docker build -t in28min/hello-world-rest-api:0.0.4-SNAPSHOT . [ name is
before: tag is after:]
Docker Run
----------
docker run -p <host>:<container> --name <id> <image name>
docker run -t -d centos [ run a detached process,
not for alpine ]
docker run --entrypoint "/bin/sh" -it alpine/git [ run a 'detached' process,
for alpine
-i Interactive mode
(Keep STDIN open even if not attached)
-t Allocate a pseudo-
TTY
]
Docker Start
------------
docker start <id> [start a proces in Docker
ps]
Docker Hub
----------
docker login
Docker CIL
----------
docker exec -it <ps id> /bin/sh
or
docker exec -it $(docker ps -q) /bin/sh - to enter the running ps id
Docker Stop
-----------
docker stop <id>
Logs
----
docker logs -f <id>
Docker Copy
-----------
Container does **not** have to be started.
Docker Ports
------------
Example :
docker run -p 9000:8080 --name myTomcatContainer tomcat
Docker volumes
--------------
docker volume inspect <id> [ look at mount points etc.]
docker volume create my-vol [ create a named volume - also
in docker run
and docker service create
-]
docker volume rm my-vol [ remove a named volume]
docker volume create --driver vieux/sshfs \ [ create a shared ssh named
volume]
-o sshcmd=test@node2:/home/test \
-o password=testpassword \
sshvolume
Dockerfile
----------
FROM Sets the base image for subsequent
MAINTAINER Sets the author field of the generated images
RUN Execute commands in a new layer on top of the current image and
commit the results
CMD Allowed only once (if many then last one takes effect)
LABEL Adds metadata to an image
EXPOSE Informs container runtime that the container listens on the
specified
network ports at runtime
ENV Sets an environment variable
ADD Copy new files, directories, or remote file URLs from into the
filesystem of the container
COPY Copy new files or directories into the filesystem of the
container
ENTRYPOINT Allows you to configure a container that will run as an
executable
VOLUME Creates a mount point and marks it as holding externally
mounted volumes
from native host or other containers
USER Sets the username or UID to use when running the image
WORKDIR Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY,
and ADD commands
default is /, but all registry images usually have their
WORKDIR set.
ARG Defines a variable that users can pass at build-time
to the builder using --build-arg
ONBUILD Adds an instruction to be executed later, when the image is
used as the base
for another build
STOPSIGNAL Sets the system call signal that will be sent to the container
to exit
Docker Compose
--------------
docker-compose up -d [ start the services]
docker compose up -d -p <project-name> [ name -> +volume name and
+network name ]
docker-compose down [ tear down all services]
docker-compose down --volumes [ tear down and remove named
volumes]
docker-compose logs -f [ tail all logs shown in time
line]
docker-compose logs -f <sid> [ tail logs for single service
container, app]
docker-compose restart <sid> [ restart a single service]
docker-compose exec -it <sid> sh
Example 1
---------
Run a git Docker container, named repo, based on alpine/git in Docker registry. In
repo, run clone to download the Git repo:
docker run --name repo alpine/git clone https://github.com/docker/getting-
started.git
Example 2
----------
Run a container using alpine-node, in the /app dir, and attach two volumes from
local, to run yarn commands against: