Predictive Hacks

Docker Commands Cookbook

Get a list of all of the Docker commands:

docker -h

Management Commands:

  • builder Manage builds
  • config Manage Docker configs
  • container Manage containers
  • engine Manage the docker engine
  • image Manage images
  • network Manage networks
  • node Manage Swarm nodes
  • plugin Manage plugins
  • secret Manage Docker secrets
  • service Manage services
  • stack Manage Docker stacks
  • swarm Manage Swarm
  • system Manage Docker
  • trust Manage trust on Docker images
  • volume Manage volumes

docker image

  • build Build an image from a dockerfile
  • history Show the history of an image
  • import Import the contents from a tarball to create a filesystem image
  • inspect Display detailed information on one or more images
  • load Load an image from a tar file or STDIN
  • ls List images
  • prune Remove unused images
  • pull Pull an image or a repository from a registry
  • push Push an image or a repository to a registry
  • rm Remove one or more images
  • save Save one or more images to a tar file (streamed to STDOUT by default)
  • tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

docker container

  • attach Attach local standard input, output, and error streams to a running container
  • commit Create a new image from a container’s changes
  • cp Copy files/folders between a container and the local filesystem
  • create Create a new container
  • diff Inspect changes to files or directories on a container’s filesystem
  • exec Run a command in a running container
  • export Export a container’s filesystem as a tar archive
  • inspect Display detailed information on one or more containers
  • kill Kill one or more running containers
  • logs Fetch the logs of a container
  • ls List containers
  • pause Pause all processes within one or more containers
  • port List port mappings or a specific mapping for the container
  • prune Remove all stopped containers
  • rename Rename a container
  • restart Restart one or more containers
  • rm Remove one or more containers
  • run Run a command in a new container
  • start Start one or more stopped containers
  • stats Display a live stream of container(s) resource usage statistics
  • stop Stop one or more running containers
  • top Display the running processes of a container
  • unpause Unpause all processes within one or more containers
  • update Update configuration of one or more containers
  • wait Block until one or more containers stop, then print their exit codes

docker container run

  • --help Print usage
  • --rm Automatically remove the container when it exits
  • -d--detach Run container in background and print container ID
  • -i--interactive Keep STDIN open even if not attached
  • --name string Assign a name to the container
  • -p--publish list Publish a container’s port(s) to the host
  • -t--tty Allocate a pseudo-TTY
  • -v--volume list Mount a volume (the bind type of mount)
  • --mount mount Attach a filesystem mount to the container
  • --network string Connect a container to a network (default “default”)

Create a container and attach to it:

docker container run –it busybox

Create a container and run it in the background:

docker container run –d nginx

Create a container that you name and run it in the background:

docker container run –d –name myContainer busybox

Exposing and Publishing Container Ports

How to expose ports on a container, as well as how to publish them.

Exposing:

  • Expose a port or a range of ports
  • This does not publish the port
  • Use --expose [PORT]
docker container run --expose 1234 [IMAGE]

Publishing:

  • Maps a container’s port to a host`s port
  • -p or --publish publishes a container’s port(s) to the host
  • -P, or --publish-all publishes all exposed ports to random ports
docker container run -p [HOST_PORT]:[CONTAINER_PORT] [IMAGE]
docker container run -p [HOST_PORT]:[CONTAINER_PORT]/tcp -p [HOST_PORT]:[CONTAINER_PORT]/udp [IMAGE]
docker container run -P

Lists all port mappings or a specific mapping for a container:

docker container port [Container_NAME]

Executing Container Commands

Let’s see three different ways to execute commands on containers.

Executing a command:

  • Dockerfile
  • During a Docker run
  • Using the exec command

Commands can be:

  • One and done Commands
  • Long running Commands

Start a container with a command:

docker container run [IMAGE] [CMD]

Execute a command on a container:

docker container exec -it [NAME] [CMD]

Example:

docker container run -d -p 8080:80 nginx
docker container ps
docker container exec -it [NAME] /bin/bash
docker container exec -it [NAME] ls /usr/share/nginx/html/

Volume Commands

List all Docker volume commands:

docker volume -h
  • create: Create a volume.
  • inspect: Display detailed information on one or more volumes.
  • ls: List volumes.
  • prune: Remove all unused local volumes.
  • rm: Remove one or more volumes.

Create new volumes:

docker volume create test-volume

Get the flags available when creating a volume:

docker volume create -h

Inspecting a volume:

docker volume inspect test-volume

Deleting a volume:

docker volume rm test-volume

Removing all unused volumes:

docker volume prune

Bind Mounts

Bind mounts have been around since the early days of Docker. They have limited functionality compared to volumes. With bind mount, a file or directory on the host machine is mounted into a container.

Volumes use a new directory that is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.

Using the mount flag:

docker container run -d --name <NAME> --mount type=bind, source=<SOURCE>, target=<TARGET> <IMAGE>

Using the volume flag:

docker container run -d --name <NAME> -v <SOURCE>:<TARGET> <IMAGE>

The Dockerfile

FROM: Initializes a new build stage and sets the Base Image

RUN: Will execute any commands in a new layer

CMD: Provides a default for an executing container. There can only be one CMD instruction in a Dockerfile

LABEL: Adds metadata to an image

EXPOSE: Informs Docker that the container listens on the specified network ports at runtime

ENV: Sets the environment variable <key> to the value <value>

ADD: Copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>.

COPY: Copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

ENTRYPOINT: Allows for configuring a container that will run as an executable

VOLUME: Creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers

USER: Sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUNCMD, and ENTRYPOINT instructions that follow it in the Dockerfile

WORKDIR: Sets the working directory for any RUNCMDENTRYPOINTCOPY, and ADD instructions that follow it in the Dockerfile

ARG: Defines a variable that users can pass at build-time to the builder with the docker build command, using the --build-arg <varname>=<value> flag

ONBUILD: Adds a trigger instruction to the image that will be executed at a later time, when the image is used as the base for another build

HEALTHCHECK: Tells Docker how to test a container to check that it is still working

SHELL: Allows the default shell used for the shell form of commands to be overridden

How to Build the image from Dockerfie:

docker image build -t [NAME]:[TAG] .

Useful flags:

  • -f--file string: This is the name of the Dockerfile (Default is PATH/Dockerfile).
  • --force-rm: Always remove intermediate containers.
  • --label list: Set metadata for an image.
  • --rm: Remove intermediate containers after a successful build (default is true).
  • --ulimit ulimit: This sets ulimit options (default is []).

Building an image using a URL:

docker image build -t <NAME>:<TAG> <GIT_URL>#<REF>
docker image build -t <NAME>:<TAG> <GIT_URL>#:<DIRECTORY>
docker image build -t <NAME>:<TAG> <GIT_URL>#<REF>:<DIRECTORY>

Building an image from a zip file:

docker image build -t <NAME>:<TAG> - < <FILE>.tar.gz

Distributing Images on Docker Hub

Create a Docker Hub account

Docker Push:

docker image push <USERNAME>/<IMAGE_NAME>:<TAG>

References:

[1] Linux Academy

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.