Over a million developers have joined DZone.

3 Docker Tips and Tricks

DZone's Guide to

3 Docker Tips and Tricks

Learn several commands and tools that can help you out and save you time when you delve into Docker both locally and in the cloud.

Free Resource

Today’s data climate is fast-paced and it’s not slowing down. Here’s why your current integration solution is not enough. Brought to you in partnership with Liaison Technologies.

Over the past few months, we've done a lot of development with Docker. There are a few things that we end up using over and over. I wanted to share three of these with other developers working with Docker.

Remove All Containers

Inevitably, during development, you're going to pile up a bunch of stale containers that are just lying around — or maybe you have a bunch of running containers that you don't use. We end up needing to wipe out all the containers to start fresh all the time. Here's how we do it:

docker ps -a -q | awk '{print $1}' | xargs --no-run-if-empty docker rm -f

It's pretty self-explanatory — it lists all the containers and then removes each one by its ID. There are several incarnations of this, but this one has the advantage that it can be used in Windows if you install UNIX command line tools (you could do that by grabbing MinGW, for example). Alternatively, on Windows, you can use: 

FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm -f %i

Mount the Docker Unix Socket as a Volume

OK, the way we use Docker is a bit more advanced than the standard use cases, but it's crazy how often we end up using this one. That's because we always end up having to create Docker containers from within a Docker container. And the best way to do this is to mount the Docker daemon's Unix socket on the host machine as a volume at the same location within the container. That means you add the following when performing a docker run:

-v /var/run/docker.sock:/var/run/docker.sock 

Now, within the container, if you have a Docker client (whether that's the command line one, or a Java one, for example) connect to that Unix socket. It actually talks to the Docker daemon on the host. That means if you create a container from within the container with the volume, the new container is created using the daemon running on the host (meaning it will be a sibling of the container with the volume)! Very useful!

Consider Terraform as an Alternative to Compose

Terraform is for setting up infrastructure really easily, and it's great for that. For us, infrastructure means AWS when running in the cloud and Docker when running locally. We have several containers that we have to run for our application — during development, we run all the containers locally, and in the cloud, we run the containers across various EC2 instances, each instance getting one or more containers. This is perfect for Terraform.

We can use the Docker provider alone to configure resources to run our local setup, and we can use it together with the AWS provider to run our cloud setup. Note again that Terraform is for infrastructure setup, so you are doing things at a very high level — you may find that you need to do some prep using other tools to be able to work with Terraform. For example, you can't use Dockerfiles — you will have to build your custom images prior to using them with Terraform.

Related Refcard:

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

unix ,instances ,daemon ,terraform ,docker ,ec2 ,command line ,containers

Published at DZone with permission of Ravi Chodavarapu, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}