During the past year and as part of our philosophy of continuous improvement, we have been conducting various shared-training sessions, in which our team members themselves share their knowledge, experiences and ideas. As we know, constant training and continuous learning are essential to keep up to date in a constantly evolving technological world.

In this series of posts, we will discuss some key aspects of these sessions.

Training Pill 04:

Docker

Teach:

Fran Moreno

Meaning of most common options

docker run –name postgres -v ./pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=postgres –restart=always -p 5432:5432 -d postgres:9.6

–name → container name
-v → volume you want to assign to the container
-e → container variables
-p → ports to expose
-d → run in background
–restart → choose when to restart the archive (always, on-failure, unless-stopped, no)


Executing commands in a container

To run commands in a container you use “docker exec”, for example:

docker exec -it container-name mkdir test

If we want to execute several commands we can do it with:

docker exec -it container-name bash -c “mkdir test; rmdir test”.

We can also connect to a console as if we were inside the container with:

docker exec -it bash-container-name


How to connect to an unexposed port of a container

To connect to an unexposed port of a container as for example to a postgresql you have to know the IP of the container, which can be obtained with the command “docker inspect container-name” which returns all the information of the container and at the end of the whole will put something like:

“NetworkID”: “086d8c1d41a96207054f480272b16e0a4c327df9565c0a19bef54c1b43337bb1”, “EndpointID”: “fb07794d9ef7d2982dcc76065fe70758f0dc0289f1d24695f47a7fa3e25f39ce”, “Gateway”: “192.168.208.1”,
“IPAddress”: “192.168.208.4”,
“IPPrefixLen”: 20,
“IPv6Gateway”: “”, “GlobalIPv6Address”: “”, “GlobalIPv6PrefixLen”: 0, “MacAddress”: “02:42:c0:a8:d0:04”, “DriverOpts”: null.

Where IPAddress is the IP of the container.

Once we know the IP we could establish the connection as long as the container is on the same machine where we establish the connection, otherwise we should open a SSH tunnel to the machine where the container is located.

 

Copying files from the host to a container and vice versa

With the command “docker cp” you can copy files between containers and host, the syntax is as follows:

To copy files from the host to a container:
docker cp /path/test.txt container-name:/path

To copy files from a container to the host:
docker cp container-name:/path/test.txt /path


Container images

To create a docker image we use the command “docker build” when executing this command what it does is to execute the commands that have been defined in the Dockerfile.

To see the images that we have in our computer we use the command “docker image ls”, to delete an image that we are not using we use “docker image rm image-name” and to delete all the images that we are not using we use the command “docker image prune -a”.


Networks

Networks are used to group containers in different subnets and containers within a network can only connect to containers that are within the same network.

 

Docker compose

With docker compose you can build several containers at the same time with their respective configurations, for example:


version: “3.9” services:
db:
image: mysql:5.7
volumes:
– ./mysql-dump/init.sql:/docker-entrypoint-initdb.d/1-init.sql – ./db-data:/var/lib/mysql
restart: always ports:
– “33060:3306” environment:
MYSQL_ROOT_PASSWORD: calblanque MYSQL_DATABASE: calblanque MYSQL_USER: calblanque MYSQL_PASSWORD: calblanque
wordpress: depends_on:
– db
image: wordpress:latest
volumes:
– .:/var/www/html
ports:
– “8000:80”
restart: always environment:
WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: calblanque WORDPRESS_DB_PASSWORD: calblanque WORDPRESS_DB_NAME: calblanque


Passing a running container to another machine

To move a running container from one machine to another you must do the following: Save the container as a docker image:

docker commit container-name container-name-image

Save the image in a zip file:

docker save image-name | gzip > image-name.tar.gz

Load the compressed image on the other machine:

gunzip -c image-name.tar.gz | docker load

Finally re-run “docker run” to lift the container:

docker run -d –name container-name container-name-image

Admin