How to install Docker and docker-compose

A roadmap to install Docker and docker-compose in Linux.

Docker is a tool to wrap a piece of software in a complete filesystem that contains everything needed to run an application, such as code, runtime, system tools, and system libraries. This guarantees that the software will always run in the same way, regardless of its environment.

docker-compose is a tool to aid in starting a plenty of software using Docker and configuring anything in a yml file. With this you don’t need to know by heart any large command line nor to create a script for that.

To Install

If you don’t use pacman to install software (or you don’t know what that means), run the following command to install Docker:

%  wget -nv -O- [https://get.docker.com/](https://get.docker.com/) | sh

This script will run in Debian, Fedora, Oracle, CentOS, and RedHat (as well as any derived distro).

Now let’s go ahead and configure it.

Starting Docker

If you don’t know how the booting system works on your machine (or you don’t know what that means), it is likely that docker already starts as soon as your machine boots up, but if you need to manually start Docker, you can do it with this command:

% sudo start docker

And if you receive this error:

% sudo start docker
start: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused

You are likely running a new version of Ubuntu and you have to run this:

% sudo service docker start

Tip by Marina Limeira 🙂

Alright, let’s move on to the configuration.

Configuring Docker

Now you have to add your user to a group called docker.

There are a lot of ways to do this, here is a simple approach that works in any distribution:

1º. Edit /etc/group , this is a system file so you will have to prefix your command with sudo, like:

%  sudo nano /etc/group

2º. Look for a line similar to:

docker:x:142:

There is no problem if the group number is different. Now add your username to the group:

docker:x:142:meu_usuario

In case your computer is used by more than one person, split the usernames using comma, i.e.:

docker:x:142:meu_usuario,dmitry

Use control+x and enter to save the file.

Once more: there are a lot of ways to do this, but I will show you only one:

3º. Logout from your user account then login again.

To test, open your terminal and run:

%  docker ps

If the return of this command is something like:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS

It means everything is ok :).

Checking the configuration

To test using Ruby REPL:

%  docker run -it ruby irb
irb(main):001:0>

The first time you execute this command it shows you a progress bar, which is normal: Docker downloads the containers that you don’t have in your machine from docker hub.

Type exit and fire the command again: you will notice it won’t re-download the containers.

Now I will explain a little bit about the command above:

  1. docker is the software you run to connect to Docker daemon;

  2. run is a subcommand, it means to “run a software in a container and exit”;

  3. The -it means to run Docker in interactive mode and tty mode, i.e., to run a command and “attach” it to your terminal 😛

  4. ruby is the name of the image in Docker Hub;

  5. irb is the command to run within the container. Try to execute the same command using bash instead.

To Install docker-compose

The installation of docker-compose is the one I see people making mistakes more often (including myself).

In Linux:

1º. Change to root:

%  sudo su -

2º. And execute this line:

curl -L [https://github.com/docker/compose/releases/download/1.8.0/docker-compose-uname](https://github.com/docker/compose/releases/download/1.8.0/docker-compose-uname) -s-uname -m` > /usr/local/bin/docker-compose

And then:

chmod +x /usr/local/bin/docker-compose

This information is in Install Docker Compose — Docker.

Conclusion

Docker and docker-compose are tools I am using a lot lately. I have already uninstalled databases, Ruby and other development tools from my computer.

To test your installation and have a starting point adding them to your project, I recommend you to take a look in these two projects:

You only need to go through their README. Have fun!

Thanks to Thiago Araújo Silva and Will Soares.

We want to work with you. Check out our "What We Do" section!