
Introduction to Docker
Docker is a set of platforms as a service (PaaS) products that use the Operating system level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and therefore use fewer resources than a virtual machine.
Difference between Docker Containers and Virtual Machines
1. Docker Containers
[if !supportLists]· [endif]Docker Containers contain binaries, libraries, and configuration files along with the application itself.
[if !supportLists]· [endif]They don’t contain a guest OS for each container and rely on the underlying OS kernel, which makes the containers lightweight.
[if !supportLists]· [endif]Containers share resources with other containers in the same host OS and provide OS-level process isolation.
2. Virtual Machines
[if !supportLists]· [endif]Virtual Machines (VMs) run on Hypervisors, which allow multiple Virtual Machines to run on a single machine along with its own operating system.
[if !supportLists]· [endif]Each VM has its own copy of an operating system along with the application and necessary binaries, which makes it significantly larger and it requires more resources.
[if !supportLists]· [endif]They provide Hardware-level process isolation and are slow to boot.
Important Terminologies in Docker
1. Docker Image
[if !supportLists]· [endif]It is a file, comprised of multiple layers, used to execute code in a Docker container.
[if !supportLists]· [endif]They are a set of instructions used to create docker containers.
2. Docker Container
[if !supportLists]· [endif]It is a runtime instance of an image.
[if !supportLists]· [endif]Allows developers to package applications with all parts needed such as libraries and other dependencies.
3. Docker file
[if !supportLists]· [endif]It is a text document that contains necessary commands which on execution helps assemble a Docker Image.
[if !supportLists]· [endif]Docker image is created using a Docker file.
4. Docker Engine
[if !supportLists]· [endif]The software that hosts the containers is named Docker Engine.
[if !supportLists]· [endif]Docker Engine is a client-server based application
[if !supportLists]· [endif]The docker engine has 3 main components:
[if !supportLists]· [endif]Server: It is responsible for creating and managing Docker images, containers, networks, and volumes on the Docker. It is referred to as a daemon process.
[if !supportLists]· [endif]REST API: It specifies how the applications can interact with the Server and instructs it what to do.
[if !supportLists]· [endif]Client: The Client is a docker command-line interface (CLI), that allows us to interact with Docker using the docker commands.
5. Docker Hub
[if !supportLists]· [endif]Docker Hub is the official online repository where you can find other Docker Images that are available for use.
[if !supportLists]· [endif]It makes it easy to find, manage, and share container images with others.
[if !supportLists]· [endif]
Docker installation
[if !supportLists]1. [endif]Remove old version of Docker
$ sudo apt-get remove docker docker-engine docker.io containerd runc
2. Installing Docker Engine
$ sudo apt-get update
$ sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
$ echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
Check if docker is successfully installed in your system
$ sudo docker run hello-world
Create an application in Docker
1. Create a folder with 2 files (Dockerfile and main.py file) in it.
[if !supportLists]· [endif]Dockerfile
[if !supportLists]· [endif]main.py
3. Edit main.py with the below code.
Python3
|
3. Edit Dockerfile with the below commands.
FROM python:latest
COPY main.py /
CMD [ "python", "./main.py" ]
4. Create a Docker image.
Once you have created and edited the main.py file and the Dockerfile, create your image to contain your application.
$ sudo docker build -t python-test .
The ‘-t’ option allows to define the name of your image. ‘python-test’ is the name we have chosen for the image.
5. Run the Docker image docker run -it --name Docker ubuntu /bin/bash
Once the image is created, your code is ready to launch.
$ sudo docker run python-test
Push an image to Docker Hub
1. Create an Account on Docker Hub.
2. Click on the “Create Repository” button, put the name of the file, and click on “Create”.
3. Now will “tag our image” and “push it to the Docker Hub repository” which we just created.
Now, run the below command to list docker images:
$ docker images
The above will give us this result
REPOSITORY TAG IMAGE_ID CREATED SIZE afrozchakure/python-test latest c7857f97ebbd 2 hours ago 933MB
Image ID is used to tag the image. The syntax to tag the image is:
docker tag <image-id> <your dockerhub username>/python-test:latest
$ docker tag c7857f97ebbd afrozchakure/python-test:latest
4. Push image to Docker Hub repository
$ docker push afrozchakure/python-test
Fetch and run the image from Docker Hub
1. To remove all versions of a particular image from our local system, we use the Image ID for it.
$ docker rmi -f af939ee31fdc
2. Now run the image, it will fetch the image from the docker hub if it doesn’t exist on your local machine.
$ docker run afrozchakure/python-test
Conclusion:
So you have learned about the basics of Docker, the difference between Virtual Machines and Docker Containers along some common terminologies in Docker. Also, we went through the installation of Docker on our systems. We created an application using Docker and pushed our image to Docker Hub. Lastly, we learned how we could remove a particular image from our local system and later pull the image from Docker Hub if it doesn’t exist locally.
Feature of Docker
Docker is one of the most popular open-source sets of platforms for developing and automating the deployment of applications. It deploys applications into containers and enables us to separate our applications from infrastructure. It is designed to provide a lightweight and fast environment in which to run our code as well as an efficient workflow to get that code from our laptop to our test environment and then into production.
Architecture of Docker
Features of Docker:
[if !supportLists]· [endif]Open-source platform
[if !supportLists]· [endif]An Easy, lightweight, and consistent way of delivery of applications
[if !supportLists]· [endif]Fast and efficient development life cycle.
[if !supportLists]· [endif]Segregation of duties
[if !supportLists]· [endif]Service-oriented architecture
[if !supportLists]· [endif]Security
[if !supportLists]· [endif]Scalability
[if !supportLists]· [endif]Reduction in size
[if !supportLists]· [endif]Image management
[if !supportLists]· [endif]Networking
[if !supportLists]· [endif]Volume management
Open-Source Platform:
One of the most important aspects of an open-source platform is the ability to choose which technology to use to complete a task. If a user needs a lightweight, clean environment for testing, the Docker engine can be useful for lone developers. Docker Community Edition (docker-ce) is a great way to get started with containers if Docker is already installed on your system and everyone around you is familiar with the Docker toolchain.
Easy Way of Delivery of Applications:
It is one of the most significant Docker features that allows you to rapidly and simply set up the system. Because of this functionality, codes may be input quickly and easily. Because Docker is utilized in a number of places, the infrastructure is not related to the application environment.
Docker architecture
Docker is an open-source project that automates the deployment of applications as movable, independent containers that can run locally or in the cloud. You can divide your applications from your infrastructure with the help of Docker, allowing for quick software delivery and it also allows you to manage your infrastructure in the same ways that you manage your applications.
The number of commands found in docker is very huge in number, but we will be looking at the top commands in docker. To know more about Docker commands refer to Docker Cheat Sheet – Most Important Docker Commands.
Docker Commands
Docker Run
This command is used to run a container from an image. The docker run command is a combination of the docker create and docker start commands. It creates a new container from the image specified and starts that container. if the docker image is not present, then the docker run pulls that.
$ docker run <image_name>
To give name of container
$ docker run --name <container_name> <image_name>
Docker Pull
This command allows you to pull any image which is present in the official registry of docker, Docker hub. By default, it pulls the latest image, but you can also mention the version of the image.
$ docker pull <image_name>
Docker PS
This command (by default) shows us a list of all the running containers. We can use various flags with it.
[if !supportLists]· [endif]-a flag: shows us all the containers, stopped or running.
[if !supportLists]· [endif]-l flag: shows us the latest container.
[if !supportLists]· [endif]-q flag: shows only the Id of the containers.
$ docker ps [options..]
Docker Stop
This command allows you to stop a container if it has crashed or you want to switch to another one.
$ docker stop <container_ID>
Docker Start
Suppose you want to start the stopped container again, you can do it with the help of this command.
$ docker start <container_ID>
Docker rm
To delete a container. By default when a container is created, it gets an ID as well as an imaginary name such as confident_boyd, heuristic_villani, etc. You can either mention the container name or its ID.
Some important flags:
[if !supportLists]· [endif]-f flag: remove the container forcefully.
[if !supportLists]· [endif]-v flag: remove the volumes.
[if !supportLists]· [endif]-l flag: remove the specific link mentioned.
$ docker rm {options} <container_name or ID>
Docker RMI
To delete the image in docker. You can delete the images which are useless from the docker local storage so you can free up the space
docker rmi <image ID/ image name>
Docker Images
Lists all the pulled images which are present in our system.
$ docker images
Docker exec
This command allows us to run new commands in a running container. This command only works until the container is running, after the container restarts, this command does not restart.
Some important flags:
[if !supportLists]· [endif]-d flag: for running the commands in the background.
[if !supportLists]· [endif]-i flag: it will keep STDIN open even when not attached.
[if !supportLists]· [endif]-e flag: sets the environment variables
$ docker exec {options}
Docker Ports (Port Mapping)
In order to access the docker container from the outside world, we have to map the port on our host( Our laptop for example), to the port on the container. This is where port mapping comes into play.
$ docker run -d -p <port_on_host>
<port_on_container> Container_name
So these were the 9 most basic docker commands that every beginner must know. Containerization is a very vast topic but you can start from the very basic commands and by practicing them daily you can master them.
Docker Login
The Docker login command will help you to authenticate with the Docker hub by which you can push and pull your images.
docker login
It will ask you to enter the username and password after that you will authenticate with DockerHub and you can perform the tasks.
Docker Push
Once you build your own customized image by using Dockerfile you need to store the image in the remote registry which is DockerHub for that you need to push your image by using the following command. To know more about How to Push a Container Image to a Docker Repository?
docker push <Image name/Image ID>
Docker Build
The docker build command is used to build the docker images with the help of Dockerfile.
docker build -t image_name:tag .
In the place of image_name use the name of the image you build with and give the tag number and . “dot” represents the current directory.
Docker Stop
You can stop and start the docker containers where you can do the maintenance for containers. To stop and start specific containers you can use the following commands.
docker stop container_name_or_id
Stop Multiple Containers
Instead of stopping a single container. You can stop multiple containers at a time by using the following commands.
docker stop container1 container2 container3
Docker Restart
While running the containers in Docker you may face some errors and containers fails to start. You can restart the containers to resolve the containers by using the following commands.
docker restart container_name_or_id
Docker Inspection
Docker containers will run into some errors in real time to debug the container’s errors you can use the following commands.
docker inspect container_name_or_id
Docker Commit commands
After running the containers by using the current image you can make the updates to the containers by interacting with the containers from that containers you can create an image by using the following commands.
docker commit container_name_or_id new_image_name:tag
Running Commands Inside Docker Container
If you are working on an application inside Docker Container, you might need commands to install packages or access file system inside the Docker Container. Executing commands inside Docker Containers should be easy enough for you since you have to do it multiple times across your development phase. Docker provides you with many ways to execute commands inside the Containers.
In this article, we are going to discuss different ways to execute any type of command inside the Docker Container.
Method 1: Using Bash
You can directly access the bash of the Docker Container and execute commands there. It’s very easy to launch the bash of the Container and you can do so using this command.
sudo docker run -it ubuntu bin/bash
The above command runs an Ubuntu Container and fires up its bash.
Once you have access to the bash, you can start executing any command there. In this example, we will perform an echo command execution.
echo geeksforgeeks
Method 2: Using the Docker exec Command
In order to run a command inside a Docker Container using the exec command, you have to know the Container Id of the Docker Container. You can get the Container Id using the following Command.
sudo docker container ls
or
sudo docker ps -a
Once you have the Container ID, you can use the Docker exec command. But you have to confirm that the Container is running before you can execute the exec command. To start the Container, use this command.
sudo docker start d64b00529582
After that, execute the exec command.
sudo docker exec -it d64b00529582 echo "GeeksforGeeks"
Method 3: By using the Dockerfile
When you are creating a large application, it is always advised that you execute your commands by specifying it inside the Dockerfile. However, you should only include those commands inside the Dockerfile which you want to execute while building the Container. For executing commands on the go, you can use any of the two methods above. To execute commands through Dockerfile, you can specify them using the Docker Run Commands.
FROM ubuntu:latest
RUN echo "geeksforgeeks"
After you have created the above Dockerfile, you can build the images using the Docker build command.
sudo docker build -t sample-image .
You can see that after step 2, “geeksforgeeks” has been printed.
Docker – USER Instruction
By default, a Docker Container runs as a Root user. This poses a great security threat if you deploy your applications on a large scale inside Docker Containers. You can change or switch to a different user inside a Docker Container using the USER Instruction. For this, you first need to create a user and a group inside the Container.
In this article, we are going to use the USER instruction to switch the user inside the Container from Root to the one which we will create. To do so follow the below steps:
Step 1: Create the Dockerfile
You can specify the instructions to create a new user and group and to switch the user both in the Dockerfile. For this example, we will simply create an Ubuntu Image and use the bash with a different user other than the Root user.
FROM ubuntu:latest
RUN apt-get -y update
RUN groupadd -r user && useradd -r -g user user
USER user
In the above dockerfile, we have pulled the base Image Ubuntu and updated it. We have created a new group called user and a new user inside the group with the same name. Using the USER option, we have then switched the user.
Step 2: Build the Docker Image
After creating the Dockerfile, we can now create the Docker Image using the Build command.
sudo docker build -t user-demo .
Step 3: Run the Docker Container
Use the Docker Run command to run the Container.
sudo docker run -it user-demo bash
Step 4: Verify the output
You can now check that the default user and the group have now changed to the one we created in the Dockerfile using the id command.
id
To conclude, in this article we discussed how to use the USER instruction inside the Dockerfile to switch the Docker Container’s default user from Root to another user that we can create using the useradd and groupadd commands.
Is this possible to create a user inside Docker Without creating docker file?
Yes, it is possible to create a user inside a Docker container without creating a Dockerfile. You can achieve this by running a container from an existing image and then using the docker exec command to execute commands inside the running container to create the user.
Here's how you can do it:
Run the container: Start by running a container based on the image of your choice. Replace your_image_name with the name of the Docker image you want to use.docker run -it your_image_name
The -it flags allow you to interact with the container's terminal.
Execute commands inside the container: Once the container is running, you can use the docker exec command to execute commands inside the running container. This allows you to create a new user and set up their environment.
docker exec -it container_id_or_name useradd -m myuser
Replace container_id_or_name with the ID or name of the running container. The useradd command creates a new user named "myuser" inside the container, and the -m flag ensures that the user's home directory is created.
(Optional) Set the default user for the container: If you want to set the default user for the container, you can use the docker exec command to switch to the new user.
docker exec -it container_id_or_name su – myuser
This command switches the default user inside the container to "myuser."
Keep in mind that any changes made inside the running container using docker exec are not persisted when the container stops. If you need to create the same user every time you run a container from a specific image, consider creating a Dockerfile and building a custom image with the user setup, as mentioned in the previous answer. This way, you will have a consistent environment whenever you run the container.
Docker Networking
allows you to create a Network of Docker Containers managed by a master node called the manager. Containers inside the Docker Network can talk to each other by sharing packets of information. In this article, we will discuss some basic commands that would help you get started with Docker Networking.
Docker Networking allows you to create a Network of Docker Containers managed by a master node called the manager. Containers inside the Docker Network can talk to each other by sharing packets of information. In this article, we will discuss some basic commands that would help you get started with Docker Networking.
Docker Networking
A network is a group of two or more devices that can communicate with each other either physically or virtually. The Docker network is a virtual network created by Docker to enable communication between Docker containers. If two containers are running on the same host they can communicate with each other without the need for ports to be exposed to the host machine. You may use Docker to manage your Docker hosts in any platform manner, regardless of whether they run Windows, Linux, or a combination of the two.
[if mso & !supportInlineShapes & supportFields]><span style='color:black;mso-color-alt:windowtext'><span style='mso-element:field-begin; mso-field-lock:yes'></span><span style='mso-spacerun:yes'> </span>SHAPE <span style='mso-spacerun:yes'> </span>\* MERGEFORMAT <span style='mso-element:field-separator'></span></span><![endif][if mso & !supportInlineShapes & supportFields]><span style='color:black;mso-color-alt:windowtext'><v:shape id="_x0000_i1068" type="#_x0000_t75" style='width:23.9pt;height:23.9pt'> <v:imagedata croptop="-65520f" cropbottom="65520f"/> </v:shape><span style='mso-element:field-end'></span></span><![endif]
Network Drivers
There are several default network drivers available in Docker and some can be installed with the help of plugins, Command to see the list of containers in Docker mentioned below.
docker network ls
Types of Network Drivers
[if !supportLists]1. [endif]bridge: If you build a container without specifying the kind of driver, the container will only be created in the bridge network, which is the default network.
[if !supportLists]2. [endif]host: Containers will not have any IP address they will be directly created in the system network which will remove isolation between the docker host and containers.
[if !supportLists]3. [endif]none: IP addresses won’t be assigned to containers. These containments are not accessible to us from the outside or from any other container.
[if !supportLists]4. [endif]overlay: overlay network will enable the connection between multiple Docker demons and make different Docker swarm services communicate with each other.
[if !supportLists]5. [endif]ipvlan: Users have complete control over both IPv4 and IPv6 addressing by using the IPvlan driver.
[if !supportLists]6. [endif]macvlan: macvlan driver makes it possible to assign MAC addresses to a container.
Launch a Container on the Default Network
1. Understanding the Docker Network Command
The Docker Network command is the main command that would allow you to create, manage, and configure your Docker Network. Let’s see what the sub-commands can be used with the Docker Network command. to know more about Creating a Network in Docker and Connecting a Container to That Network.
sudo docker network
We will see all the Network sub-commands one by one.
2. Using Docker Network Create command
With the help of the “Create” command, we can create our own docker network and can deploy our containers in it.
sudo docker network create --driver <driver-name> <bridge-name>
3. Using the Docker Network Connect command
Using the “Connect” command, you can connect a running Docker Container to an existing Network.
sudo docker network connect <network-name> <container-name or id>
In this example, we will connect an Ubuntu Container to the Bridge Network we created in the last step.
4. Using the Docker Network Inspect command
Using the Network Inspect command, you can find out the details of a Docker Network.
sudo docker network inspect <network-name>
You can also find the list of Containers that are connected to the Network.
5. Using the Docker Network ls command
To list all the Docker Networks, you can use the list command.
sudo docker network ls
6. Using the Docker Network Disconnect command
The disconnect command can be used to remove a Container from the Network.
sudo docker network disconnect <network-name> <container-name>
7. Using the Docker Network rm command
You can remove a Docker Network using the rm command.
sudo docker network rm <network-name>
Note that if you want to remove a network, you need to make sure that no container is currently referencing the network.
8. Using the Docker Network prune command
To remove all the unused Docker Networks, you can use the prune command.
sudo docker network prune
To know more about Docker-published ports refer to this article Docker – Managing Ports
Common Operations
[if !supportLists]· [endif]docker network inspects: We may examine the configuration information of a specific network, such as the name of the network, the containers that have linked to this network, the type of driver used to construct this network, and other characteristics, by using the “docker network inspect” command.
[if !supportLists]· [endif]docker network ls: We can see all of the networks that are available on the current host by using “docker network ls”.
[if !supportLists]· [endif]docker network creates: Using the command “docker network create” and the name of the driver, such as bridge, overlay, or macvlan, we can establish a new network.
[if !supportLists]· [endif]docker network connects: In order to use this command, we must first confirm that the appropriate network has already been formed on the host. Then, using docker “network connect”, we may attach the container to the necessary network.
Conclusion
Only with the aid of the IP address, gateway, routing table, DNS service, and any other networking information can the container learn what network interface is being used. The type of network associated with a container is unknown; it might be any driver, such as “bridge, host, and null.”
Docker – Managing Ports
Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. These containers may need to talk to each other or to services outside docker, for this we not only need to run the image but also expose the container’s port and make it accessible by other services or containers which are on a different network.
Note: If you need to run docker as the root user, please remember to prepend sudo to the commands.
Let us now see how we manage ports in Docker.
Pulling Image From Docker Hub:
Docker Hub is a hosted repository service provided by Docker for finding and sharing container images. You can host your own images on Docker Hub, but we will use the official Nginx repository to pull images of Nginx which is an open-source reverse proxy server. An official image on docker has an official image tag.
This repository has all the information regarding the Nginx image and how we can use it. On the top right corner, we see a docker pull command specifying how to pull the image to our computer.
Step 1. Use the command to pull the Nginx image.
$ docker pull nginx
Output:
Running A Container From The Pulled Image:
When we run a container using the docker run command, it does not publish any of its ports to the outside world. To expose our container port to services outside of Docker, or to containers that are not in the same network as our container, use the –publish or -p flag.
A typical example of –publish flag:
$ docker run -p 8080:80 --name webhost -d nginx
Let us break this command to understand better:
[if !supportLists]1. [endif]Docker run Nginx: will start the container from the Nginx image.
[if !supportLists]2. [endif]-p 8080:80: will map TCP port 80 in the container to port 8080 on the Docker host.
[if !supportLists]3. [endif]–name webhost: will name our container webhost, if not specified docker will give it a random name.
[if !supportLists]4. [endif]-d: flag will run docker in a detached mode that is, in the background.
How To Know Which Port Is Exposed?
The one question which still remains is, how will we know which port in the container should we map our computers port to? Usually, this detail is provided on the image repository or using the inspect command.
[if !supportLists]· [endif]Go to the Nginx repository on the docker hub and try to locate the expose port section:
[if !supportLists]· [endif]Using the docker inspect command:
$ docker inspect nginx
This will list all configurations of the Nginx image, from where we can locate the ExposedPort property.
Step 2: This data can be filtered out using the –format flag:
$ docker inspect --format="{{.ContainerConfig.ExposedPorts}}" nginx
Step 3: Now that we know for sure that Nginx runs on TCP port 80 inside the container’s virtual network. We can use the docker run command.
$ docker run -p 8080:80 --name webhost -d nginx
Step 4: Verify if Nginx is running on port 8080:
Creating And Exposing Local Containers:
Well, this was easy, let us create a local docker image for a react app and publish its port:
Step1: For this, I am using a simple react application. Let us initialize the react application first:
$ npx create-react-app gfg
If you don’t have NodeJs installed you can clone a react app project from GitHub.
Step 2: Create A Dockerfile file in the root directory of the project. Create a Dockerfile named file in your project’s root directory and paste this:
# pull official base image
FROM node:13.12.0-alpine
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent
RUN npm install react-scripts@3.4.1 -g --silent
# add app
COPY . ./
# can be skipped
EXPOSE 3000
# start app
CMD ["npm", "start"]
Step 3. Build An Image.
$ docker build -t basic-react-app .
Step 4. Run the Image:.$ docker inspect –format=”{{.ContainerConfig.ExposedPorts}}” basic-react-app
$ docker run -p 3000:3000 -d basic-react-app
This step will need a little explanation. Ours react app runs on port 3000 inside the container -p 3000:3000 maps this port to the host port.
Step 5. Verify in your browser
Docker Images
Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how. Docker Container is a virtual environment that bundles application code with all the dependencies required to run the application. The application runs quickly and reliably from one computing environment to another.
Uses of Docker Images
[if !supportLists]1. [endif]We can easily and effectively run the containers with the aid of docker images.
[if !supportLists]2. [endif]All the code, configuration settings, environmental variables, libraries, and runtime are included in a Docker image.
[if !supportLists]3. [endif]Docker images are platform-independent.
[if !supportLists]4. [endif]Layers are the building blocks of an image.
[if !supportLists]5. [endif]WithWhen using the build command, the user has the option of completely starting from scratch or using an existing image for the first layer.
Difference between Docker Image and Docker Container
Docker image | Docker container |
The Docker image is the Docker container’s source code. | The Docker container is the instance of the Docker image. |
Dockerfile is a prerequisite to Docker Image. | Docker Image is a pre-requisite to Docker Container. |
Docker images can be shared between users with the help of the Docker Registry. | Docker Containers cant be shared between the users. |
To make changes in the docker image we need to make changes in Dockerfile. | We can directly interact with the container and can make the changes required. |
Structure Of Docker Image
The layers of software that make up a Docker image make it easier to configure the dependencies needed to execute the container.
[if !supportLists]· [endif]Base Image: The basic image will be the starting point for the majority of Dockerfiles, and it can be made from scratch.
[if !supportLists]· [endif]Parent Image: The parent image is the image that our image is based on. We can refer to the parent image in the Dockerfile using the FROM command, and each declaration after that affects the parent image.
[if !supportLists]· [endif]Layers: Docker images have numerous layers. To create a sequence of intermediary images, each layer is created on top of the one before it.
[if !supportLists]· [endif]Docker Registry: Refer to this page on the Docker Registry for further information.
How To Create A Docker Image And Run It As Container?
Follow the below steps to create a Docker Image and run a Container:
Step 1: Create a Dockerfile.
Step 2: Run the following command in the terminal and it will create a docker image of the application and download all the necessary dependencies needed for the application to run successfully.
docker build -t <name>:<tag>
This will start building the image.
Step 3: We have successfully created a Dockerfile and a respective Docker image for the same.
Step 4: Run the following command in the terminal and it will create a running container with all the needed dependencies and start the application.
docker run -p 9000:80 <image-name>:<tag>
The 9000 is the port we want to access our application on. 80 is the port the container is exposing for the host to access.
Docker Image commands
List images
docker ls
Example:
$ docker ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 0d9c6c5575f5 4 days ago 126MB
ubuntu 18.04 47b199b0cb85 2 weeks ago 64.2MB
Pull an image from a registry
docker image pull <image-name>
Example:
$ docker pull alpine:3.11
3.11: Pulling from library/alpine
Digest: sha256:9f11a34ef1c67e073069f13b09fb76dc8f1a16f7067eebafc68a5049bb0a072f
Status: Downloaded newer image for alpine:3.11
Remove an Image from Docker
docker rmi <id-of-image>
Example:
$ docker rmi <image_id>
Untagged: <image_id>
Deleted: sha256:<image_id>
Searching for a specific image on Docker Hub
docker search ubuntu
Example:
$ docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating s… 4458 [OK]
ubuntu-upstart Upstart is an event-based replacement for … 62 [OK]
tutum/ubuntu Simple Ubuntu docker images with ssh access 49 [OK]
ansible/ubuntu14.04-ansible
Commit your own Docker Customized Image from container
Docker is an open-source container management service and one of the most popular tools of DevOps which is being popular among the deployment team. Docker is mostly used in Agile-based projects which require continuous delivery of the software. The founder, Chief Technical Officer, and Chief Architect Officer of the Docker Open source project is Solomon Hykes. It was launched in 2013 by Dotcloud since then it is the world’s leading software container platform. For more details about containerization using docker and its docker architecture.
How we can create our own customized Docker images and how we can push them to the Docker hub profile? It is good practice to push your images to the docker hub profile as you don’t have to create it again and you can pull those images in your system as well as in the cloud with all your work saved in it.
Creating docker images is not a tedious task. We can create a docker image easily with a few commands. There are two ways of creating a docker image depending upon the purpose for which you want to create the image. The first method is using the commit command and another method is by using the Dockerfile concept. Know more details about the components of Docker ie, Docker images, and Docker File.
Now let’s start creating our own customized docker image using the commit command. Before going with our own docker image we should first set up and configure docker in our operating system. To learn more about how to set up docker you can refer to how to install docker. After successful installation let’s learn some of the docker commands.
Docker Commit
A new image can be produced using the Docker commit command based on modifications made to an existing container. It is a practical technique to generate a fresh image that incorporates any adjustments made to a container, like adding new packages or changing files.
Note: Using docker commit we can create an image from the container.
Syntax:
docker commit <containerId/Name> <imageName>
Steps for Committing Changes to Docker Image
Now we will create our own image from the existing alex43/ubuntu-with-git:v1.0 image and we will customize it with our needs and we will upload it.
Commit a Container
Step 1: Pull a Docker Image
The very first step is to pull the image as shown in the below image. Use the command and pull the image into your system as shown below.docker pull alex43/ubuntu-with-git:v1.0
Step 2: Deploy The Container
After pulling the image run the container by using the below command where The “-it” flag instructs Docker to create an interactive bash shell in the container by allocating a pseudo-TTY linked to the container’s stdin. The command opens a new container and moves you to a fresh shell prompt so you can start working inside of it.
docker run -it <Imagename/ImageID> bin/bash
Step 3: Modify The Container
Know we are in the container we can install the required package or modify the image here we will try to install Nmap Software. Check whether the software has already been installed before you start installing it. with the following command.
nmap --version
To install the Nmap use the following command.
apt-get install nmap
Once the installation is complete, confirm once more that the software was installed as shown in the example below. And exit from the container.
Step 4: Commit Changes to The Image
Lastly, commit the changes by using the syntax shown below to produce a new image.us the container ID and tag the new image with a new tag.
sudo docker commit [CONTAINER_ID] [new_image_name]
Execute the docker image ls after committing the image. We can see our recently committed image in this list.
Additional Options for Docker Commit Command
The first command is the pull command. This command will download/pull the complete operating system within seconds depending on your internet connectivity. The syntax is like, docker pull image_name. Here I am pulling alex43/ubuntu-with-git:v1.0 which is my own customized image.
docker pull alex43/ubuntu-with-git:v1.0
The second command is the run command which we will use to run the pulled image. This command will launch my image and we will get an interactive shell/terminal of that image. The syntax is like this -it is for an interactive terminal, –name to give the reference name for my image launched, and then my image_name.
docker run -it --name myos alex43/ubuntu-with-git:v1.0
The third command and the most important command for creating our own image is the commit command. By using this command we can simply create our own image with the packages which we want from the existing image. The syntax is like, docker commit Nameof_RunningImage your_own_name: tag.
docker commit myos ubuntu-basicbundle:v1.0
The fourth command is the tag command. By using this command we need to rename our image with the syntax username/image-name:tag. Before executing this command you need to create an account on the Docker hub and you have to give the same username which you have given in the Docker hub profile.
docker tag alex43/ubuntu-with-git:v1.0 alex43/ubuntu-basicbundle:v1.0
The fifth command is the login command. By using this command we will log in to the docker hub account through our terminal and it is required to upload our docker image to the docker hub profile.
docker login --username alex43 --password your_passwd
The fifth command is the push command. By using this command we can upload our own created docker image to the docker hub profile and can use it anywhere from our local system to the cloud by pulling it.
docker push alex43/ubuntu-basicbundle:v1.0
So these were the few commands with the concept which we will be using in this tutorial and I will be uploading one fresh image so that you guys can understand it in a better way.
When to Commit New Changes to a New Container Image
By committing new changes to a new container image it will be useful in the containerization process where you can make an image from the changes we have done to a container. The timing of when to commit a new image depends upon a few factors:
[if !supportLists]1. [endif]Modifications are finished: Be sure that the modifications you’ve made are complete and function as intended before committing new changes to a container image. You can end up with an image that doesn’t perform properly or needs additional adjustments if you commit insufficient changes.
[if !supportLists]2. [endif]Consistency of Changes: It’s crucial to make sure that the changes you’ve made to the container are stable and won’t result in any problems when they’re deployed. Test the container rigorously to confirm that it performs as expected before making modifications to an image.
[if !supportLists]3. [endif]Frequency of Changes: Committing changes to a fresh container image more regularly may make sense if you frequently modify the container. This can lessen the chance of needing to roll back modifications if problems develop and ensure that each new version of the container reflects the most recent changes.
In conclusion, only commit fresh changes to a fresh container image once they have been fully finished, stable, and properly tested. When to commit new changes to an image depends on the frequency of changes and your deployment workflow.
Conclusion In this post, we’ve discussed the significance of the docker commits command and provided step-by-step instructions with an example of how to use it. Docker commit is mainly used to commit the image from the running container in which we have done some modifications like installing some software or adding any variables in the container.
Docker File components
A Dockerfile is a text file that contains a set of instructions for building a Docker image. The instructions in a Dockerfile are executed in order, and each instruction creates a layer in the image. The layers are stacked on top of each other, and the final image is a collection of all the layers.
The following are some of the most common components of a Dockerfile:
FROM: This instruction specifies the base image from which the new image will be built.
RUN: This instruction executes a command in the image. The command can be used to install packages, configure the environment, or run any other arbitrary command.
CMD: This instruction specifies the command that will be run when the container starts.
ENTRYPOINT: This instruction is similar to CMD, but it allows you to specify the command that will be run as an executable.
COPY: This instruction copies files from the host machine to the image.
ADD: This instruction is similar to COPY, but it can also download files from the internet.
ENV: This instruction sets an environment variable in the image.
WORKDIR: This instruction sets the working directory for the RUN, CMD, and ENTRYPOINT instructions.
Example 1 : These are just some of the most common components of a Dockerfile. There are many other instructions that can be used, and the specific instructions that you use will depend on the application that you are building.
Here is an example of a simple Dockerfile:
On your machine vi Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get install -y nginx
CMD ["nginx", "-g", "daemon off;"]
This Dockerfile will create an image based on the Ubuntu latest image. The RUN instruction will install the nginx web server, and the CMD instruction will start nginx in daemon mode.
Example 2: Create a Dockerfile:
Open a text editor and create a file named "Dockerfile" (no file extension) with the following content:
Dockerfile
# Use a base image from Docker Hub
FROM ubuntu:latest
# Set the working directory inside the container
WORKDIR /app
# Copy files from your local machine to the container
COPY . /app
# Install any necessary dependencies (e.g., if you have a script that requires certain packages)
# RUN apt-get update && apt-get install -y <package_name>
# Define the command to run your application (replace "your_script.py" with your actual script name)
CMD ["python", "your_script.py"]
Build the Docker image:
Open a terminal/command prompt, navigate to the directory where you saved the Dockerfile and your application files, and run the following command:
docker build -t my_app_image .
This command will build a Docker image named "my_app_image" based on the instructions provided in the Dockerfile. Don't forget the dot (".") at the end, as it refers to the current directory containing the Dockerfile.
Run the Docker container:
After the image is successfully built, you can run a Docker container based on this image with the following command:
docker run my_app_image
Replace "my_app_image" with the name you used when building the image. This command will start a container using the specified image, and it will execute the command defined in the Dockerfile (e.g., running your Python script).
Docker volumes
Mounting a Volume Inside Docker Container
When you are working on a micro-service architecture using Docker Containers, you create multiple Docker Containers to create and test different components of your application. Now, some of those components might require sharing files and directories. If you copy the same files in all the Containers separately, it might lead to an unnecessary increase in the Image size, and also, changing a file in one Container will not create the corresponding change in the same file in other Containers.
Hence, you required a shared directory or volume that you can mount on multiple Docker Containers and all of them have shared access to a particular file or directory. Docker allows you to mount shared volumes in multiple Containers. In this article, we will mount a volume to different Containers and check whether the changes in the file is shared among all the Containers or not. Follow the below steps to mount a volume inside Docker Container:
Step 1: Display all the existing Docker Volumes
To display all the existing Docker Volumes, you can use the list command as follows.
sudo docker volume ls
Volume List
Step 2: Creating a Volume
To create a new Docker Volume, you can use the Volume Create Command.
When you are working on a micro-service architecture using Docker Containers, you create multiple Docker Containers to create and test different components of your application. Now, some of those components might require sharing files and directories. If you copy the same files in all the Containers separately, it might lead to an unnecessary increase in the Image size, and also, changing a file in one Container will not create the corresponding change in the same file in other Containers.
Hence, you required a shared directory or volume that you can mount on multiple Docker Containers and all of them have shared access to a particular file or directory. Docker allows you to mount shared volumes in multiple Containers. In this article, we will mount a volume to different Containers and check whether the changes in the file is shared among all the Containers or not. Follow the below steps to mount a volume inside Docker Container:
Step 1: Display all the existing Docker Volumes
To display all the existing Docker Volumes, you can use the list command as follows.
sudo docker volume ls
Volume List
Step 2: Creating a Volume
To create a new Docker Volume, you can use the Volume Create Command.
sudo docker volume create geeksforgeeks
Volume Create
Step 3: Inspecting Docker Volumes
To get the details of the Volumes you have created , you can use the Volume Inspect Command.
sudo docker volume inspect geeksforgeeks
Volume Inspect
Step 4: Mounting Docker Volumes
After creating the Volume, the next step is to mount the Volume to Docker Containers. We will create a Docker Container with the Ubuntu base Image and mount the geeksforgeeks Volume to that Container using the -v flag.
sudo docker run -it -v geeksforgeeks:/shared-volume --name my-container-01 ubuntu
The above command mounts the geeksforgeeks volume to a directory called shared-volume inside the Docker Container named my-container-01.
Mounting Docker Volumes
Step 5: Create a file inside the Docker Volume
Inside the bash of the Container, create a new file and add some content.
ls
cd /shared-volume
echo “GeeksforGeeks” > geeksforgeeks.txt
ls
exit
Creating a file
Step 6: Create another Container and Mount the Volume
Create another Docker Container called my-container-02 and mount the same Docker Volume called geeksforgeeks inside the Container.
sudo docker run -it -v geeksforgeeks:/shared-volume --name my-container-02 ubuntu
Verifying Shared Contents
If you go to the shared-volume directory and list the files, you will find the geeksforgeeks.txt file that you had created in the same volume but mounted in my-container-01 earlier and it also has the same content inside it. This is so because, the volume is shared among the two Containers.
To conclude, in this article we discussed how to create and inspect a Volume and mount it to multiple Docker Containers. This proves to be very helpful when you want shared access to files and directories among multiple Docker Containers
Remove All Containers and Images in Docker
Remove Image
To delete the image by the ImageId/Name we can use the following command. To know more about how to build a docker image with the help of Dockerfile refer to Concept of Dockerfile.
docker rmi <imageId/Name>
Force Remove Image
To force remove the docker Images by the ImageID/Name we can use the following command.
docker rmi -f <imageId/Name>
Note: We can’t remove the images by force or normally while the container is running.
Dangling Images
Dangling Images are those that don’t map to either the repository or the tag. The command used is to remove the dangling images. To know more about how to tag Docker images by referring to Docker image tags.
docker image prune
Removing all Images
We can remove all images in the docker-machine to remove unwanted clutter and space in the system. We can anyways fetch the latest version or specific versioned image from the docker registry or from the cache.
docker rmi $(docker images -q)
How To Delete Containers In Docker
Before deleting the containers we need to stop the container first for that we use the command.
docker stop <containerId/Name>
The Difference Between Docker Stop & Docker Kill
Docker stop will first send a SIGTERM signal before killing the process with a SIGKILL signal and a grace period. When Docker kill sends SIGKILL, it immediately terminates the process.
[if !supportLists]· [endif]Stop all running Containers: In order to stop the containers which have not exited. This might happen when the command used in the Docker image is left running. The command should be exited and this will in turn stop the container. To stop the container when you have not exited the container by stopping the command, you need to run the following command.
docker stop $(docker ps -aq)
[if !supportLists]· [endif]Delete The Container: If the container is stopped then we can use the following command to delete the container.
docker rm <containerId/Name>
[if !supportLists]· [endif]Force Delete The Container: We can force remove the containers while they are running without stopping them by using the below command.
docker rm -f <containerId/Name>
Remove all Containers
To remove all containers from the docker machine, we need to get the ids of all the containers. We can simply get the ids of the containers with the command docker ps -aq, then by using the docker rm command, we can remove all the containers in the docker-machine.
docker rm $(docker ps -aq)
Remove all Stopped Containers
To remove all containers which are stopped/exited, we can use filters in the ps command argument. We can’t directly remove a container if it is not stopped. We can stop containers that are not exited or are running by using the -f argument to the ps command in docker, the -f or –filter option takes in a filter like status=exited or status=running or name and so on. We can filter out to stop the specific containers according to the requirement.
docker rm $(docker ps -aq --filter status="exited")
After filtering out the container which is running, we can use the stop command to stop those containers with the -q to silence the numeric ids associated with those containers.
docker stop $(docker ps --filter status=running -q)
This will stop all the containers and thus we can now remove the containers from the docker-machine. We can even filter the containers which are stopped here to remove only those whose status is exited.
docker rm $(docker ps --filter status=exited -q)
[if !supportLists]· [endif]The below command removed all the containers which are in the existing state. That means the containers stopped.
docker container prune
Docker Compose
Docker Compose: Simplifying Multi-Container Applications
What is Docker Compose? 🤔
Docker Compose emerges as a lifesaver in the realm of managing multi-container applications. It's an orchestration tool that enables you to define, configure, and run multiple Docker containers as a single application using a single command. 🛳️
Imagine this: you're working on a microservices-based application with various interconnected components like databases, APIs, and frontend services. Juggling individual docker run commands for each container can quickly become a nightmare. Docker Compose steps in, allowing you to define your entire application stack in a single YAML file (typically named docker-compose.yml). With a simple command, you can launch your entire application stack effortlessly. ⚙️
YAML: Your Configuration Ally 📝
Before diving deeper, let's understand the backbone of Docker Compose configurations: YAML (Yet Another Markup Language). Despite its name, YAML isn't just another markup language; it's a versatile data serialization language used extensively for configuration files. YAML's human-readable syntax and intuitive structure make it an excellent choice for defining Docker Compose services and their relationships. 🧩
How to write YAML file and why it is used for:
Here's an example of a YAML file that provides details about a smartphone:
In this YAML file, we've structured the smartphone details into various sections, each with its own set of attributes. Let me explain each section:
· model, manufacturer, and release_year provide general information about the smartphone.
· os includes the operating system details, with its name (Android) and version (12.0).
· storage specifies both internal and external storage capacities.
· display contains details about the display technology, size, and resolution.
· camera provides information about the main and front cameras, including resolution and aperture.
· battery includes the battery capacity and whether it's removable.
· connectivity lists various connectivity options like Wi-Fi, Bluetooth, 4G LTE, and NFC.
· features highlight additional features like fingerprint sensors, facial recognition, and fast charging.
This example demonstrates how YAML can be used to structure and represent data in a human-readable format. YAML uses indentation to define the hierarchy and relationships between data elements. It's commonly used for configuration files, data serialization, and other scenarios where structured data needs to be stored or exchanged.
Task-1: Exploring Docker Compose Basics 📦
In this task, we'll familiarize ourselves with Docker Compose basics. Let's set up a basic environment and configure services using the docker-compose.yml file
version: '3.8'
services:
frontend:
image: nginx:latest
ports:
"80:80"
backend:
image: my-backend:latest
depends_on:
database
database:
image: postgres:latest
environment:
POSTGRES_PASSWORD: secret
Here, we define three services: "frontend," "backend," and "database." The "backend" service depends on the "database" service. We've also set the PostgreSQL password as an environment variable.
Task-2: Hands-on with Containers 🛠️
Pulling and Running Images
Now let's dive into practical exercises. We'll pull a pre-existing Docker image from Docker Hub and run it on our local machine.
bashCopy codedocker-compose pull
docker-compose up -d
Inspecting Containers
You can inspect a container's details using the docker inspect command.
docker inspect <container_id>
Viewing Logs
Monitor container logs using the docker logs command.
docker logs <container_id>
Stopping and Starting Containers
Control container lifecycle with docker stop and docker start.
bashCopy codedocker stop <container_id>
docker start <container_id>
Removing Containers
When you're done, remove containers using docker-compose down.
bashCopy codedocker-compose down
Docker Without Sudo: Simplifying Access 🔑
By default, Docker commands require administrative privileges. However, you can grant your user the necessary permissions to run Docker commands without using sudo.
bashCopy codesudo usermod -a -G docker $USER
Remember to reboot your machine after executing this command to apply the changes.

