Fork me on GitHub

Docker 101: networking

Docker running containers inside a bridge network

The purpose of this post is to isolate each tiers of our app into different containers.

We will create a private network and run containers inside this private network.

This article is largely inspired by this great resource.

Networking

Create an isolated private bridge network

docker network create web-net    

Inspect the private network

docker network inspect web-net

Run a container inside the private bridge network with --net option

docker run --name pgsql-web --rm  -d --net web-net postgres:latest
docker run --name debian-web --rm -p 80:80 --net web-net -d benit/debian-web:latest

Inspect the private network to get IPs of containers of the private network

docker network inspect web-net

gives:

 "Containers": {
            "3729ccbb14e514fd6c8b571ed9c985c28293cb5bfdb10c6c233773f50d6ba763": {
                "Name": "debian-web",
                "EndpointID": "c370cab93cdd2ac9f30f568a1709b8c998d2ca36106d5423b484a20aadbff84f",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "61bcf5cd9838c8c2e66beef73a04a2704a3be7e2085a5c6b4ad58bd78f12a138": {
                "Name": "pgsql-web",
                "EndpointID": "128f50ead4974a725c04551cb284fec2c733a48375a4819096b0b623ff2af4ac",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },

Putting it all together

Let's create a database

docker container exec pgsql-web psql -U postgres -c "create database webapp";
docker container exec pgsql-web psql -U postgres -d webapp -c "CREATE TABLE account(user_id serial PRIMARY KEY,username VARCHAR (50) UNIQUE NOT NULL,created_on TIMESTAMP NOT NULL);" ;
docker container exec pgsql-web psql -U postgres -d webapp -c "INSERT INTO account (username,created_on ) VALUES ('foo','2019-01-01') ;" ;
docker container exec pgsql-web psql -U postgres -d webapp -c "INSERT INTO account (username,created_on ) VALUES ('bar','2019-01-02') ;" ;

Connect database container from webserver container

docker container exec -it debian-web  psql -U postgres -h 172.18.0.2 -d webapp -c "select * from account;" ;

gives:

 user_id | username |     created_on      
---------+----------+---------------------
       1 | foo      | 2019-01-01 00:00:00
       2 | bar      | 2019-01-02 00:00:00
(2 rows)

Docker 101: Dockerfile

Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Example

You will find in this repo a running Dockerfile based on a debian image that runs an Apache webserver.

You will find in this file all the base commands to manage a Dockerfile .

Install Docker on ubuntu 16.04

Install Docker CE for Ubuntu

See official documentation.

Run docker as a non-root user

See official documentation.

Configure docker to be used behind a http proxy

Create the following file /etc/systemd/system/docker.service.d/http-proxy.conf and edit it:

[Service]
Environment=http_proxy=http://user:pass@ip:port
Environment=https_proxy=http://user:pass@ip:port

Test

docker run hello-world

Install docker-compose

See official documentation.

currently, I use docker-compose v1.23.2

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version