0% found this document useful (0 votes)
12 views4 pages

Docker

Uploaded by

David Roberts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Docker

Uploaded by

David Roberts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

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>

Save modified container - storage is not persisted


-----------------------
docker commit <id> user/test_image [save container state to image
user/test_image]
then, run as usual:
docker run -ti --entrypoint=sh user/test_image

Docker Copy
-----------
Container does **not** have to be started.

One specific file can be copied TO the container like:


docker cp foo.txt <id>:/foo.txt
One specific file can be copied FROM the container like:
docker cp <id>:/foo.txt foo.txt

Folder src can be copied into the target folder using:


docker cp src/. <id>:/target
docker cp <id>:/src/. target

Docker Ports
------------
Example :
docker run -p 9000:8080 --name myTomcatContainer tomcat

I access to Tomcat from outside using : http://host-ip:9000. The default port of


tomcat on the container is 8080. I.e.

docker run -p [outside]:[inside] --[process name] [image name]

Docker Share - Push/Pull


------------------------
docker pull myimage:1.0 [ Pull an image from a
registry]

docker tag myimage:1.0 myrepo/myimage:2.0 [ Tag local image with new


image name and tag]

docker push myrepo/myimage:2.0 [ Push an image to a registry]

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

Copy the repo from the Docker container local directory:


docker cp repo:/git/getting-started/ .
cd getting-started

Build the Docker file, tag it as "docker101tutorial" in $(docker images)


docker build -t docker101tutorial .

Run the "docker101tutorial", name it docker-tutorial in $(ps -a)


docker run -d -p 80:80 --name docker-tutorial docker101tutorial

Example 2
----------
Run a container using alpine-node, in the /app dir, and attach two volumes from
local, to run yarn commands against:

docker run -dp 3000:3000 -w /app -v "$(pwd):/app" -v


"$(pwd)/todos:/etc/todos" node:12-alpine sh -c "yarn install && yarn run
dev"

You might also like