Explaining Docker Commands in detail

Rinkesh Patidar
4 min readMar 29, 2023

--

What is Docker?

Docker is an open-source platform for developing, shipping, and running applications.

Docker containers are lightweight, portable, and self-sufficient, allowing developers to package their applications and dependencies into a single, reproducible unit that can be easily deployed across different environments.

you can get a detailed explaination about docker here:

theSorcerer

Creating a Dockerfile:

A Dockerfile is a text file that contains instructions for building a Docker image. The content of a Dockerfile typically includes:

# vim Dockerfile

FROM ubuntu

RUN apt update

RUN apt install –y apache2

RUN apt install –y apache2-utils

RUN apt clean

EXPOSE 80

CMD [“apache2ctl”, “-D”, “FOREGROUND”]

This Dockerfile is used to create a Docker image based on the Ubuntu operating system that runs an Apache web server.

  • Here is a step-by-step explanation of what each line does:

FROM ubuntu — This line specifies the base image to use for the Docker container, which is Ubuntu in this case.

RUN apt update — This line updates the package index for the Ubuntu operating system.

RUN apt install –y apache2 — This line installs the Apache web server on the Ubuntu operating system.

RUN apt install –y apache2-utils — This line installs the Apache utility programs on the Ubuntu operating system.

RUN apt clean — This line cleans up the package cache to reduce the size of the Docker image.

EXPOSE 80 — This line specifies that the container will listen on port 80 for incoming requests.

CMD [“apache2ctl”, “-D”, “FOREGROUND”] — This line specifies the command to run when the container is started, which in this case is the Apache control program that will run in the foreground. The -D option specifies the Apache server should run in the foreground and not as a background process.

After creating the Dockerfile, we will run the following commands on the location where the Dockerfile exists to build the image and run the container:

# docker build -t myimage .

“docker build” is the command to build a Docker image.

“-t myimage” specifies the name of the Docker image to be built, which in this case is “myimage”.

“.” specifies the build context, which is the directory that contains the Dockerfile and any other files needed to build the image. In this case, the “.” represents the current directory, meaning that Docker will look for the Dockerfile in the current directory.

Run the following command to check if the image is formed or not:

# docker images

Then we’ll run the following command to form, a container using the image that we formed earlier:

# docker run -d -p 80:80 myimage

In this command,

“docker run” is used to run a Docker container.

The “-d” flag tells Docker to run the container in detached mode, which means that it will run in the background.

The “-p 80:80” flag tells Docker to map port 80 of the host machine to port 80 of the container. This allows incoming traffic to reach the container through port 80 of the host machine.

Now we will check if the container is running or not using the following command:

# docker ps

Now the container is running, we can hit our IP to see the apache web server running on port number 80.

We can now execute the following command to get the shell of the container:

# docker exec -it containerID /bin/bash

It will give you the /bin/bash shell

In the shell, you can configure whatever you want and then press ctrl+p ctrl+q to exit the shell and run the docker comit command to save the changes permanently in the image.

# docker commit containerID myimage:tag

  • The following steps are to start or stop the container, delete images andset an entry point command int the docker container:

# docker ps -a

This command shows all the existing containers. You can give the following command to start a container:

# docker start containerID

And you can stop the running container by following container:

# docker stop containerID

Use this command to see all the existing images:

# docker images

To forcefully delete an existing docker image, you can use the following command:

# docker image rm -f imagename:tag

Here’s a breakdown of the command:

“docker”: This is the command-line interface tool for Docker.

“image rm”: This subcommand is used to remove one or more Docker images from your system.

“-f”: This option is used to force the removal of the specified image, even if it is currently being used by one or more containers.

“imagename”: This is the name or ID of the Docker image that you want to remove.

  • You can use the following command to delete all the existing images on your machine:

# docker image rm -f $(docker images -aq)

--

--