Gangmax Blog

First Docker Notes

Here is some notes about Docker I learned today.

How to create a Docker base image from scratch

From here.

The basic idea is to use the tool “debootstrap“, which can create a base Debian system by pulling all needed files into a given directory. Then create a tar file upon the directory and import that tar(image) file into Docker. After that the image can be used as any other ones in Docker. Debian is the best Linux distro!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# The first "wily" is the name of the target directory, the
# second wily is the mirror name(the alias of Ubuntu 15.10).
# After the execution a "wily" directory will be created.
sudo debootstrap wily wily

# The following command will create a tar stream(the image)
# and put the image into Docker.
sudo tar -C wily -c . | docker import - wily

# Then run the following command to check.
docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
wily latest b1e26f8656c7 23 minutes ago 222.1 MB
java 7 beabd1eef902 6 days ago 587.3 MB
java latest beabd1eef902 6 days ago 587.3 MB
ubuntu latest 07f8e8c5e660 3 weeks ago 188.3 MB
mysql latest 56f320bd6adc 4 weeks ago 282.9 MB

How to save/load images

One can use Docker save/load commands to persist an image into tar file and vice versa. For an example:

1
2
docker save -o wily_latest.tar wily:latest
docker load -i /media/sf_exchange/docker/ubuntu_latest.tar

Here is an article about the difference between Docker “save” and “export”.

Some other useful commands

1
2
3
4
5
6
7
8
9
10
11
# 1. Start a new container instance.
docker run -it ubuntu /bin/bash

# 2. List all container instances including the stopped ones.
docker ps -a

# 3. Start a stopped container instance.
docker start eb344a5ba6b1 # The container instance ID.

# 4. Attach to a running container instance.
docker attach eb344a5ba6b1

Install Docker Compose

From here.

1
2
3
4
5
6
7
8
9
# Need root permission to add the following files into system directory.
sudo -i
# Download the Compose binary file.
curl -L https://github.com/docker/compose/releases/download/1.3.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
# Download the Compose completion file.
curl -L https://raw.githubusercontent.com/docker/compose/1.3.1/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
# exit sudo.
exit

Comments