Cloud

6 quick Docker tips to make managing your containers easier

There is much to be learned about Docker. So much so, it's not always easy to know where to start. Here are six quick tips to help you get up to speed on Docker management.

dockerhero.jpg
Image: Jack Wallen

Docker is the single most popular method of deploying containers on the planet, with good reason. It's powerful, flexible, open source, and can be made simpler than you might expect. However, it's also easy to get bogged down in the management and running of your containers. That's why I've decided to shell out some of my favorite quick tips to help you make Docker much more manageable in the day-to-day.

And so, without further ado, let's get to the tips.

Running Docker without root (or sudo)

By default, the Docker command can only be run by root or with the help of sudo. You might have users that need to run Docker, but don't have access to the root user. For that, you need to add those users to the Docker group with the following commands:

sudo gpasswd -a USERNAME docker

Where USERNAME is the user to be added. The Docker group should already exist, so there is no need to first add it. The only thing remaining is to restart Docker with the command:

sudo service docker restart

Now the user you just added to the group can run Docker without having to employ sudo or first change to the root user.

Make use of Dockerfiles

Deploying a Docker container can get really cumbersome because the commands are often unwieldy (and a serious challenge to remember). Instead of working from the command line, you should be making use of Dockerfiles. A Dockerfile is a short recipe that describes the files, environment, and commands that make up an image. Once you've created the Dockerfile, it can be easily used with a command like:

docker build -t dockerfilename .

Where dockerfilename is the name of your Dockerfile.

Note: I'll be covering the creation of Dockerfiles at a later date.

A quick lesson on running containers

I want to show you how easy it is to start up a Docker container, and gain an interactive prompt for the newly running container. To do this I will demonstrate by spinning up a Ubuntu container.:

Pull the image with the command:

sudo docker pull ubuntu

Run the container with an interactive shell with the command:

sudo docker run -it ubuntu

At this point your command prompt will change, indicating you are in the container interactive shell (Figure A).

Figure A

Figure A

The Ubuntu container interactive shell.

You are now working inside of the Ubuntu container. That is Docker at its most basic. Simple, right?

Aliases

Many Linux admins create their own aliases for various commands. Because some Docker commands can get a bit long (or just hard to remember), it's always best to create aliases. These aliases are added to either your ~/.bashrc or ~/.bash_aliases file and can look like:

alias drm="docker rm"
​alias dps="docker ps"
​alias dl='docker ps -l -q'

Now you only would have to remember the aliases drm, dps, and dl. A much easier route to working with Docker.

Find a container IP Address

This is a necessity, especially when you're working with containers that need to be accessed from your network. If you have such a running container, to locate the IP address you first must have the container ID. To do this, issue the command docker ps -a. The container ID will be in the first column and will be in the form bf8f8f2c5b72. With that string of characters in hand, you will then issue the following command to locate the IP address for that container:

sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' CONTAINER_ID

Where CONTAINER_ID is the actual ID of the container in question.

You should now see the IP address of your container (Figure B).

Figure B

Figure B

Listing the IP Address of a running container.

Binding ports

Say you want to run an Nginx container, but port 80 is already taken by another service on the machine. You can bind a host port to a container port (say, host port 8080 to container port 80) when issuing the command to run your container. A command to do this would look like:

sudo docker run --name docker-nginx -p 8080:80 -d -v /docker-nginx/html:/usr/share/nginx/html nginx

The important part in the section above is the -p 8080:80 section. If you now point a browser to the IP address of your Docker server at port 8080, the nginx server will be visible. Granted, there's much more to it than that (which I will explain in a later post), but you get the idea. Port binding is a very important element of Docker.

There's so much more to it

Although Docker can be used very easily, it is quite a complex system. There's so much more to it than most understand. These tips should help you on your journey to making Docker a manageable tool for your company. We'll be revisiting Docker quite a bit, to continue gaining fundamental knowledge of this game-changing technology.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox