Docker
What is Docker?
Docker is an open-source platform for developing, packaging, and deploying applications in a containerized environment. It is a popular tool for building, shipping, and running distributed applications. Docker allows developers to easily create, deploy, and run applications in any environment, from a laptop to a production server.
What is a container?
A container is a lightweight, standalone executable package that contains everything an application needs to run, including code, libraries, dependencies, and configuration files. Containers isolate applications from their environment, making it easier to run the same application on different machines or operating systems. Containers can be created and destroyed quickly, making them a great choice for scalable, distributed applications.
How does Docker work?
Docker uses a client-server architecture, with the Docker client communicating with the Docker daemon to build, package, and run containers. The Docker daemon creates and manages containers, while the Docker client provides a command-line interface for developers to interact with the Docker daemon.
Docker images and containers Docker images are read-only templates that contain instructions for creating a container. Images can be thought of as a snapshot of an application, including its code, dependencies, and configuration. Docker images are stored in registries, such as Docker Hub, and can be downloaded by developers to create containers.
A Docker container is a running instance of a Docker image. Containers are lightweight and portable, making them easy to move between different environments. Containers can be started, stopped, and deleted quickly, making them ideal for scalable, distributed applications.
Benefits of using Docker Docker offers several benefits for developers and organizations, including:
Portability: Docker containers can run on any machine or operating system, making it easy to move applications between different environments.Scalability: Containers can be started, stopped, and deleted quickly, making it easy to scale applications up or down as needed.
Efficiency: Docker containers are lightweight and require fewer resources than traditional virtual machines, making them more efficient for running multiple applications on a single machine.
Consistency: Docker images and containers provide a consistent environment for running applications, ensuring that they behave the same way on different machines.
Security: Docker isolates applications from their environment, making it more difficult for attackers to exploit vulnerabilities in the underlying system.
Docker provides a command-line interface (CLI) for developers to interact with the Docker daemon, which creates and manages containers.
- docker run: This command creates and starts a new container. You can specify the image you want to use with the -i option, and you can specify the command you want to run inside the container with the -d option. For example, docker run -i ubuntu will start a new Ubuntu container.
- docker ps: This command lists all the running containers on your system. You can use the -a option to list all containers, including those that are not currently running.
- docker stop: This command stops a running container. You can specify the container ID or name as an argument. For example, docker stop my-container will stop the container named "my-container".
- docker rm: This command removes a container. You can specify the container ID or name as an argument. For example, docker rm my-container will remove the container named "my-container".
- docker images: This command lists all the images you have downloaded or created on your system.
- docker pull: This command downloads an image from a registry, such as Docker Hub. You can specify the image name and tag you want to download. For example, docker pull ubuntu:latest will download the latest Ubuntu image.
- docker push: This command uploads an image to a registry. You must first tag the image with the registry name before pushing it. For example, docker tag my-image my-registry/my-image:tag will tag the image with the name "my-registry/my-image:tag", and docker push my-registry/my-image:tag will upload the image to the registry.
- docker exec: This command runs a command inside a running container. You can specify the container ID or name and the command you want to run. For example, docker exec my-container ls / will run the ls / command inside the "my-container" container.
docker build: This command builds a new image from a Dockerfile. You can specify the path to the Dockerfile and the name and tag for the new image. For example, docker build -t my-image:tag . will build a new image from the Dockerfile in the current directory and tag it with "my-image:tag".
- docker network: This command manages Docker networks. You can create, list, connect, and disconnect networks using this command.
These are just a few of the many Docker commands available. Docker provides a comprehensive CLI for managing containers, images, and networks, making it easy to develop, package, and deploy applications in a containerized environment.
Conclusion Docker is a powerful tool for developing, packaging, and deploying applications in a containerized environment. It offers several benefits for developers and organizations, including portability, scalability, efficiency, consistency, and security. With its client-server architecture, Docker makes it easy for developers to build and run distributed applications, from a laptop to a production server.
Most frequent used command in docker:-
docker run nginx to run an instance of the NGINX web server inside a Docker container
By running the command "docker run nginx," you are instructing Docker to download the NGINX image from the Docker Hub repository (if it is not already available locally), create a new container based on that image, and start the NGINX server inside the container. The NGINX server will then be accessible on the default HTTP port (port 80) of your host machine, or you can map it to a different port using the appropriate Docker options.
docker run -p 80:80 nginx to run an instance of the NGINX web server inside a Docker container and map the host machine's port 80 to the container's port 80.
"-p 80:80" is a flag that specifies port mapping. It tells Docker to map the host machine's port 80 to the container's port 80. This means that when you access port 80 on the host machine, the requests will be forwarded to the NGINX server running inside the container.
docker run -d -p 80:80 nginx flag that specifies detached mode. It instructs Docker to run the container in the background.
Here's a breakdown of the command:
- "docker run" is the basic Docker command used to create and run a new container.
- "-d" is a flag that specifies detached mode. It instructs Docker to run the container in the background.
- "-p 80:80" is the port mapping flag, as explained in the previous response. It maps the host machine's port 80 to the container's port 80.
- "nginx" refers to the NGINX image name, which Docker will download from the Docker Hub repository if it's not already available locally.
Commands
- To initialize the swarm mode and listen to a specific interface:
Docker swarm init --advertise-addr 10.1.0.2
- To join an existing swarm as a manager node:
Docker swarm join --token<manager-token> 10.1.0.2:2377
- To join a swarm as a worker node:
Docker swarm join --token<worker-token> 10.1.0.2:2377
- To list all nodes in the swarm:
Docker node ls
- To create a service from an image on the existing port and deploy three instances:
Docker service create --replicas 3 -p 80:80 name -webngix
- To list services running in a swarm:
Docker service ls
- To scale a service:
Docker service scale web=5
- To list the tasks of a service:
Docker service ps web
Build
The build command is used for building images from a Docker file. Let’s now check out some of the essential Docker build commands.
Commands
- To build an image from the Docker file and tag it:
Docker build -t myapp :1.0
- To list all the images that are locally stored:
Docker images
- To delete an image from the Docker Store:
Docker rmi alpine: 3.4
Run
The run command is used for creating a container from a specified image. Check out the below-listed run commands.
Commands
- To create and run a command:
Docker run --name container_name docker_image
Flags used:
- -d: To detach a container on start
- -rm: To remove a container once it stops
- -p: To publish the host IP and the host port to the container port
- -v: To define and share the volume across containers
- –read-only: To set to the read-only permission
Ship
Docker gives us the capability of shipping our application container anywhere, on any platform. Let’s check out some commands used for it.
Commands
- To pull an image from the registry:
Docker pull alpine:3.4
- To retag a local image with a new image name:
Docker tag alpine:3.4 myrepo/ myalpine:3.4
- To log in to the registry:
Docker login my.registry.com:8000
- To push an image to the registry:
Docker push myrepo/ myalpine:3.4
Clean up
To prevent wasting resources, we must know how to clean up. In this Docker cheat sheet tutorial, next, a few essential clean-up commands are provided.
Commands
- To clean an unused/dangling image:
Docker image prune
- To remove an image that is not used in a container:
Docker image prune -a
- To prune the entire system:
Docker system prune
- To leave a swarm:
Docker swarm leave
- To remove a swarm:
Docker stack rm stack_name
- To kill all running containers:
Docker kill $ (docker ps -q)
- To delete all stopped containers:
docker rm $(docker ps -a -q)
- To delete all images:
docker rmi $(docker images -q)
Services
Let’s now take a sneak peek at the commands used to view the running services, to run the services, to view all service logs, and to scale the services.
Learn the ins and outs of Docker with our comprehensive Docker tutorial. Sign up now to become a Docker expert!
Commands
To list all services running in a swarm:
Docker service ls
To see all running services:
Docker stack services stack_name
To see all service logs:
Docker service logs stack_name service_names
To scale a service across qualified nodes:
Docker service scale stack_name_service_name= replicas
Interaction with a Container
Let’s check out how to make an interaction with a container in Docker.
Commands
To run a command in a container:
Docker exe -ti container_name command.sh
To follow container logs:
Docker logs -ft container name
To save a running container as an image:
Docker commit -m “commit message” -a “author” container_name username/image_name: tag

Comments
Post a Comment