Skip to content

Important Docker commands – every DevOps Engineer should know

As we know Docker containers are used to run our applications in an isolated environment. Below are few important docker commands like: docker run, docker port, docker volume etc. that are required to operate a docker environment smoothly and efficiently.

1. Important Docker commands: Docker Naming

When we create a new container, docker assign name to the container automatically. We can also assign name to the container that we generate for better container management. Also, we can keep track of the program running in the container.

docker run –name <container name> <image>

With the below command I am running a nginx container named as “myweberver-01”

#docker run –name myweberver-01 nginx

2. Important Docker commands : Environment Variable

To pass additional parameters while running a docker container we can user docker environment variables. For Example: we can adjust the configuration of MySQL instance by passing one or more environment variable on the docker run command line.

#docker run --name  prod-DB  -e DB_Host=mysql-db  -e DB_Password=db_pass123  mysql:latest

3. Docker Port

We can access a running service inside container port by mapping it to a docker host port. For Example: with below command we can access port 80 of nginx container by mapping it to port no 8080 of docker host port.

#docker run --name my-web -p 8080:80 nginx

#docker ps
docker port
3.1 – show all mapped port of a container.
#docker port <container name>
#docker port my-web

4. Docker Volume

All the changes we make inside a container like creating files and directories or any type of data are lost when we stop and delete the container. To ensure data persistence docker volumes are used with docker containers. Data kept in a volume are accessible even after the container stopped and deleted.

The volumes are stored on the docker host and is independent of container life cycle. The data can be backed up and shared amount containers very easily.

Docker engine has two options to store files that are persisted in nature on the host machine.

  • Volumes are the preferred mechanism for persisting data generated and used by Docker containers. volumes are completely managed by Docker.
  • Bind mounts can stored date anywhere on the host filesystem.
Docker Volume

If container generates non-persistent state data, we should use tempfs to avoid storing the data permanently. It also increases the container’s performance by avoiding writing into the container’s writable layer.

4.1 – creating docker volume
#docker volume create DB_Data_Vol
4.2 – List docker volumes
# docker volume ls

This volume is created on docker host path: “/var/lib/docker/volumes

4.3 – Inspect Docker Volume
#docker inspect volume <volume_name>
#docker inspect volume DB_Data_Vol
4.4 – Remove a docker volume.
# docker volume rm DB_Data_Vol
4.5 – Remove all unused local volumes
#docker volume prune
4.6 – Starting a container with a volume
#docker run -v webData:/opt/prodWebData --name uatWebServer nginx

The above command creates a docker volume inside the directory of docker host and also mount it inside the directory of container. we can verify with below commands

Go inside the running container

4.7 – mount a file system of docker host inside container.

mount a file system of docker host inside container. The source directory should be available on the host.

#docker run --mount 'type=bind,src=/testDBManab11/,dst=/testDBContainer111' --name=testWEB111 nginx

Read more about Docker volume: Link

5. Docker Network

Docker networking is used to communicate between docker containers and the outside world through docker host. When we install docker on a host machine a default bridge network is assigned to the host machine named “docker0”

The Ip address “” is assigned to host system and the container will get the Ip range from

docker comes with 5 built in network drivers that implement networking functionality.

  1. bridge – This is default network driver. This is good for running containers that requires to communicate with other containers on the same host
  2. host: This shares the host’s network with the container, means container’s network isn’t isolated from host network.
  3. none: completely isolate a container from the host and other containers.
  4. overlay: This is best when containers running on different Docker Hosts need to communicate, or when multiple applications work together using swarm service.
  5. ipvlan : It is similar to Macvlan, but doesn’t assign unique MAC addresses to containers.
  6. macvlan : These networks allow to assign a MAC address to a container, making it appear as a physical device on network. The Docker daemon routes traffic to containers by their MAC addresses.
5.1 – command to check docker network.

#docker network ls
docker network
Important Docker commands

#docker network inspect bridge.

Important Docker commands
5.2 – List container ip address and network type

#docker run -d nginx

#docker ps

#docker inspect <image-name> | grep -i -A 20 "NetworkSettings"
#docker inspect flamboyant_solomon | grep -i -A 20 "NetworkSettings
5.3 – Create a custom bridge network
# docker network create --driver bridge --subnet --gateway db-prod-network

The above command creates a new bridge network on the docker host. We can verify this wit below command.

# docker network ls

#docker network inspect db-prod-network

5.4 – attach a container to the newly created network

#docker run –network=db-prod-network nginx

5.5 – check container network information

#docker inspect hardcore_wescoff | grep -i -A 20 Networks

5.6 – remove a docker network.

docker network rm <network id>

#docker network rm 4946ddc8a6d2

Also Read: 15 docker commands , install docker on ubuntu, Official docker Documentation