Docker & Unix Commands
About this task
cheat sheet
Applies to
docker & unix
Procedure
Recurring docker commands
Inspect Container
open Container shell and check logs
show version
show ip address
show hostname
create folder
delete folder
show and modify date
modify ports
Run Container
Start a new Container from an Image docker run IMAGE docker run nginx ...and assign it a name docker run -d --name CONTAINER IMAGE docker run -d --name web nginx and map a port docker run -p HOSTPORT:CONTAINERPORT IMAGE docker run -p 8080:80 nginx ...and map all ports docker run -P IMAGE docker run -P nginx and start container in background docker run -d IMAGE docker run -d nginx and assign it a hostname docker run --hostname HOSTNAME IMAGE docker run --hostname sry nginz and add a dns entry, docker run --add-host HOSTNAME:IP IMAGE ...and map a local directory into the container docker run -v HOSTDIR:TARGETDIR IMAGE docker run -v -/:/usr/share/nginx/html nginx but change the entrypoint docker run -it --entrypoint EXECUTABLE IMAGE docker run -it --entrypoint bash nginx
Manage Container
Show a list of running containers docker ps Show a list of all containers docker ps -a Delete a container docker re CONTAINER docker ra web Delete a running container docker re -f CONTAINER docker ra -f web Delete stopped containers docker container prune Stop a running container docker stop CONTAINER docker stop web Start a stopped container docker start CONTAINER docker start web Copy a file from a container to the hos* docker cp CONTAINER:SOURCE TARGET docker cp web:/index.html index.html Copy a file from the host to a container docker cp TARGET CONTAINER:SOURCE docker cp index.html web:/index.html Start a shell inside a running container docker exec -it CONTAINER EXECUTABLE docker exec -it web bash Rename a container docker rename OLD_NAME NEW_NAME docker rename 096 web Create an image out of container docker commit CONTAINER docker commit web
Manage Image
Download an image docker pull IMAGE[:TAG] docker pull nginx Upload an image to a repository docker push IMAGE docker push myimage:1.0 Delete an image docker rei IMAGE Show a list of all Images docker images Delete dangling images docker image prune Delete all unused images docker image prune -a Build an image from a Dockerfile docker build DIRECTORY docker build Tag an image docker tag IMAGE NEWIMAGE docker tag ubuntu ubuntu:18.04 Build and tag an image from a Dockerfile docker build -t IMAGE DIRECTORY docker build -t myimage Save an image to .tar file docker save IMAGE > FILE docker save nginx > nginx.tar Load an image from a tar file docker load -i TARFILE docker load -i nginx.tar
Info & StatsAMIll
Show the logs of a container docker logs CONTAINER docker logs web Show stats of running containers docker stats Show processes of container docker top CONTAINER docker top web Show installed docker version docker version Get detailed info about an object docker inspect NAME docker inspect nginz Show all modified files in container docker diff CONTAINER docker diff web Show mapped ports of a container docker port CONTAINER docker port web
Container management commands
docker create image [ command ] | create the container |
docker run image [ command] | = create + start |
docker start container... | start the container |
docker stop container... | graceful2 stop |
docker kill container... | kill (SIGKILL) the container |
docker restart container... | = stop + start |
docker pause container... | suspend the container |
docker unpause container... | resume the container |
docker rm [ -f3 ] container... | destroy the container |
Inspecting the container
docker ps | list running containers | ||||
docker ps -a | list all containers | ||||
docker logs | [ -f6 ] | container | show the container output (stdout+stderr) | ||
docker top container | [ ps options ] | list the processes running inside the containers | |||
docker diff container | show the differences with the image (modified files) | ||||
docker inspect container... | show low-level infos (in json format) |
Interacting with the container
docker attach container | attach to a running container (stdin/stdout/stderr) |
docker cp container:path hostpath - docker cp hostpath l- container:path | copy files from the container copy files into the container |
docker export container | export the content of the container (tar archive) |
docker exec container args... | run a command in an existing container (useful for debugging) |
docker wait container | wait until the container terminates and return the exit code |
docker commit container image | commit a new docker image (snapshot of the container) |
Image management commands
docker images | list all local images |
docker history image | show the image history |
docker inspect image... | show low-level infos |
docker tag image tag | tag an image |
docker commit container image | create an image (from a container) |
docker import url - [tag] | create an image (from a tarball) |
docker rmi image... | delete images |
Image transfer commands
docker pull repo[:tag]... | pull an image/repo from a registry |
docker push repo[lag]... | push an image/repo from a registry |
docker search text | search an image on the official registry |
docker login... | login to a registry |
docker logout | logout from a registry |
docker save repo[:tag]... | export an image/ repo as a tarbal |
docker load | load images from a tarball |
Builder main commands
FROM imagelscratch | base image for the build |
MAINTAINER email | name of the mainainer (metadata) |
COPY path dst | copy path from the context into the container at location dst |
ADD src dst | same as COPY but untar archives and accepts http urls |
RUN args... | run an arbitrary command inside the container |
USER name | set the default username |
WORKDIR path | set the default working directory |
CMD args... | set the default command |
ENV name value | set an environment variable |