Getting Started with Docker

Docker container tips
Docker container tips-

Docker container tips

We are back with other post about Docker. Containers are so hot at the moment that we couldn’t resist 🙂
We are going to continue where we left from our previous post. We will provide some examples and tips on how to run and troubleshoot Docker containers. On this Getting Started with Docker post we will describe how to name containers, how to start/stop them and the basics about docker logs.

Container naming

Docker will automatically generate a name randomly for each container we create.
If we want to specify a particular container name in place of the automatically generated name, we can do so using the –name flag:

sudo docker run --name australtech_container -i -t ubuntu /bin/bash
root@45ad6b72f148:/# exit

We can use the container name in place of the container ID in most Docker commands, as we’ll see. Container names are useful to help us identify and build logical connections between containers and applications. It’s also much easier to remember a specific container name (e.g., web or db ) than a container ID or even a random name. I recommend using container names to make managing your containers easier.

Names are unique. If we try to create two containers with the same name, the command will fail. We need to delete the previous container with the same name before we can create a new one. We can do so with the docker rm command.

Starting a stopped container

If we want, we can restart a stopped container running:

docker start australtech_container

We could also refer to the container by its container ID instead.

docker start 45ad6b72f148

Attaching to a container

Our container will restart with the same options we’d specified when we launched it with the docker run command. So there is an interactive session waiting on our running container. We can reattach to that session using the docker attach command.

 sudo docker attach australtech_container

Creating daemonized containers

In addition to these interactive containers, we can create longer-running containers. Daemonized containers don’t have the interactive session we’ve just used and are ideal for running applications and services. Most of the containers you’re likely to run will probably be daemonized. Let’s start a daemonized container now:

sudo docker run --name daemon_australtech -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"


Here, we’ve used the docker run command with the -d flag to tell Docker to detach the container to the background.
We’ve also specified a while loop as our container command. Our loop will echo hello world over and over again until the container is stopped or the process stops.
With this combination of flags, you’ll see that, instead of being attached to a shell like our last container, the docker run command has instead returned a container ID and returned us to our command prompt. Now if we run docker ps , we see a running container.

docker ps
 CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
 e3ac25e9b6cd        ubuntu              "/bin/sh -c 'while t…"   About a minute ago   Up About a minute                       daemon_australtech

Container logs

Docker container tips. Docker logs

We now have a daemonized container running our while loop; let’s take a look inside the container and see what’s happening. To do so, we can use the docker logs command. The docker logs command fetches the logs of a container.

sudo docker logs daemon_australtech
 hello world
 hello world
 hello world

Here we see the results of our while loop echoing hello world to the logs. Docker will output the last few log entries and then return. We can also monitor the container’s logs much like the tail -f binary operates using the -f flag.

You can also tail a portion of the logs of a container, again much like the tail command with the -f –tail flags. For example, you can get the last ten lines of a log by using docker logs –tail 10 daemon_australtech . You can also follow the logs of a container without having to read the whole log file with docker logs –tail 0 -f daemon_australtech .
To make debugging a little easier, we can also add the -t flag to prefix our log entries with timestamps.

sudo docker logs -ft daemon_australtech
 2019-04-19T19:13:13.373241730Z hello world
 2019-04-19T19:13:14.373412221Z hello world
 2019-04-19T19:13:15.374226271Z hello world
 2019-04-19T19:13:16.375048567Z hello world

You can also control the logging driver used by your daemon and container. This is done using the –log-driver option. You can pass this option
to both the daemon and the docker run command.
There are a variety of options including the default json-file which provides the behavior we’ve just seen using the docker logs command.
Also available is syslog which disables the docker logs command and redirects all container log output to Syslog. You can specify this with the Docker daemon to output all container logs to Syslog or override it using docker run to direct output from individual containers.

sudo docker run --log-driver="syslog" --name daemon_australtech -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"

This will cause the container to log to Syslog and result in the
docker logs command showing no output.
Lastly, also available is none , which disables all logging in containers and results in the docker logs command being disabled.

This is all for now in regards to Docker container tips. But don’t worry we are not done with the Docker posts!