Docker Basic with the most frequent docker commands!
First Lets go over some basic concepts of Docker.
What is a docker image?
- A docker image is mainly an application binary (a.k.a: setup file).
- An image might also consist of the dependencies of a specific application.
- An image does NOT consist of a Complete Operating System.
- An image does NOT consist of any KERNEL or KERNEL drivers.
(As docker container shares the underline OS KERNEL) - There is also a large Image available with OS, Driver & all.
What is Dockerfile?
- Docker images are created from Dockerfile.
- A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
- Using docker build users can create an automated build that executes several command-line instructions in succession.
What is Docker Compose?
Compose is a tool for defining and running multi-container Docker applications.
With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
What is Docker Data Persistence?
By default data(ex: log files) doesn’t persist when that container no longer exists. But, Docker has two options for containers to store files in the host machine, so that the files are persisted even after the container stops.
we can persist data in Docker in two ways:
docker volumes, docker bind Mounts.
Docker Volumes
- It’s a configuration option for Creating Containers.
- It creates a special location outside of the container file system to store the data.
- we can attach this volume to one or many containers.
- If we remove a container having a docker volume, the data in the docker volume remains as it is.
- docker Volume needs manual Deletion.
Docker Bind Mounts
- It’s a process of mapping a host file or directory to a container file or directory.
- The container will treat the Bind mount file path like a local file path.
- we Can not use bind mounts in the docker file.
we must create it with docker run or with docker-compose.
Docker Commands
Run a Nginx container in detached mode with the host network
docker container run --publish 80:80 --detach --name nginx_for_testing --net=host nginx
Run Mysql With default Password and Storage
docker container run --publish 3306:3306 --detach --name myssqlDB -e MYSQL_RANDOM_ROOT_PASSWORD=true mysql
Run Mysql With user given Password and Storage
docker container run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123321 -v /var/lib/docker/volumes/mysqldb:/var/lib/mysql --name mysqldb mysql
we are using docker bind mounts here.
This line
/var/lib/docker/volumes/mysqldb:/var/lib/mysql ,
is binding the
/var/lib/docker/volumes/mysqldb docker directory to the
/var/lib/mysql which is a server directory.
Get Container List
docker container ls
docker container ls -a
Start a specific Container
docker container start YOUR_GIVEN_CONTAINER_NAME
Get a log of a specific container
docker container logs YOUR_GIVEN_CONTAINER_NAME
Get a log of a specific container in realtime in the command line
docker container logs -f YOUR_GIVEN_CONTAINER_NAME
Get container Resource Usage
docker container top YOUR_GIVEN_CONTAINER_NAME
Check the extra information on a container
docker container inspect YOUR_GIVEN_CONTAINER_NAME
Stop a specific Container
docker container stop YOUR_GIVEN_CONTAINER_NAME
Get all the container status in realtime
docker container stats
Run docker in interactive Mode with Bash
docker container run -it YOUR_GIVEN_CONTAINER_NAME bash
Check if one container is accessible from another
docker container exec -it YOUR_GIVEN_CONTAINER_NAME ping YOUR_GIVEN_CONTAINER_NAME
Remove a specific Container
docker container rm -f YOUR_GIVEN_CONTAINER_NAME
Run container with a specific name & alias from a Image
docker container run -d --net NETWORK_NAME --net-alias NETWORK_ALIAS_NAME IMAGE_NAME
Check all the DNS entries of a specific NETWORK_ALIAS_NAME
docker container run --rm --net NETWORK_NAME alpine nslookup NETWORK_NAME
Get the list of all image
docker images ls
Show the history of an image
docker image history IMAGE_NAME:IMAGE_TAG
Inspect a docker Image
docker image inspect IMAGE_NAME
Tag an image
docker image tag SOURCE_IMAGE_NAME TARGET_IMAGE_NAME
Build an image with Custom Name in the current directory
docker image build -t YOUR_GIVEN_CUSTOM_NAME
Remove Docker Image
docker rmi IMAGE-NAME
Inspect the default Bridge Interface
docker network inspect bridge
Create a New Network
docker network create YOUR_GIVEN_NETWORK_NAME
Add a network to the container
docker network connect YOUR_GIVEN_NETWORK_NAME YOUR_GIVEN_CONTAINER_NAME
Remove a network from a container
docker network disconnect YOUR_GIVEN_NETWORK_NAME YOUR_GIVEN_CONTAINER_NAME
Build Image from a docker file
docker build -f PATH_TO_DOCKERFILE
See the Space used by the docker container & images
docker system df
Get all the created docker volume
docker volume ls
Create a new docker volume
docker volume create YOUR_GIVEN_VOLUME_NAME
inspect a docker volume
docker volume inspect YOUR_GIVEN_VOLUME_NAME
Among many others, these are the few most frequently used docker commands.
You can check the docker official Site or the docker cheat sheet for more in-depth information.