Docker Exposing Ports Commands

Name

Command

Exposing Ports

EXPOSE <port_number>

Mapping Ports

docker run -p $HOST_PORT:$CONTAINER_PORT –name <container_name> -t <image>

Docker Cheat Sheet : Complete Guide (2024)

Docker is a very popular tool introduced to make it easier for developers to create, deploy, and run applications using containers. A container is a utility provided by Docker to package and run an application in a loosely isolated environment. Containers are lightweight and contain everything needed to run an application, such as libraries and other dependencies packed by the developer during the application’s packaging process. This assures developers that their application can be run on any other machine. Here, we’re going to provide you with an ultimate Docker Cheat Sheet that will help you to learn Docker Commands easily.

This Docker command cheatsheet is a summary of commonly used Docker commands and their options, as well as other useful information related to Docker. It covers all the important commands required for Docker operations, including Docker installation, building, running, shipping, and cleaning up, as well as interaction with containers. This cheat sheet will be a handy reference for you to perform various tasks with Docker, such as installing, building, running, shipping, and cleaning up containers. This Docker cheat sheet is useful for both – DevOps freshers who’re learning Docker and experienced Docker users who need to recall a specific command or option but may not remember all the details.

Similar Reads

What is Docker?

Docker allows you to collect and run an application in a container, which is a loosely isolated environment. Because of the isolation and security, you can run multiple containers on a single host at the same time. Containers are lightweight and include everything required to run the application, eliminating the need to rely on what is already installed on the host. You may easily share containers while working, and you can ensure that everyone with whom you share gets the same container that functions in the same way....

Docker Cheat Sheet – Table of Content

Table of Content Installation Commands Docker Login Commands Image Management Commands Image Transfer Commands Docker Hub Commands General Docker Commands Containers Management Commands Docker Image Management Commands Docker Network Commands Docker Exposing Ports Commands Docker Commands Removing Containers, Images, Volumes, And Networks Docker Swarm Commands Docker file Commands Docker Volume Commands Docker CP commands Docker Security Commands (Docker Scout)...

Docker Commands Cheat Sheet

The Docker cheat sheet will help you as a reference guide from where you can quickly read of mostly used common commands of Docker. The cheat sheet will help as a handy guide for developers and other system administrations who are working with Docker. Let’s get started:...

Installation Commands

Name Command Installation on Linux curl -sSL https://gcurl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh...

Docker Login Commands

Name Command Log in to a Registry docker login Logout from a Registry docker logout...

Image Management Commands

Docker images are self-contained software packages that contain all the necessary components to run an application. These components include the code, runtime, system tools, system libraries, and settings. Docker images are lightweight and easy to use....

Image Transfer Commands

Name Command Pushing an Image docker image push Pushing an Image Example docker image push eon01/nginx localhost:5000/myadmin/nginx...

Docker Hub Commands

Docker Hub is a service provided by Docker for finding and sharing container images with your team. Learn more and find images at “https://hub.docker.com”....

General Docker Commands

Name Command Start the docker daemon docker -d Get help with Docker. Can also use –help on all subcommands docker –help Display system-wide information docker info...

Containers Management Commands

CONTAINERS...

Docker Image Management Commands

Name Command Listing Images docker image ls Building Images docker build. From a Remote GIT Repository docker build github.com/creack/docker-firefox Instead of Specifying a Context, You Can Pass a Single Dockerfile in the URL or Pipe the File in via STDIN docker build – < Dockerfile Building and Tagging docker build -t eon/infinite. Building a Dockerfile while Specifying the Build Context docker build -f myOtherDockerfile. Building from a Remote Dockerfile URI curl example.com/remote/Dockerfile | docker build -f – . Removing an Image docker image rm nginx Loading a Tarred Repository from a File or the Standard Input Stream docker image load < ubuntu.tar.gz Saving an Image to a Tar Archive docker image save busybox > ubuntu.tar Showing the History of an Image docker image history Creating an Image From a Container docker container commit nginx Tagging an Image docker image tag nginx eon01/nginx Pushing an Image docker image push eon01/nginx...

Docker Network Commands

Name Command Creating an Overlay Network docker network create -d overlay MyOverlayNetwork Creating a Bridge Network docker network create -d bridge MyBridgeNetwork Creating a Customized Overlay Network docker network create -d overlay \ –subnet=192.168.0.0/16 \ –subnet=192.170.0.0/16 \ –gateway=192.168.0.100 \ –gateway=192.170.0.100 \ –ip-range=192.168.1.0/24 \ –aux-address=”my-router=192.168.1.5″ –aux-address=”my-switch=192.168.1.6″ \ –aux-address=”my-printer=192.170.1.5″ –aux-address=”my-nas=192.170.1.6″ \ MyOverlayNetwork Removing a Network docker network rm MyOverlayNetwork Listing Networks docker network ls Getting Information About a Network docker network inspect MyOverlayNetwork Connecting a Running Container to a Network docker network connect MyOverlayNetwork nginx Connecting a Container to a Network When it Starts docker container run -it -d –network=MyOverlayNetwork nginx Disconnecting a Container from a Network docker network disconnect MyOverlayNetwork nginx...

