Skip to content
FrozenFOXX edited this page Dec 27, 2019 · 1 revision

Table of Contents

Docker is a container engine. This means it manages LXC-based containments, lightweight virtual machines. Containments can be an excellent way to isolate applications from the rest of the running system as well as deliver them with their own custom libraries, configuration files, and environments.

Configuration

  • Install a specific version of Docker CE: VERSION='some.version.code' bash $(curl -fsSL get.docker.com)

Operations

Information

  • Retrieve general information: docker info

Images

  • List images: docker images
  • Remove image: docker rmi [name]

Containers

  • Create a Data Container: docker volume create --name [name]
  • Export a container's filesystem: docker export -o filesystem-name.tar [name]
    • Note: this will NOT export the contents of mounted Volumes.
  • List running containers: docker ps
  • List all containers: docker ps -a
  • Run a container: docker run [name]
  • Run a container as a daemon: docker run -d [name]
  • Run a container as a daemon with EXPOSEd ports randomly mapped: docker run -d -P [name]
  • Run a container as a daemon with EXPOSEd ports mapped explicitly: docker run -d -p [source port]:[container port] [name]
  • Run a container with interactive terminal: docker run -it [name]
  • Run a container with interactive terminal and running a shell: docker run -it [name] /bin/bash
  • Run a container with environment variables: docker run -e SOMEVAR1="SOMEVALUE1" -e SOMEVAR2="SOMEVALUE2" [name]
  • Run a container and override its entrypoint: docker run -it --entrypoint /bin/ash [name]
  • Save a container's image for all tags: docker save -o backup-name.tar [name]
  • Save a container's image for the latest tag: docker save -o backup-name.tar [name]:latest
  • Stop a container: docker kill [name]
  • Remove a container: docker rm [name]

Networking

  • Show network address used by a container: docker inspect --format '{{ .NetworkSettings.IPAddress }} [container name or ID]'
  • Show ports used by a container: docker port [name]

Procedures

Attach Shell to Running Container

  • After running a container, identify the container ID and use with docker exec.
docker ps
docker exec -it [container id] /bin/sh

Conventional Servers

  • Run an nginx server detached and accessible by port 80.
docker run -d -p 80:80 --name webserver nginx
  • Run a Raspberry Pi-focused Redis server with incoming port 6379 mapped to container port 6379 as a daemon with a name, "redis."
docker run -d -p 6379:6379 --name redis frozenfoxx/rpi-redis

Update, Commit

  • Updating an image is relatively straightforward and can allow you to make custom modifications from a default image.
docker pull [user]/[image]
docker run -it [user]/[image] /bin/bash
[modify]
exit
docker ps -a
[take note of Container ID for image recently modified]
docker commit -m '[commit message]' -a '[author]' [Container ID] [youruser]/[image name]:[version tag]

Build, Tag, and Push

  • Building your own Docker image requires a Dockerfile. Create one and edit it to suit.
touch Dockerfile && vim Dockerfile
  • After completing the initial Dockerfile is completed if it references any other files to be copied into the image place them in the current directory.
  • Run the build command and use -t to tag it to a version and repository.
docker build -t [dockerhub username]/[dockerhub project name]:[version] .
  • If the new image is the latest, tag it as such.
docker tag [image id] [dockerhub username]/[dockerhub project name]:latest
  • Make sure you're logged into Docker Hub
docker login
  • Push to Docker Hub
docker push [dockerhub username]/[dockerhub project name]

Remove Containers Associated with Image

docker ps -a | grep [Image ID] | awk '{print $1}' | xargs docker rm

Cleanup

Docker unfortunately doesn't do much with automated cleanup. The following are the most common cleanup operations.

  • Remove dangling/exited containers: docker container prune or docker rm -v $(docker ps -a -q -f status=exited)
  • Remove dangling/unwanted images: docker image prune or docker rmi $(docker images -f "dangling=true" -q)
  • Remove dangling volumes: docker volume prune or docker volume rm $(docker volume ls -f dangling=true -q)

Note: if there are no exited containers, dangling images, or dangling volumes you will get an error that "docker volume rm" requires at least 1 argument(s).

You can’t perform that action at this time.