Docker Basic Commands — Part 3

This article contains the basic docker commands for everyday use.

Harsha Nanayakkara
DevopSquare
Published in
3 min readAug 3, 2021

--

In this article, more docker commands are covered including inspect, environment variables and docker logs, etc. Kindly check my other stories on Docker Basics using the links below for more commands.

Please visit here for Docker Basic Commands — Part 1.

Please visit here for Docker Basic Commands — Part 2.

Sometimes when we need to view the underlying configurations (low level details) on a docker image, we need to use commands as below. As depicted below, either image ID (only the first few digits of the image ID can be inserted) or image name could be passed to get the details.

docker image inspect b9e23
docker image inspect nginx

Similarly, a docker container can be inspected using either one of the below command. This is pretty much the same command as above but, without the image keyword. Either docker container ID or name could be used to run the command.

docker inspect b9e23
docker inspect nginx

At times, it might be required to debug docker containers. In such cases we could use the below command to get the logs. Either docker container ID or name could be used to run the command.

docker logs b9e23
docker logs nginx

Docker images can be scanned to assess the security state and identify any vulnerabilities in terms of packages, versions and libraries. Docker Scan runs on Snyk engine. However, as a prerequisite, you need to log in to the Docker Hub prior to scanning. The below command allows you to scan existing Docker images using the image name or ID.

docker scan b9e23
docker scan nginx

Docker creates an isolated network where the containers run. When two containers are deployed in the same network, they can communicate with each other using just the container name. You can view the networks using the below command, where it will show the existing networks. Bridge, host and none are default networks.

docker network lsNETWORK ID     NAME      DRIVER    SCOPE
06c43799a3bf bridge bridge local
f5921affe036 host host local
78fbb1198bae none null local
  1. Bridge: This is the default network a container gets attached to when it is run, if no network has been defined specifically when run. Additionally, this isolates the docker network from the host, therefore if we need to access from outside the container port needs to be mapped to a host port. However, this isolation allows you to run multiple containers on the same port.
  2. Host: This network type eliminates the network isolation between docker host and container. This associates the container to the host network. For instance, if you run a web server on port 3000 in a container, it is automatically accessible on the same port externally without requiring any port mapping as the web container uses the host’s network. However, in this scenario, unlike in a bridge network, you cannot run multiple web containers on the same host on the same port as ports are now common to all containers in the host network.
  3. None: Containers won't be attached to any network and won’t have any access to the external world.

If we need to associate a container with any other network (i.e. host or none) — -network should be passed with the desired network. Below command sets the network to host.

docker run --network=host nginx

Moreover, we could create a new custom network as well. The command is broken into lines for better readability. if --driver flag is not specified, it will be automatically assigned to bridge. As per below command, the network name is custom-network-name. In case if you need containers to talk to each other, it is better to create a custom network and put the desired containers inside since it will be easier to manage.

docker network create \
--driver bridge \
--subnet 172.30.0.0/16 \
custom-network-name

Before wrapping up the article, I will show how to set environment variables when docker container is run. It can be achieved using the -e or —- env in the simplest form.

docker run -e APP_ENVIRONMENT=dev nginx

In the above code sample, APP_ENVIRONMENT is the name of the environment variable. The value is dev.

This concludes the part 3 of Docker basic commands. I will meet you again with more docker commands in the next part.

Stay safe!

--

--

An enthusiastic autodidact who is passionate to gain and freely share knowledge. I would really appreciate your feedback and support!