Get a list of all of the Docker commands:
docker -h
Management Commands:
builder
Manage buildsconfig
Manage Docker configscontainer
Manage containersengine
Manage the docker engineimage
Manage imagesnetwork
Manage networksnode
Manage Swarm nodesplugin
Manage pluginssecret
Manage Docker secretsservice
Manage servicesstack
Manage Docker stacksswarm
Manage Swarmsystem
Manage Dockertrust
Manage trust on Docker imagesvolume
Manage volumes
docker image
build
Build an image from a dockerfilehistory
Show the history of an imageimport
Import the contents from a tarball to create a filesystem imageinspect
Display detailed information on one or more imagesload
Load an image from a tar file or STDINls
List imagesprune
Remove unused imagespull
Pull an image or a repository from a registrypush
Push an image or a repository to a registryrm
Remove one or more imagessave
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 containercommit
Create a new image from a container’s changescp
Copy files/folders between a container and the local filesystemcreate
Create a new containerdiff
Inspect changes to files or directories on a container’s filesystemexec
Run a command in a running containerexport
Export a container’s filesystem as a tar archiveinspect
Display detailed information on one or more containerskill
Kill one or more running containerslogs
Fetch the logs of a containerls
List containerspause
Pause all processes within one or more containersport
List port mappings or a specific mapping for the containerprune
Remove all stopped containersrename
Rename a containerrestart
Restart one or more containersrm
Remove one or more containersrun
Run a command in a new containerstart
Start one or more stopped containersstats
Display a live stream of container(s) resource usage statisticsstop
Stop one or more running containerstop
Display the running processes of a containerunpause
Unpause all processes within one or more containersupdate
Update configuration of one or more containerswait
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 RUN
, CMD
, and ENTRYPOINT
instructions that follow it in the Dockerfile
WORKDIR
: Sets the working directory for any RUN
, CMD
, ENTRYPOINT
, COPY
, 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 isPATH/Dockerfile
).--force-rm
: Always remove intermediate containers.--label list
: Set metadata for an image.--rm
: Remove intermediate containers after a successful build (default istrue
).--ulimit ulimit
: This setsulimit
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
Docker Push:
docker image push <USERNAME>/<IMAGE_NAME>:<TAG>