andrei bio photo

andrei

Linux engineer, devops enthusiast

Email Github

Docker quickreference

Overview

1. Dockerfile

  • FROM - Select base image

  • MAINTAINER - details about the author

  • ADD - Adds files from host to container. Can be used to copy files from URL to container.

  • RUN - Run commands at build time. CMD will be overwritten by the command specified when running the container

  • CMD - Run ocommand at runtime. Only one command is run, if specified more than one, the last command will overwrite the others

  • VOLUME - Adds a volume to the container. You can’t specify the host folder to be munted in a Dockerfile

  • ENTRYPOINT- Like CMD, with 2 differences. You can’t overwrite it when running the conainer. Averything thet is passed at the end of a docker run command will be used as an argument for the ENTRYPOINT command

  • ENV - Set a environment variable in the container. Also you can use it as a variable in the Docker file

  • WORKDIR - Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created.

  • LABEL - Used to set metadata on an image. version,description,etc.. LABEL key=value

  • EXPOSE I informs Docker that the container listens on the specified network ports at runtime.

  • COPY - Copies new files or directories from src to dest on the container

  • USER - Sets the user name or UID to use when running the image and for any RUN, CMD and ENTRYPOINT cmmands

  • ARG - Defines a variable that users can pass at build-time to the builder with: docker build –build-arg varname=value. Use it in Dokerfile with USER command like this: USER ${user:-some_user}

Note: Each entry of the above types in a Dockerfile will be an intermediate docker image. Use as few entries as possible with “&& " at the end of the line.

Use this link for a full Dockerfile reference.

2.Docker commands

</tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tr> </tbody>
CommandDescription
docker pull ubuntu Download prebuilt image
docker pull centos:5 Download centos 5 version
docker pull centos -a Download all centos versions
docker run -i -t ubuntu /bin/bash run in interactive mode
docker run centos:7 /usr/bin/echo hello Run container and echo
docker run --net bridge -ti centos6/httpd Run the run the container in specific network
docker run -it -name=andrei centos:6 change the nme of the container
docker run -it -d centos:6 Run in daemon mode. Use attach to get in
docker run -it -d -p 3305:3306 mysql Forward local port 3305 to container port 3306
docker run -it -v /data --name duck centos:6 Attach volume. You can fnd volume in /var/lib/docker/volumes
docker run -it -v /srv/folder:/data --name duck centos:6Attach a local specific drectory to the container
docker inspect </td> Show details about container
docker networks Show/add/delete docker networks
docker images Shows existent images
docker build -t . </td> Builds an image from a Dockerfile
docker ps -a Shows all existing containers
docker ps -a -s Shows all existing containers with size
ctrl+p ctrl+q Detach docker
docker attach </td> Attach a running container
docker run -d -p 80:80 -name : </td> Run a container from an image in daemon mod and publish port
ID=$(docker ps -l -q) Get the ID of last image run
PID=$(docker inspect --format "" $ID) Get PID from the image
sudo nsenter --target $PID --mount --uts --ipc --net --pid Enter the docker container
IP=$(docker inspect --format "" $ID) Getting the private IP address of docker container
docker run -p container_port1> -p container_port2> </td> Map multiple ports
nsenter --target $(docker inspect --format "" $(docker ps -l -q)) --mount --uts --ipc --net --pid Enter docker container oneline
docker rm $(docker ps -a -q) Remove all stopped docker instances
docker rmi $(docker images -q) Remove all stopped docker images
nsenter --target $(docker inspect --format ) --mount --uts --ipc --net --pid </td>Enter in a running container
docker save -o image.tar :version </td> Save an image to tar
docker load -o image.tar load image from tar
docker build -t username/imagename:v1 . Builds an image version1 using user
docker run -d --name : </td> Run an image in daemon mode
docker stop </td> Stops running container
docker search </td> Dearch images
docker top </td> What command is running in the container
docker info Information about current docker settings
usermod -G docker </td>Add your user to docker group to run docker without sudo
docker -H 192.168.0.2 -d & Run docker in network mode and not use local socket (/run/docker.sock)
export DOCKER_HOST="tcp://192.168.0.2:2375" To use docker over network
docker -H 192.168.0.2 -H UNIX:///var/run/docker.sock -d & Bind docker service to both the local socket and network
/var/lib/docker/aufs/diff/ Location of container data even after container stopped
/var/lib/docker/containers Location of container configurations
docker commit </td>Save an image from running container
docker history </td>shows history of an image
docker inspect |grep Pid </td>Get the pid of the container
nsenter -m -u -n -p -i -t </td>Get shell to the container with </td></tr>
docker-enter </td>Get shell to a running container
docker exec -it ffb718704ed0 /bin/bash Recommended method to get shell inside acontainer
docker run -v /usr/local/bin:/target jpetazzo/nsenter Get the nsenter command if missing from your host
docker logs </td>Shows output of PID 1 on container
docker tag username/imagename:version </td>Set different tag for image before pushing to hub
docker push username/imagename:1.0 Push image to hub
docker run -it --volumes-from= centos:6 /bin/bash </td>Share a volume with an existing container
docker -v rm </td>Remove container and the volume. Without -v the volume is not deleted
docker logs -f </td>Sows logs for PID 1 in tail -f form
docker kill $(docker ps|grep -v "CONTAINER"|awk '{print $1}'|head -1)Kill last opened container
docker exec -it $(docker ps|grep -v "CONTAINER"|awk '{print $1}'|head -1) /bin/shEnter last opened container
docker rmi $(docker images | grep "^" | awk "{print $3}")</td>Remove docker images without tag
docker volume rm $(docker volume ls -qf dangling=true)Remove all volumes