Learn Docker — 8 Docker Tips You Oughta Know

By now, most of the avid programmers would have known or used Docker, a container technology provides a self-contained execution environments that share the kernel of the host operating system, which provide an ease way to move to different environment. There are tons of sites to get started (the official site is a great way to learn more or for windows user). Here we will see about 8 Docker tips to make daily life with it ease. It’s not just about shortcut keys but using existing commands in which didn’t use.

8 Awesome Docker Tips

1. Using ‘Docker Compose’

Do you create a ‘shell’ and run each morning when starting your pc?
Do still running multiple containers one by one in the proper order?

Well there is something called ‘Docker Compose’ – It can run multiple containers, choose which one to start first and lot more in just a single YAML file. Every morning, all you need to do is run docker-compose start thats it! Lets ease the life!
Following is an example for running 2 containers, apache and mysql, by starting mysql container first.

 

Save this as ‘mywebapp.yml’ and just do ‘docker-compose up’ to start it.
Learn more about Docker Compose from https://docs.docker.com/compose/compose-file/compose-file-v2/

2) Use Docker Hub

There is an Docker image for most of things already there in Docker hub. Instead of recreating everything from scratch, all we can do is to run ‘docker pull…’ command. But my preference is always to use an Official Docker image so that it can be trusted for my projects.

Example:
Need mysql 5.7 and 5.5 version? just do ‘docker pull mysql:5.7’ and ‘docker pull mysql:5.5’ you will now have both images installed very easily in your machine. Now, all you need to is to mention the image name along with version in your docker-compose file to switch over. (one of the many uses of having Docker)
Some of the useful Docker images from hub”

1. https://hub.docker.com/_/nginx/
2. https://hub.docker.com/_/httpd/
3. https://hub.docker.com/_/mysql/
4. https://hub.docker.com/_/php/
5. https://hub.docker.com/_/composer/

3. Switching to Alpine

Even though the size of the Docker images are not huge compared to virtual machine which we use before (like Vagrant), sometimes, its still high. A Ubuntu base image can be around 200MB which is quite high. The problem with huge size is the docker build will be slow. The solution for this to choose an Alpine version of Docker – A minimal Docker image based on Alpine Linux with a complete package index and only 5 MB in size!

docker alpine
docker alpine

https://hub.docker.com/_/alpine/

4. Restart Policy

Docker containers, once started will run until the process/command mentioned in the containers are completed and eventually will be stopped and status will be in ‘Exited’. What if you have to restart containers automatically on every exit? or restart only if there is an error? or restart n number of times? There are lot of solution like using Supervisord to handle this. But much more easier (but limited) way is to use the Docker –restart policy.

docker restart policy
docker restart policy

https://docs.docker.com/engine/admin/start-containers-automatically/

5. Clean Up

If you are using Docker for several months, chances are your system will be left with lot of junks. It can be dangling images, or unused memory, more like garbage collections. It can actually reduce few MB’s.

The reason for these dumps can happen whenever building images are abruptly stopped due to an error or containers are killed without stopping properly.

To solve this, there is ‘Docker Prune’

  • Allows cleaning by unused dangling images. (not all images)

  • It will remove all stopped containers (in case you have forgotten to give –rm when running)

  • Allows cleaning by unused dangling images

  • All networks not used by at least one container.

  • Does everything from 1 – 4

https://docs.docker.com/engine/admin/pruning

6. Filtering

Another effective use of Docker is using Filters. There are tons of filters provided by Docker (Checkout the reference section). Few of the useful filters are

  • Lists all container which are exited status
    • Other status filters values: created, restarting, running, removing, paused, exited and dead.

  • Lists all containers that use the latest ubuntu image
    • Other ancestor filters values, tags, image id

  • Lists all containers that mount a specific volume or have a volume mounted in a specific path

https://docs.docker.com/engine/reference/commandline/ps

7. Docker Stats

Once you get your Docker build application to production, you outta know more about metrics like resource usages like memory etc. For this we have ‘docker stats’.

The above format is a customised one which will neatly display how much memory docker container is used. (running and stopped)

docker stats
docker stats

https://docs.docker.com/engine/reference/commandline/stats

8. Docker Tips : Command Line

Few of the awesome command line tricks:

  • Remove stop containers

  • Remove dangling images

(or you can use docker prune) latest version

  • Get Docker container IP

  • Export & Import
    Export

Import

 

Well, these covers most of them and I really hope these docker tips are useful to developers who are new and still learning about Docker. There are really tons of tips still out there and best place to learn more about these docker tips to start with Docker reference site.