DevOps

Docker Start Container Example

A container, in Docker terminology, is an instance of an image. A typical simile used for this, is the classes and objects in Object Oriented Programming.

This example will show how to start containers, that is, making run images instances.

For this example, Linux Mint 18 and Docker version 1.12.1 have been used.
 
 
 
 

Tip
You may skip Docker installation and jump directly to the beginning of the example below.

1. Installation

Note: Docker requires a 64-bit system with a kernel version equal or higher to 3.10.

We can install Docker simply via apt-get, without the need of adding any repository, just installing the docker.io package:

sudo apt-get update
sudo apt-get install docker.io

For more details, you can follow the Install Docker on Ubuntu Tutorial.

2. Pulling a sample image

If you have used Docker before, you (should) already know that a container is an instance of an image, so, first, we need one.

The easiest way to obtain Docker images is from Docker Hub, pulling them. We could pull, for example, an Nginx image:

docker pull nginx

You can check that you have the image on your system executing:

docker images

Which should show something similar to:

REPOSITORY         TAG             IMAGE ID            CREATED           SIZE
nginx              latest          abf312888d13        2 weeks ago       181.5 MB

3. Creating a container from an image and running it: run

Now that we have a Docker image, we can easily create a container from it.

As said before, a container is just an instance of an image. We can have several different containers from a single image, running at the same time.

The run docker command creates the container, and then runs it. So, running a container from an image is as easy as executing:

docker run <image-name|image-id>[:tag]

That means that we can create our container executing:

docker run nginx

And, also:

docker run abf312888d13

Additionally, we can also specify the tag:

docker run nginx:latest

For the previous examples, Docker would create the container from the image that already exists in our disk. But, we can also directly run containers for which we don’t have the image in the disk; Docker is clever enough to know that it has to pull it, if it cannot be found in the disk. So, we could execute:

docker run nginx:1.10

Which is an image that doesn’t exist, and Docker will do a pull followed by the run, after saying that it was unable to find the image:

Unable to find image ‘nginx:1.10’ locally

3.1. Running containers in detached mode

If you ran the container, you would saw that the container is being ran in the console, and, that if you terminate the process, the container is stopped.

Usually, we want to run the containers in background or detached. For that, we have to use the -d option:

docker run -d nginx

With this option, Docker will show the container ID, and then detach the container.

3.2. Giving a name to the container

The random names generated by Docker do not say nothing about the container, so, we should always give a meaningful name to the container. This is achieved with the pull option, e.g.:

docker run -d --name=nginx1 nginx

3.3. Mapping container ports to host ports

An essential option for most of the containers is the port publishing. If we are running a web server, as in this example, is obviously essential.

For this, we have to use the -p (--publish) option, which follows this format:

docker run -p <host-port>:<container-port> <image>

So, for example, if we would want to map the port 80 of the container to the port 80 of the host, we would execute:

docker run -d --name=nginx2 -p 80:80 nginx

Now, if we follow localhost (or 127.0.0.1) in a browser, we should see the Nginx welcome page. That is, we would be accessing the web root of the web server running in the container.

Of course, we can use any (free) port in the host:

docker run -d --name=nginx3 -p 8080:80 nginx

3.4. Run a container and get the command line

For some occasions, we just want to get the command line of a container. For this, we have to use two options: -i (--interactive and -t (--tty and also specify the shell:

docker run -i -t <image> </path/to/shell>

For example:

docker run --name=nginx4 -i -t nginx /bin/bash

And we will run the container interactively.

For exiting the container, we can execute exit inside it.

We can also combine both options in just one with -it:

docker run --name=nginx5 -it nginx /bin/bash

3.5. Specifying a username

Exists the possibility of specifying the username the container has to be ran with, with the -u (--user) option:

docker run -u <username> <image>

Applied to the previous example, for running the container not with root user, but with nginx:

docker run --name=nginx6 -u nginx -it nginx /bin/bash

4. Starting an existing container: restart

Let’s suppose that we stop one of the containers:

docker stop nginx1

Now, the nginx1 container is stopped. If we would try to create a container with the same name:

docker run --name=nginx1 nginx

Docker would throw an error, an the container wouldn’t be started:

docker: Error response from daemon: Conflict. The name “/nginx1” is already in use by container 4e3a240cd6c20d6968afdb91be14860be72ed6ca652e6d52ac8546ee5d6ce16d. You have to remove (or rename) that container to be able to reuse that name.

The message is quite explanatory: we cannot create a container with the name of another container (that’s why we have used different names for the containers in this example), even if stopped. For that, we would have to delete or rename it.

If what we want to do is to run that container, that is, that specific instance of the nginx image that we created before, we don’t have to use the start command, but restart:

docker restart <container-name|container-id>

So, for restarting our stopped container, we just have to execute:

docker restart nginx1

And it will be running again, keeping the port mapping, if specified.

5. Summary

This example has shown how to run Docker containers, that is, an instance of a Docker image. We have seen several options for these. Apart from that, we have also seen how to restart existing containers, which is slightly different from creating them.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button