Docker Basic

Docker -1

·

5 min read

What is docker?

We usually need to containerize our application after building it to run it on any environment irrespective of OS (Mac, Windows, Linux) dependencies. Hence Docker is a set of Paas(platform as a service) products that use OS Level virtualization to deliver software in packages called "Containers". These Containers are isolated from one another and build their libraries, configuration files etc. Containers can talk to each other using defined channels.

What is Container?

Containers are the packaged application along with dependencies, binaries and configuration files.

Docker Terminologies:

  1. Docker image: A Docker image is a read-only template that contains a set of instructions for creating a container. It is built from a Dockerfile and contains all the necessary dependencies and configurations needed to run the application.

  2. Docker container: A Docker container is a running instance of a Docker image. It is an isolated environment that runs the application with its dependencies and configurations.

  3. Dockerfile: A Dockerfile is a text file that contains a set of instructions for building a Docker image. It defines the base image, environment variables, commands to be executed, and other settings needed to create the image.

  4. Docker registry: A Docker registry is a central repository that stores and manages Docker images. The Docker Hub is the most popular public registry, but there are also private registries that can be used to store images within an organization.

  5. Docker-compose: Docker-compose is a tool used to define and run multi-container Docker applications. It allows you to define the services, networks, and volumes needed to run the application in a YAML file.

  6. Docker swarm: Docker swarm is a native clustering and orchestration solution provided by Docker. It allows you to deploy and manage multiple Docker containers across multiple hosts, providing high availability and scalability.

  7. Docker volume: A Docker volume is a way to store data that needs to persist across containers. It allows you to separate the application data from the container and manage it independently.

  8. Docker network: A Docker network is a way to connect Docker containers, allowing them to communicate with each other. It can be used to isolate containers or to provide a common network for multiple containers.

Docker File:

Firstly we need to make a file with the name "Dockerfile". While writing the file, It has a specific structure as follows:

1) FROM [base-image]

  • From statement specifies the fetched base image from some central repositories like docker hub or ECR(Elastic Container Registry) or we can even start building it from SCRATCH.

2) WORKDIR [working_directory]

  • The working directory is to specify a location in the docker container where we need to run the code.

3) RUN [dependencies]

  • Here we need to install dependencies that are needed to run the container code.

4) COPY <source> <destination>

  • Copy is used to transfer any files from the host system to the docker container. COPY is a docker file command that copies files from a local source location to a destination in the Docker container.

5) ADD <source> <destination>

  • Add does the same thing as a copy but supports two more features of moving URL instead of a local file/directory and Extracting tar from the source directory into the destination. ADD command is used to copy files/directories into a Docker image.

6) EXPOSE <Port>

  • This tells Docker your webserver will listen on Port for TCP connections since TCP is the default. For UDP, specify the protocol after the port.

7) ENV <key> <value>

  • It is used to set environment variables within a container.

8) ENTRYPOINT ["command", "arg1", "arg2"]

  • It is used to specify the command that will be executed when a container is started from the image. The specified command can be a script or an executable binary file. Here, the command represents the executable or script to be run and arg1, arg2, etc. represent any command-line arguments to be passed to the command.

9) CMD ["command", "arg1", "arg2"]

  • CMD command in Docker is used to specify the default command and/or arguments to run when a container is started from the image. The difference between CMD and ENTRYPOINT could be CMD has a default executable/code to run hence it is passed to the entry point which is the start of the container.

MultiStage Docker Build:

This is a technique we generally use in the case of frontend and backend applications where each of the apps is packaged and containerized using a docker-compose.yml file.

Docker Commands:

1) Enter into the docker container:

docker exec -it <container ID or name> <command>

The -it option tells Docker to run the command in interactive mode, which allows you to interact with the container's shell.

2) docker build: It is used to build a Docker image from a Docker file. It creates a new image layer for each instruction in the Dockerfile and saves the final image to the local Docker registry.

3) docker run: It is used to start a Docker container from a Docker image. It creates a new container instance based on the image and runs the specified command inside the container.

4) docker ps: List all running Docker containers. It displays information such as the container ID, image name, status, and uptime.

5) docker images: List all available Docker images in the local registry. It displays information such as the image ID, repository, and tag.

6) docker push: It is used to push a Docker image to a remote registry, such as Docker Hub or a private registry like ECR. It uploads the image layers to the registry and makes the image available for others to use.

7) docker pull: It is used to download a Docker image from a remote registry. It retrieves the image layers from the registry and saves them to the local Docker registry.

See more https://medium.com/@ibrahims/all-docker-commands-in-one-place-ec234587ce31