Docker tips and tricks

Docker tips and tricks. In this article I would like to add some commonly using Docker commands, and some tips and tricks. Will start with some basics and add more commands/tricks going forth.

I shared some basics of Linux containerisation technologies and Docker containerisation in our previous discussions. You can check all previous discussions from this link.

Topics yet!!

  1. What is Linux containerisation?
  2. What is the Docker daemon?
  3. What is the Docker client/CLI?
  4. Docker command to list all images on your machine?
  5. What is Docker’s default configuration file?
  6. How to remove a Docker image?
  7. How to remove a multiple Docker images in a single command?
  8. How to build a Docker image?
  9. How to run a Docker container from an image?
  10. How to enter into a running Docker container?
  11. Command to exit from a Docker container without interrupting the container?
  12. Tips to remove all Docker images from host?
  13. Find all information about a Docker image or container using docker inspect.

Lets start docker tips and tricks…

What is Linux containerisation?

Basic: Containerisation helps to isolate processes. You can run your App/Services as an isolated process, running from a distinct image that provides all files necessary to support the processes.

Basically Linux containers are OS level virtualisation technique for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel. Read more…

What is the Docker daemon?

dockerd

dockerd is the daemon for managing Docker things. It listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.

What is the Docker client/CLI?

docker

docker is the command line tool to manage Docker containers. When we run docker command for some operation, this command communicate with docker daemon and pass the information, then, docker daemon manages it.

For example, when you use commands such as docker build, the client sends these commands to dockerd (Docker daemon), which carries them out. The docker command uses the Docker API.

The Docker client can communicate with more than one daemon.

Docker command to list all images on your machine?

docker images
# docker images
REPOSITORY                                          TAG                 IMAGE ID            CREATED             SIZE
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4   latest              67bab6537ca8        7 weeks ago         513MB
wordpress                                           latest              d5668b07b850        7 weeks ago         442MB
haproxy                                             latest              2b6a4216580b        8 weeks ago         74.4MB
docker-redis                                        latest              08a845c3d70f        8 weeks ago         154MB

What is Docker’s default configuration file?

It’s under /etc.

/etc/docker/daemon.json

In Linux, you can manage Docker daemon by the following two ways:

1. Using the command “dockerd”
2. By creating a JSON file under /etc/docker/ directory.

Refer this doc for more details.

How to remove a Docker image?

This can be done by using the switch “rmi” in docker cli command. You can use image name or image id in this command to remove an image. Another Docker tips and tricks.

docker rmi image-name/image-id

Example:

# docker rmi haproxy
Untagged: haproxy:latest
Untagged: haproxy@sha256:2d62770b66eb85986e852229838326417ff21591aa1dd80b6802cb525f0fef11
Deleted: sha256:2b6a4216580becae60807758c16536a01905e948979e83aa4d5bcae38d988394
Deleted: sha256:358784192cf7d3f7ca689cddc5772ffba48be86ad896d6262c6176e7846488fe
Deleted: sha256:cb70afefe475738ff91ee23d15bc321b5944875a10028e542d63974c5a6a30d1
Deleted: sha256:0297ab6f17ad2da63dec94bff36f51eb6830ea4747e7db6969cb4417b5fec861
# docker rmi c52aa68da8ac
Untagged: centos-updated:v1
Deleted: sha256:c52aa68da8ac47e228cf9c99fb02829d91a962fa53f8d70234a323b7389c75fb
Deleted: sha256:52feef756b148edbe11c5138b720cbc8017093b8af0b805a21e1cb951ac188c1
Deleted: sha256:0a6d3ac4f3ec043eeccc08cd57186b5eb7e1f4971006544870b517c5f2277aa0

How to remove a multiple Docker images in a single command?

The same command, by adding all images name/id.

docker rmi image1 image2 image3 .. imagen 

Example

# docker rmi node-tkd arun_nginx b8d4b86f9b27
Untagged: node-tkd:latest
Untagged: arun_nginx:latest
Deleted: sha256:6417f05108cbac154faae31def0443fb137d120aad068ad94a16aa2eca518206
Deleted: sha256:10a180fdd1f987dc732e9d6ff96bc2f22032cf939dd2aac79cef21655ae7ac93
Deleted: sha256:81bea4a1048dbca56a08e088ca4768906cc957fd59107cac8d4be4688a059125
Deleted: sha256:b8d4b86f9b27d0af7650d75b88cf1624daf1e9f900479438bad69646f196c7c3

How to build a Docker image?

You can use the “docker build” command build an Docker image from a Dockerfile. Please see the syntax below:

docker build -t myAppver:0.0.1 .

Here myAppver:0.0.1 is the image name. In the above example, we are building the image from the directory where the Dockerfile is located. Otherwise, we can use the “-f” option to specify the location where the Docker file is located.

docker build -f path/to/Dockerfile .

Normally we write Dockerfile in the repository directory itself and build the image using (.) options as PATH. We can use “-f” flag with “docker build” to point a docker fle anywhere in your file system.

Refer An introduction note to Docker containers – basics, part 1 for more details..

How to run a Docker container from an image?

Yup, after creating an image we need to start/run the environment or container using that image. The command is “docker run.”

docker run -it --name container-name -d -p 1883:80 docker-image
-t              : Allocate a pseudo-tty
-i              : Keep STDIN open even if not attached
-d              : To start a container in detached mode, you use -d=true or just -d option.
-p              : Publish a container᾿s port or a range of ports to the host, format: ip:hostPort:containerPort

The above is a basic command to start a Docker container using a Docker image.

Replace container-name with your container name and docker-image with your Docker image.

How to enter into a running Docker container?

The command is “docker exec
See the syntax and example pasted below:

docker exec -it container-name bash
docker exec -it My_Apache_Test /bin/bash

Command to exit from a Docker container without interrupting the container?

Press the following keys..

Ctrl+p+q

You can use “Ctrl+d” command to exit from a container. But some time it may cause some issues, so using “Ctrl p q” is a good trick.

Make sure that the container status using “docker ps” command.

Tips to remove all Docker images from host?

docker rmi $(docker images -q)

docker rmi $(docker images --format "{{.ID}}")

docker rmi $ (docker images | awk {'print $3'})

Find all information about a Docker image or container using docker inspect.

It’s simple to fetch or print all the information about a Docker image from the command line. Consider the scenario, you wanna find out the the running command or exposed port or environments or mounted volume or any, any information of a Docker image, you can simply find out using the docker inspect command.

Syntax

docker inspect <image name>

Example

docker inspect wordpress:new
      "Id": "sha256:521eced3d5b7b8a85a521a7878fc57413e3e9a684a9878aa7243d0b964020c3a",
        "RepoTags": [
            "wordpress:new"
        ],
        "RepoDigests": [],
        "Parent": "sha256:d5668b07b850c439c79d472fd28d4068629d9e053ea6d2b4565843b5bf06f5ed",
        "Comment": "",
        "Created": "2018-06-27T18:29:25.342166318Z",
        "Container": "247d66f3aa77ca57ecdc721135ee7022803e562a4c064b139427d542ef0d1f71",
        "ContainerConfig": {
            "Hostname": "247d66f3aa77",
            "Domainname": "",

docker tips and tricks
docker tips and tricks

 

Is this a good thing to remove Docker images?

Absolutely, a good question, do this carefully!! Removing an image will erase all information about your container. So do this, if you want really need it.

You’re safe. This command wont erase images which are used by your running and or stopped containers. You will get alert like: More about removing images in a single short. A must read…

Docker tips and tricks will continue..

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

3 thoughts on “Docker tips and tricks

Leave a Reply

Your email address will not be published. Required fields are marked *