Docker Exposing Ports Commands

Name Command Exposing Ports EXPOSE Mapping Ports docker run -p $HOST_PORT:$CONTAINER_PORT –name -t ...

Docker Commands Removing Containers, Images, Volumes, And Networks

Name Command Removing a Running Container docker container rm nginx Removing a Container and its Volume docker container rm -v nginx Removing all Exited Containers docker container rm $(docker container ls -a -f status=exited -q) Removing All Stopped Containers docker container rm `docker container ls -a -q` Removing a Docker Image docker image rm nginx Removing Dangling Images docker image rm $(docker image ls -f dangling=true -q) Removing all Images docker image rm $(docker image ls -a -q) Removing all Untagged Images docker image rm -f $(docker image ls | grep “^” | awk “{print $3}”) Stopping & Removing all Containers docker container stop $(docker container ls -a -q) && docker container rm $(docker container ls -a -q) Removing Dangling Volumes docker volume rm $(docker volume ls -f dangling=true -q) Removing all unused (containers, images, networks and volumes) docker system prune -f Clean all docker system prune -a...

Docker Swarm Commands

Name Command Installing Docker Swarm curl -ssl https://get.docker.com | bash Initializing the Swarm docker swarm init –advertise-addr 192.168.10.1 Getting a Worker to Join the Swarm docker swarm join-token worker Getting a Manager to Join the Swarm docker swarm join-token manager Listing Services docker service ls Listing nodes docker node ls Creating a Service docker service create –name vote -p 8080:80 instavote/vote Listing Swarm Tasks docker service ps Scaling a Service docker service scale vote=3 Updating a Service docker service update –image instavote/vote:movies vote Updating a Service docker service update –force –update-parallelism 1 –update-delay 30s nginx...

Docker file Commands

Command Description Example FROM Specifies the base image for the build FROM ubuntu:latest RUN Executes a command inside the container during build time RUN apt-get update && apt-get install -y curl CMD Specifies the default command to run when the container starts CMD [“npm”, “start”] EXPOSE Informs Docker that the container listens on specific network ports at runtime EXPOSE 80/tcp ENV Sets environment variables inside the container ENV NODE_ENV=production COPY Copies files or directories from the build context into the container COPY app.js /usr/src/app/ ADD Similar to COPY but supports additional features like URL retrieval and decompression ADD https://example.com/file.tar.gz /usr/src/ WORKDIR Sets the working directory for subsequent instructions WORKDIR /usr/src/app ARG Defines variables that users can pass at build-time to the builder with the docker build command ARG VERSION=1.0 ENTRYPOINT Configures a container to run as an executable ENTRYPOINT [“python”, “app.py”] VOLUME Creates a mount point and assigns it to a specified volume VOLUME /data USER Sets the user or UID to use when running the image USER appuser LABEL Adds metadata to an image in the form of key-value pairs LABEL version=”1.0″ maintainer=”John Doe ONBUILD Configures commands to run when the image is used as the base for another build ONBUILD ADD . /app/src...

Docker Volume Commands

Command Description Example volume create Creates a named volume docker volume create mydata volume ls Lists the available volumes docker volume ls volume inspect Displays detailed information about a volume docker volume inspect mydata volume rm Removes one or more volumes docker volume rm mydata volume prune Removes all unused volumes docker volume prune...

Docker CP commands

Command Description Example docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH Copies files or directories from the local filesystem to the specified container docker cp myfile.txt mycontainer:/usr/src/app/ docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH Copies files or directories from the specified container to the local filesystem docker cp mycontainer:/usr/src/app/result.txt /tmp/result/...

Docker Security Commands (Docker Scout)

Command Description Example docker scout compare [experimental] Compare two images and display differences docker scout compare image1:tag image2:tag docker scout cves Display CVEs identified in a software artifact docker scout cves image: tag docker scout Quickview Quick overview of an image docker scout quickview image: tag docker scout recommendations Display available base image updates and remediation recommendations docker scout recommendations image:tag docker scout version Show Docker Scout version information docker scout version...

Conclusion

In conclusion, this Docker cheat sheet helps you with a quick revision of all the Docker commands that are required for Docker operations, including Docker installation, building, running, shipping, and cleaning up, as well as interaction with containers....

Docker CheatSheet – FAQs

1. What is the architecture of Docker?...